/** UTM parameters captured from the storefront URL at checkout start. */ export interface UtmParams { utm_source?: string; utm_medium?: string; utm_campaign?: string; } const UTM_KEYS = ['utm_source', 'utm_medium', 'utm_campaign'] as const; /** Read UTM params from the current page URL (web). Returns {} on native or SSR. */ export function captureUtmFromUrl(href?: string): UtmParams { try { const url = new URL(href ?? (typeof window !== 'undefined' ? window.location.href : '')); const out: UtmParams = {}; for (const key of UTM_KEYS) { const v = url.searchParams.get(key); if (v) out[key] = v.slice(0, 256); } return out; } catch { return {}; } } /** True when at least one UTM field is set. */ export function hasUtmParams(utm: UtmParams): boolean { return Boolean(utm.utm_source || utm.utm_medium || utm.utm_campaign); }