|
| 1 | +/** |
| 2 | + * Framework-agnostic helpers for Google Analytics (GA4) + Google Consent Mode v2. |
| 3 | + * |
| 4 | + * These contain no React or Next.js dependencies so they can be reused from |
| 5 | + * Next.js apps and plain-React/MPA contexts (e.g. the sphinx injection) alike. |
| 6 | + */ |
| 7 | + |
| 8 | +export type GoogleConsentState = 'granted' | 'denied' |
| 9 | + |
| 10 | +export type GoogleConsentSettings = { |
| 11 | + ad_storage?: GoogleConsentState |
| 12 | + ad_user_data?: GoogleConsentState |
| 13 | + ad_personalization?: GoogleConsentState |
| 14 | + analytics_storage?: GoogleConsentState |
| 15 | + wait_for_update?: number |
| 16 | +} |
| 17 | + |
| 18 | +type GtagFn = (...args: unknown[]) => void |
| 19 | + |
| 20 | +type AnalyticsWindow = Window & { |
| 21 | + dataLayer?: unknown[] |
| 22 | + gtag?: GtagFn |
| 23 | +} |
| 24 | + |
| 25 | +const GA_SCRIPT_ID = 'quantinuum-google-analytics' |
| 26 | + |
| 27 | +/** |
| 28 | + * The default consent state: everything denied. In this state GA runs in |
| 29 | + * "cookieless ping" mode, recording anonymous, aggregate statistics (such as |
| 30 | + * pageview counts) without setting any tracking cookies or storing an |
| 31 | + * identifier. |
| 32 | + */ |
| 33 | +export const DEFAULT_GOOGLE_CONSENT: Required<Omit<GoogleConsentSettings, 'wait_for_update'>> = { |
| 34 | + ad_storage: 'denied', |
| 35 | + ad_user_data: 'denied', |
| 36 | + ad_personalization: 'denied', |
| 37 | + analytics_storage: 'denied', |
| 38 | +} |
| 39 | + |
| 40 | +/** Ensures `window.dataLayer` / `window.gtag` exist and returns the gtag function. */ |
| 41 | +export function ensureGtag(): GtagFn | undefined { |
| 42 | + if (typeof window === 'undefined') { |
| 43 | + return undefined |
| 44 | + } |
| 45 | + |
| 46 | + const analyticsWindow = window as AnalyticsWindow |
| 47 | + analyticsWindow.dataLayer = analyticsWindow.dataLayer || [] |
| 48 | + analyticsWindow.gtag = |
| 49 | + analyticsWindow.gtag || |
| 50 | + ((...args: unknown[]) => { |
| 51 | + analyticsWindow.dataLayer?.push(args) |
| 52 | + }) |
| 53 | + |
| 54 | + return analyticsWindow.gtag |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Sets the Google Consent Mode v2 default. This MUST run before GA's `config` |
| 59 | + * call. Everything defaults to `denied` so GA sends anonymous cookieless pings |
| 60 | + * until the visitor consents; pass `overrides` to adjust individual signals. |
| 61 | + */ |
| 62 | +export function setGoogleConsentDefault(overrides: GoogleConsentSettings = {}): void { |
| 63 | + const gtag = ensureGtag() |
| 64 | + if (!gtag) { |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + gtag('consent', 'default', { |
| 69 | + ...DEFAULT_GOOGLE_CONSENT, |
| 70 | + wait_for_update: 500, |
| 71 | + ...overrides, |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +/** Updates the analytics consent state after the visitor makes a choice. */ |
| 76 | +export function updateAnalyticsConsent(granted: boolean): void { |
| 77 | + const gtag = ensureGtag() |
| 78 | + if (!gtag) { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + gtag('consent', 'update', { |
| 83 | + analytics_storage: granted ? 'granted' : 'denied', |
| 84 | + }) |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Initialises GA (`gtag('js')` + `gtag('config')`) and injects the gtag.js |
| 89 | + * script tag. Safe to call more than once: the script is only appended once. |
| 90 | + */ |
| 91 | +export function loadGoogleAnalytics(gaId: string): void { |
| 92 | + if (typeof document === 'undefined') { |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + const gtag = ensureGtag() |
| 97 | + if (!gtag) { |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + gtag('js', new Date()) |
| 102 | + gtag('config', gaId) |
| 103 | + |
| 104 | + const scriptUrl = new URL('https://www.googletagmanager.com/gtag/js') |
| 105 | + scriptUrl.searchParams.set('id', gaId) |
| 106 | + |
| 107 | + const existingScript = document.getElementById(GA_SCRIPT_ID) as HTMLScriptElement | null |
| 108 | + if (existingScript) { |
| 109 | + if (existingScript.src !== scriptUrl.toString()) { |
| 110 | + existingScript.src = scriptUrl.toString() |
| 111 | + } |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + const script = document.createElement('script') |
| 116 | + script.id = GA_SCRIPT_ID |
| 117 | + script.async = true |
| 118 | + script.src = scriptUrl.toString() |
| 119 | + document.head.appendChild(script) |
| 120 | +} |
0 commit comments