export const GA_MEASUREMENT_ID = "G-QTT9QT0T3S"; export const CONSENT_STORAGE_KEY = "evandro.analytics-consent.v1"; export const GA_DISABLE_KEY = "ga-disable-G-QTT9QT0T3S"; export type AnalyticsConsent = "granted" | "denied"; export type Gtag = (...args: unknown[]) => void; declare global { interface Window { dataLayer?: IArguments[]; "ga-disable-G-QTT9QT0T3S"?: boolean; gtag?: Gtag; } } export function readConsent(): AnalyticsConsent | null { try { const value = localStorage.getItem(CONSENT_STORAGE_KEY); return value === "granted" || value === "denied" ? value : null; } catch { return null; } } function analyticsPagePath(): string { const path = window.location.pathname.replace(/\/$/, "") || "/"; switch (path) { case "/": case "/servicos": case "/cases": case "/curriculo": case "/privacidade": return path; default: // Route templates keep arbitrary slugs and personal data out of metrics. if (path.startsWith("/servicos/")) return "/servicos/[slug]"; if (path.startsWith("/cases/")) return "/cases/[slug]"; return "/other"; } } /** Measures an attempted handoff, not an opened app, delivered message or lead. * Callers must supply static labels, never form values or destination URLs. */ export function trackWhatsAppOpen({ service, placement, }: { service: string; placement: string; }): void { if ( process.env.NODE_ENV !== "production" || typeof window === "undefined" || readConsent() !== "granted" || window[GA_DISABLE_KEY] === true || typeof window.gtag !== "function" ) { return; } try { const pagePath = analyticsPagePath(); window.gtag("event", "whatsapp_open", { send_to: GA_MEASUREMENT_ID, service, placement, page_path: pagePath, // Override gtag's URL defaults so query/hash/referrer data cannot leak. page_location: `https://evandro.dev.br${pagePath}`, page_referrer: "", }); } catch { // Analytics must never interrupt navigation. Dropped events are not replayed. } }