|
| 1 | +/** |
| 2 | + * UTM Tracking Utilities |
| 3 | + * Handles extraction and storage of UTM parameters for user registration tracking |
| 4 | + */ |
| 5 | + |
| 6 | +export interface UTMParameters { |
| 7 | + utm_source?: string; |
| 8 | + utm_medium?: string; |
| 9 | + utm_campaign?: string; |
| 10 | + utm_term?: string; |
| 11 | + utm_content?: string; |
| 12 | +} |
| 13 | + |
| 14 | +const UTM_STORAGE_KEY = "nilgpt_utm_params"; |
| 15 | + |
| 16 | +/** |
| 17 | + * Extracts UTM parameters from the current URL |
| 18 | + */ |
| 19 | +export function extractUTMParameters(): UTMParameters { |
| 20 | + if (typeof window === "undefined") { |
| 21 | + return {}; |
| 22 | + } |
| 23 | + |
| 24 | + const urlParams = new URLSearchParams(window.location.search); |
| 25 | + const utmParams: UTMParameters = {}; |
| 26 | + |
| 27 | + // Standard UTM parameters |
| 28 | + const utmKeys = [ |
| 29 | + "utm_source", |
| 30 | + "utm_medium", |
| 31 | + "utm_campaign", |
| 32 | + "utm_term", |
| 33 | + "utm_content", |
| 34 | + ]; |
| 35 | + |
| 36 | + utmKeys.forEach((key) => { |
| 37 | + const value = urlParams.get(key); |
| 38 | + if (value) { |
| 39 | + utmParams[key as keyof UTMParameters] = value; |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + return utmParams; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Stores UTM parameters in session storage |
| 48 | + */ |
| 49 | +export function storeUTMParameters(utmParams: UTMParameters): void { |
| 50 | + if (typeof window === "undefined") { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // Only store if there are actual UTM parameters |
| 55 | + const hasUTMParams = Object.values(utmParams).some( |
| 56 | + (value) => value !== undefined, |
| 57 | + ); |
| 58 | + |
| 59 | + if (hasUTMParams) { |
| 60 | + try { |
| 61 | + sessionStorage.setItem(UTM_STORAGE_KEY, JSON.stringify(utmParams)); |
| 62 | + } catch (error) { |
| 63 | + console.warn("Failed to store UTM parameters in session storage:", error); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Retrieves stored UTM parameters from session storage |
| 70 | + */ |
| 71 | +export function getStoredUTMParameters(): UTMParameters { |
| 72 | + if (typeof window === "undefined") { |
| 73 | + return {}; |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + const stored = sessionStorage.getItem(UTM_STORAGE_KEY); |
| 78 | + if (stored) { |
| 79 | + return JSON.parse(stored); |
| 80 | + } |
| 81 | + } catch (error) { |
| 82 | + console.warn( |
| 83 | + "Failed to retrieve UTM parameters from session storage:", |
| 84 | + error, |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + return {}; |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Clears stored UTM parameters from session storage |
| 93 | + */ |
| 94 | +export function clearStoredUTMParameters(): void { |
| 95 | + if (typeof window === "undefined") { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + try { |
| 100 | + sessionStorage.removeItem(UTM_STORAGE_KEY); |
| 101 | + } catch (error) { |
| 102 | + console.warn("Failed to clear UTM parameters from session storage:", error); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Captures and stores UTM parameters from the current URL |
| 108 | + * Call this on landing page load |
| 109 | + */ |
| 110 | +export function captureAndStoreUTMParameters(): UTMParameters { |
| 111 | + const utmParams = extractUTMParameters(); |
| 112 | + storeUTMParameters(utmParams); |
| 113 | + return utmParams; |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Gets UTM parameters for user registration |
| 118 | + * First tries to get from current URL, then falls back to stored parameters |
| 119 | + * This handles the case where user comes from email confirmation with UTM params |
| 120 | + */ |
| 121 | +export function getUTMParametersForRegistration(): UTMParameters { |
| 122 | + // First, try to get UTM parameters from current URL (for email confirmation flow) |
| 123 | + const currentUrlParams = extractUTMParameters(); |
| 124 | + |
| 125 | + // If we have UTM params in current URL, use those and store them |
| 126 | + if (Object.keys(currentUrlParams).length > 0) { |
| 127 | + storeUTMParameters(currentUrlParams); |
| 128 | + return currentUrlParams; |
| 129 | + } |
| 130 | + |
| 131 | + // Otherwise, fall back to stored parameters (for wallet signup flow) |
| 132 | + return getStoredUTMParameters(); |
| 133 | +} |
0 commit comments