— End") or GTM Custom HTML on All Pages). Stores the FIRST utm/gclid this browser arrived with for 90 days, then feeds it to forms. v3 added axx_gclid + first_touch_date fills (GHL reserves the bare name "gclid"). v4: later clicks BACKFILL missing fields (first value per field still wins) — v3 locked the whole record, so a first visit without a gclid left axx_gclid permanently empty in that browser. */ (function () { var KEY = "axx_first_touch"; var PARAMS = ["utm_source","utm_medium","utm_campaign","utm_term","utm_content","gclid"]; try { var q = new URLSearchParams(location.search); var incoming = {}; var has = false; PARAMS.forEach(function (p) { var v = q.get(p); if (v) { incoming[p] = v; has = true; } }); var stored = null; try { stored = JSON.parse(localStorage.getItem(KEY) || "null"); } catch (e) {} if (stored && stored._ts && (Date.now() - stored._ts > 90 * 864e5)) stored = null; // expired if (has) { if (!stored) { // first touch — start the record incoming._ts = Date.now(); stored = incoming; } else { // later touch — fill gaps only PARAMS.forEach(function (p) { if (incoming[p] && !stored[p]) stored[p] = incoming[p]; }); } localStorage.setItem(KEY, JSON.stringify(stored)); } if (!stored) return; var ftd = ""; try { ftd = stored._ts ? new Date(stored._ts).toISOString().slice(0, 10) : ""; } catch (e) {} var fill = function () { // A) GHL embedded forms/surveys: append params to the iframe URL — // GHL prefills any form field whose key matches the param name. document.querySelectorAll('iframe[src*="/widget/form/"], iframe[src*="/widget/survey/"]') .forEach(function (f) { var url = new URL(f.src); PARAMS.forEach(function (p) { if (stored[p] && !url.searchParams.get(p)) url.searchParams.set(p, stored[p]); }); if (stored.gclid && !url.searchParams.get("axx_gclid")) { url.searchParams.set("axx_gclid", stored.gclid); } if (ftd && !url.searchParams.get("first_touch_date")) { url.searchParams.set("first_touch_date", ftd); } if (f.src !== url.toString()) f.src = url.toString(); }); // B) Plain HTML forms: fill hidden inputs named utm_source, gclid, etc. PARAMS.forEach(function (p) { if (!stored[p]) return; document.querySelectorAll('input[name="' + p + '"]').forEach(function (i) { if (!i.value) i.value = stored[p]; }); }); if (stored.gclid) { document.querySelectorAll('input[name="axx_gclid"]').forEach(function (i) { if (!i.value) i.value = stored.gclid; }); } if (ftd) { document.querySelectorAll('input[name="first_touch_date"]').forEach(function (i) { if (!i.value) i.value = ftd; }); } }; if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", fill); } else { fill(); } setTimeout(fill, 1500); // late-injected embeds } catch (e) {} })();