Created
January 20, 2026 10:10
-
-
Save ipabz/845809c05550e763f8b86a98295adf01 to your computer and use it in GitHub Desktop.
Captures first-touch attribution IDs from URL params, stores them safely in localStorage, and injects them into Alia popup events for consistent downstream tracking.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function () { | |
| /** | |
| * ================================= | |
| * CONFIG | |
| * ================================= | |
| */ | |
| var STORAGE_PREFIX = "first_"; | |
| var ATTRIBUTION_KEYS = ["gclid", "gbraid", "wbraid", "fbclid"]; | |
| var DEBUG = false; // set true temporarily during testing | |
| /** | |
| * ================================= | |
| * SAFE HELPERS | |
| * ================================= | |
| */ | |
| function safeGetParam(name) { | |
| try { | |
| return new URLSearchParams(window.location.search).get(name); | |
| } catch (e) { | |
| return null; | |
| } | |
| } | |
| function safeGetStorage(key) { | |
| try { | |
| return localStorage.getItem(key); | |
| } catch (e) { | |
| return null; | |
| } | |
| } | |
| function safeSetStorage(key, value) { | |
| try { | |
| if (value && !localStorage.getItem(key)) { | |
| localStorage.setItem(key, value); | |
| } | |
| } catch (e) {} | |
| } | |
| function log() { | |
| if (DEBUG && window.console) { | |
| console.log.apply(console, arguments); | |
| } | |
| } | |
| /** | |
| * ================================= | |
| * CAPTURE FIRST TOUCH | |
| * ================================= | |
| */ | |
| function captureAttribution() { | |
| ATTRIBUTION_KEYS.forEach(function (key) { | |
| var value = safeGetParam(key); | |
| if (value) { | |
| safeSetStorage(STORAGE_PREFIX + key, value); | |
| log("[Attribution] Captured", key, value); | |
| } | |
| }); | |
| } | |
| /** | |
| * ================================= | |
| * BUILD PAYLOAD FOR ALIA | |
| * ================================= | |
| */ | |
| function getStoredAttribution() { | |
| var props = {}; | |
| ATTRIBUTION_KEYS.forEach(function (key) { | |
| var stored = safeGetStorage(STORAGE_PREFIX + key); | |
| if (stored) { | |
| props[key] = stored; | |
| } | |
| }); | |
| return Object.keys(props).length ? props : null; | |
| } | |
| /** | |
| * ================================= | |
| * PUSH INTO ALIA | |
| * ================================= | |
| */ | |
| function pushToAlia() { | |
| var properties = getStoredAttribution(); | |
| if (!properties) { | |
| log("[Attribution] No stored properties to push"); | |
| return; | |
| } | |
| window.alia = window.alia || []; | |
| window.alia.push({ | |
| type: "setProperties", | |
| properties: properties, | |
| }); | |
| log("[Attribution] Pushed to Alia", properties); | |
| } | |
| /** | |
| * ================================= | |
| * INIT | |
| * ================================= | |
| */ | |
| captureAttribution(); | |
| // Run at multiple safe lifecycle points | |
| document.addEventListener("alia:popupView", pushToAlia); | |
| document.addEventListener("alia:signup", pushToAlia); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment