|
| 1 | +// Shared client-side consent logic for GDPR-compliant Google Analytics under |
| 2 | +// Consent Mode v2. No-op unless a GA4 Measurement ID is set at build time via |
| 3 | +// `NEXT_PUBLIC_GOOGLE_ANALYTICS_ID`. |
| 4 | + |
| 5 | +declare global { |
| 6 | + interface Window { |
| 7 | + dataLayer: unknown[]; |
| 8 | + gtag: (...args: unknown[]) => void; |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +export const GA_ID = process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_ID; |
| 13 | +export const GA_ENABLED = Boolean(GA_ID); |
| 14 | + |
| 15 | +export const CONSENT_STORAGE_KEY = 'qat-analytics-consent'; |
| 16 | + |
| 17 | +// Fired on `window` when the stored choice changes / to re-open the banner. |
| 18 | +export const CONSENT_CHANGE_EVENT = 'qat-consent-change'; |
| 19 | +export const OPEN_CONSENT_EVENT = 'qat-open-consent'; |
| 20 | + |
| 21 | +export type ConsentChoice = 'granted' | 'denied'; |
| 22 | + |
| 23 | +// Fallback for when localStorage is blocked: keeps the choice for the session. |
| 24 | +let memoryConsent: ConsentChoice | null = null; |
| 25 | + |
| 26 | +export function getStoredConsent(): ConsentChoice | null { |
| 27 | + if (typeof window === 'undefined') return null; |
| 28 | + try { |
| 29 | + const value = window.localStorage.getItem(CONSENT_STORAGE_KEY); |
| 30 | + if (value === 'granted' || value === 'denied') return value; |
| 31 | + } catch { |
| 32 | + // Storage blocked — fall back to the in-memory choice. |
| 33 | + } |
| 34 | + return memoryConsent; |
| 35 | +} |
| 36 | + |
| 37 | +export function setStoredConsent(choice: ConsentChoice): void { |
| 38 | + if (typeof window === 'undefined') return; |
| 39 | + memoryConsent = choice; |
| 40 | + try { |
| 41 | + window.localStorage.setItem(CONSENT_STORAGE_KEY, choice); |
| 42 | + } catch { |
| 43 | + // Storage blocked — the in-memory value keeps the choice for this session. |
| 44 | + } |
| 45 | + window.dispatchEvent(new CustomEvent<ConsentChoice>(CONSENT_CHANGE_EVENT, { detail: choice })); |
| 46 | +} |
| 47 | + |
| 48 | +// Only analytics storage is managed (no ads). |
| 49 | +export function updateGtagConsent(choice: ConsentChoice): void { |
| 50 | + if (typeof window === 'undefined' || typeof window.gtag !== 'function') return; |
| 51 | + window.gtag('consent', 'update', { analytics_storage: choice }); |
| 52 | +} |
| 53 | + |
| 54 | +// Deletes GA first-party cookies (`_ga*`) so a withdrawn consent also removes |
| 55 | +// identifiers already on the device. No-op when none exist. |
| 56 | +export function clearAnalyticsCookies(): void { |
| 57 | + if (typeof document === 'undefined') return; |
| 58 | + |
| 59 | + const names = document.cookie |
| 60 | + .split('; ') |
| 61 | + .map((cookie) => cookie.split('=')[0]) |
| 62 | + .filter((name) => name.startsWith('_ga')); |
| 63 | + if (names.length === 0) return; |
| 64 | + |
| 65 | + // GA scopes cookies to the host or the registrable domain (sometimes with a |
| 66 | + // leading dot), so expire every plausible domain/path combination. |
| 67 | + const { hostname } = window.location; |
| 68 | + const domains = ['', hostname, `.${hostname}`]; |
| 69 | + const parts = hostname.split('.'); |
| 70 | + if (parts.length > 2) { |
| 71 | + const registrable = parts.slice(-2).join('.'); |
| 72 | + domains.push(registrable, `.${registrable}`); |
| 73 | + } |
| 74 | + |
| 75 | + for (const name of names) { |
| 76 | + for (const domain of domains) { |
| 77 | + const scope = domain ? `; domain=${domain}` : ''; |
| 78 | + document.cookie = `${name}=; path=/; max-age=0${scope}`; |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments