function setUtmCookies() { // Helper: get query param from URL function getQueryParam(param) { const urlParams = new URLSearchParams(window.location.search); return urlParams.get(param); } // Helper: get cookie by name function getCookie(name) { const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)')); return match ? decodeURIComponent(match[2]) : null; } // Helper: set cookie if not already set function setCookieIfEmpty(name, value, days = 90) { if (!value) return; if (getCookie(name)) return; // Don't overwrite existing cookie const date = new Date(); date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); document.cookie = `${name}=${encodeURIComponent(value)}; expires=${date.toUTCString()}; path=/; SameSite=Lax`; } // Get UTM params const utmSource = getQueryParam('utm_source'); const utmMedium = getQueryParam('utm_medium'); const utmCampaign = getQueryParam('utm_campaign'); // Set cookies only if not already present setCookieIfEmpty('utm_source', utmSource, 90); setCookieIfEmpty('utm_medium', utmMedium, 90); setCookieIfEmpty('utm_campaign', utmCampaign, 90); } // Run on page load setUtmCookies();