import React, { useEffect, useState } from "react"; declare global { interface Window { dataLayer?: any[]; gtag?: (...args: any[]) => void; } } const STORAGE_KEY = "tt_cookie_consent"; function loadGtag(id: string) { if (!id) return; if (document.querySelector(`script[data-gtag="${id}"]`)) return; const script = document.createElement("script"); script.async = true; script.src = `https://www.googletagmanager.com/gtag/js?id=${id}`; script.setAttribute("data-gtag", id); document.head.appendChild(script); window.dataLayer = window.dataLayer || []; window.gtag = function () { // @ts-ignore - forward arguments to the dataLayer window.dataLayer!.push(arguments); }; window.gtag("js", new Date()); window.gtag("config", id, { anonymize_ip: true }); } const CookieConsent: React.FC = () => { const [consent, setConsent] = useState(null); useEffect(() => { const v = localStorage.getItem(STORAGE_KEY); setConsent(v); if (v === "granted") { const id = (import.meta.env as any).VITE_GA_ID as string | undefined; if (id) loadGtag(id); } }, []); function accept() { localStorage.setItem(STORAGE_KEY, "granted"); setConsent("granted"); const id = (import.meta.env as any).VITE_GA_ID as string | undefined; if (id) loadGtag(id); } function deny() { localStorage.setItem(STORAGE_KEY, "denied"); setConsent("denied"); } if (consent !== null) return null; return (
We use cookies for analytics.
); }; export default CookieConsent;