(function () { const params = new URLSearchParams(window.location.search); const source = params.get('utm_source'); const medium = params.get('utm_medium'); const campaign = params.get('utm_campaign'); function setCookie(name, value, days) { const expires = new Date(Date.now() + days * 86400000).toUTCString(); document.cookie = name + '=' + encodeURIComponent(value) + '; expires=' + expires + '; path=/' + '; SameSite=Lax'; } function getCookie(name) { return document.cookie .split('; ') .find(row => row.startsWith(name + '=')); } // Only save first touch once if ((source || medium || campaign) && !getCookie('first_touch')) { const firstTouch = { source: source || '', medium: medium || '', campaign: campaign || '', capturedDate: new Date().toISOString() }; const firstTouchJson = JSON.stringify(firstTouch); // Keep localStorage if you still want JS access localStorage.setItem('first_touch', firstTouchJson); // Add cookie so Rock/Lava can read it setCookie('first_touch', firstTouchJson, 365); // Optional individual cookies, easier for Rock workflows setCookie('utm_source', firstTouch.source, 365); setCookie('utm_medium', firstTouch.medium, 365); setCookie('utm_campaign', firstTouch.campaign, 365); setCookie('first_touch_date', new Date().toISOString().split('T')[0], 365); } })(); $(function () { // Find the Tracking fieldset. // If this page does not have a Tracking section, stop immediately. const $trackingFieldset = $("fieldset").filter(function () { return $(this).find("h4").first().text().trim() === "Tracking"; }); if (!$trackingFieldset.length) { return; } function getCookie(name) { const value = "; " + document.cookie; const parts = value.split("; " + name + "="); if (parts.length === 2) { return decodeURIComponent(parts.pop().split(";").shift()); } return ""; } function setFieldByLabel(labelText, value) { const $field = $trackingFieldset .find("label") .filter(function () { return $(this).text().trim() === labelText; }) .closest(".form-group") .find("input, textarea, select"); if ($field.length && value) { $field.val(value).trigger("change"); } } const firstTouchRaw = getCookie("first_touch"); let firstTouch = {}; try { firstTouch = firstTouchRaw ? JSON.parse(firstTouchRaw) : {}; } catch (e) { console.warn("Could not parse first_touch cookie", e); } const firstTouchDate = ( firstTouch.capturedDate || getCookie("first_touch_date") || "" ).split("T")[0]; const today = new Date().toISOString().split("T")[0]; setFieldByLabel("UTM Source", firstTouch.source || getCookie("utm_source")); setFieldByLabel("UTM Medium", firstTouch.medium || getCookie("utm_medium")); setFieldByLabel("UTM Campaign", firstTouch.campaign || getCookie("utm_campaign")); setFieldByLabel("First Touch Date", firstTouchDate); setFieldByLabel("First Form Date", today); // Hide the entire Tracking section after filling it. $trackingFieldset.hide(); });