import { utmKeys } from '../../shared/lead' /** * Ad attribution: last tagged visit wins and is kept for 30 days, so a lead * sent a week after clicking an ad (with the tags long gone from the URL) is * still credited to that campaign. A new tagged visit replaces the whole set — * a fresh utm_source is never mixed with an old campaign or click ID. * * localStorage is the main store, sessionStorage the fallback, and module * memory the last resort when the browser refuses both. */ type Key = (typeof utmKeys)[number] type Values = Partial> interface StoredRecord { version: 1 captured_at: number values: Values } const STORAGE_KEY = 'exo_attribution_v1' const TTL_MS = 30 * 24 * 60 * 60 * 1000 const STORES = ['localStorage', 'sessionStorage'] as const let current: StoredRecord | null = null const isFresh = (record: StoredRecord | null): record is StoredRecord => { if (!record) return false const age = Date.now() - record.captured_at return age >= 0 && age < TTL_MS } function clean(values: Record): Values { const out: Values = {} for (const key of utmKeys) { const value = values[key] // The server caps each value at 300 characters; trim here so an oversized // tag costs the lead one field, not a 400. if (typeof value === 'string' && value.trim()) out[key] = value.trim().slice(0, 300) } return out } function readStored(store: (typeof STORES)[number]): StoredRecord | null { try { const raw = window[store].getItem(STORAGE_KEY) if (!raw) return null const parsed = JSON.parse(raw) as Partial const record: StoredRecord = { version: 1, captured_at: Number(parsed.captured_at), values: clean(parsed.values ?? {}), } if (parsed.version === 1 && isFresh(record) && Object.keys(record.values).length) return record window[store].removeItem(STORAGE_KEY) } catch { // Blocked storage or corrupt JSON: behave as if nothing was stored. } return null } function persist(record: StoredRecord | null) { for (const store of STORES) { try { if (record) window[store].setItem(STORAGE_KEY, JSON.stringify(record)) else window[store].removeItem(STORAGE_KEY) } catch { // See readStored. } } } /** * Reads the tags off the current URL, falling back to the stored set. Called * once at startup so the visit is captured before any form is touched, and * again on every submission. */ export function collectAttribution(): Values { if (!current) { const local = readStored('localStorage') const session = readStored('sessionStorage') current = session && (!local || session.captured_at > local.captured_at) ? session : local } const incoming = clean(Object.fromEntries(new URLSearchParams(window.location.search))) const sameAsCurrent = current && JSON.stringify(incoming) === JSON.stringify(current.values) // An untagged URL never overwrites a stored source; a tagged one replaces it. if (Object.keys(incoming).length && !(sameAsCurrent && isFresh(current))) { current = { version: 1, captured_at: Date.now(), values: incoming } persist(current) } else if (current && !isFresh(current)) { current = null persist(null) } return current ? { ...current.values } : {} }