"use client"; import Script from "next/script"; import { useCallback, useEffect, useRef, useState } from "react"; const GA_MEASUREMENT_ID = "G-QTT9QT0T3S"; const CONSENT_STORAGE_KEY = "evandro.analytics-consent.v1"; const GA_DISABLE_KEY = "ga-disable-G-QTT9QT0T3S"; const GA_SCRIPT_SRC = `https://www.googletagmanager.com/gtag/js?id=${GA_MEASUREMENT_ID}`; const SITE_DOMAIN = "evandro.dev.br"; const deniedConsent = { analytics_storage: "denied", ad_storage: "denied", ad_user_data: "denied", ad_personalization: "denied", } as const; type AnalyticsConsent = "granted" | "denied"; type Gtag = (...args: unknown[]) => void; declare global { interface Window { dataLayer?: IArguments[]; "ga-disable-G-QTT9QT0T3S"?: boolean; gtag?: Gtag; } } function readConsent(): AnalyticsConsent | null { try { const value = localStorage.getItem(CONSENT_STORAGE_KEY); return value === "granted" || value === "denied" ? value : null; } catch { return null; } } function persistConsent(value: AnalyticsConsent): boolean { try { localStorage.setItem(CONSENT_STORAGE_KEY, value); return true; } catch { return false; } } function getGtag(): Gtag { window.dataLayer ??= []; const dataLayer = window.dataLayer; if (!window.gtag) { window.gtag = function gtag() { // biome-ignore lint/complexity/noArguments: The official gtag bootstrap requires an IArguments entry, not an Array. dataLayer.push(arguments); }; } return window.gtag; } function removeAnalyticsLoader() { document.getElementById("google-analytics-script")?.remove(); } function writeCookie(value: string) { // biome-ignore lint/suspicious/noDocumentCookie: Cookie Store is not universal and domain-scoped GA cookies must also be expired. document.cookie = value; } function expireAnalyticsCookies() { const cookieNames = new Set( document.cookie .split(";") .map((cookie) => cookie.split("=")[0]?.trim()) .filter( (name): name is string => name === "_ga" || Boolean(name?.startsWith("_ga_")), ), ); const { hostname } = window.location; const domains = hostname === SITE_DOMAIN || hostname.endsWith(`.${SITE_DOMAIN}`) ? new Set([hostname, SITE_DOMAIN]) : new Set(); for (const name of cookieNames) { const expiration = `${name}=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Path=/; SameSite=Lax`; writeCookie(expiration); for (const domain of domains) { writeCookie(`${expiration}; Domain=${domain}`); } } } const panelClassName = "fixed bottom-sp-4 left-sp-4 right-sp-4 z-[9980] ml-auto max-w-[560px] border border-[var(--slash)] bg-[var(--ink)] p-sp-5 text-white shadow-[8px_8px_0_var(--slash)] sm:left-auto"; const buttonClassName = "inline-flex min-h-11 min-w-11 items-center justify-center border px-sp-4 py-sp-2 font-mono text-[11px] font-bold uppercase tracking-[0.08em] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--slash)] focus-visible:ring-offset-2 focus-visible:ring-offset-[var(--ink)]"; function GoogleAnalyticsConsent() { const [consent, setConsent] = useState(); const [preferencesOpen, setPreferencesOpen] = useState(false); const [revokeError, setRevokeError] = useState(null); const configuredRef = useRef(false); const preferencesButtonRef = useRef(null); const enableAnalytics = useCallback(() => { const gtag = getGtag(); window[GA_DISABLE_KEY] = false; if (!configuredRef.current) { gtag("consent", "default", deniedConsent); gtag("js", new Date()); configuredRef.current = true; } gtag("consent", "update", { ...deniedConsent, analytics_storage: "granted", }); gtag("config", GA_MEASUREMENT_ID, { allow_ad_personalization_signals: false, anonymize_ip: true, }); }, []); useEffect(() => { const storedConsent = readConsent(); if (storedConsent === "granted") enableAnalytics(); setConsent(storedConsent); }, [enableAnalytics]); useEffect(() => { if (consent !== "granted" && consent !== undefined) { removeAnalyticsLoader(); } }, [consent]); function closePreferences() { setPreferencesOpen(false); preferencesButtonRef.current?.focus(); } function acceptAnalytics() { if (!persistConsent("granted")) return; enableAnalytics(); setConsent("granted"); closePreferences(); } function denyAnalytics() { persistConsent("denied"); setConsent("denied"); closePreferences(); } function revokeAnalytics() { if (!persistConsent("denied")) { setRevokeError( "Não foi possível salvar a revogação. Tente novamente para concluir.", ); return; } setRevokeError(null); window[GA_DISABLE_KEY] = true; window.gtag?.("consent", "update", deniedConsent); expireAnalyticsCookies(); setConsent("denied"); closePreferences(); } if (consent === undefined) return null; if (consent === null) { return (
Ler política de privacidade
); } return ( <> {consent === "granted" && (