'use client'; import { useEffect } from 'react'; import { useConsentContext } from '../consent/client.js'; import { gtag } from '../consent/googleConsent.js'; import { CONSENT_COOKIE, DEFAULT_CONSENT, parseConsent, setDefaultConsent } from '../consent/index.js'; import { loadClarity, loadLinkedIn, loadMetaPixel, loadTikTok } from './pixels.js'; const GTM_SCRIPT = (id)=>`https://www.googletagmanager.com/gtm.js?id=${id}`; const GA4_SCRIPT = (id)=>`https://www.googletagmanager.com/gtag/js?id=${id}`; /** Reads the current consent state from cookie, or defaults if none. */ function readConsent() { if (typeof document === 'undefined') { return DEFAULT_CONSENT; } const raw = document.cookie.split('; ').find((c)=>c.startsWith(`${CONSENT_COOKIE}=`))?.split('=')[1]; return parseConsent(raw) ?? DEFAULT_CONSENT; } function injectScript(src) { if (document.querySelector(`script[src="${src}"]`)) { return; } const s = document.createElement('script'); s.src = src; s.async = true; document.head.appendChild(s); } /** * Loads every configured analytics / marketing tag, each respecting consent: * * - **Google (GA4 / GTM)** — loaded immediately with Consent Mode: the visitor's * stored choice is set as the default BEFORE the tag loads, and useConsent * pushes an update the moment they decide. GTM wins if both IDs are set. * - **Non-Google (Meta Pixel, Clarity, LinkedIn, TikTok)** — these vendors have * no Consent Mode, so they load ONLY after the matching category is granted * (marketing for Pixel/LinkedIn/TikTok, analytics for Clarity). Reacts live to * the consent banner: accept → the tag loads immediately, no reload. * * All IDs are public and come from SiteIntegrations (getAnalyticsConfig), passed * in as props. Renders nothing. Mount once, high in the tree, INSIDE * (required — it reads live consent state). */ export function Analytics(props) { const { clarityProjectId, ga4MeasurementId, gtmContainerId, linkedInPartnerId, metaPixelId, tiktokPixelId } = props; // Live consent state — drives the non-Google pixels. const { state } = useConsentContext(); // --- Google: load once, Consent Mode handles gating --- useEffect(()=>{ if (!ga4MeasurementId && !gtmContainerId) { return; } setDefaultConsent(readConsent()); if (gtmContainerId) { window.dataLayer = window.dataLayer || []; window.dataLayer.push({ event: 'gtm.js', 'gtm.start': Date.now() }); injectScript(GTM_SCRIPT(gtmContainerId)); } else if (ga4MeasurementId) { injectScript(GA4_SCRIPT(ga4MeasurementId)); gtag('js', new Date()); gtag('config', ga4MeasurementId); } }, [ ga4MeasurementId, gtmContainerId ]); // --- Non-Google: load each when its consent category is granted --- // The loaders are idempotent, so re-running on state changes is safe; a tag // loads the first time its category flips to true and never doubles up. useEffect(()=>{ if (state.marketing) { if (metaPixelId) { loadMetaPixel(metaPixelId); } if (linkedInPartnerId) { loadLinkedIn(linkedInPartnerId); } if (tiktokPixelId) { loadTikTok(tiktokPixelId); } } if (state.analytics && clarityProjectId) { loadClarity(clarityProjectId); } }, [ state.marketing, state.analytics, metaPixelId, linkedInPartnerId, tiktokPixelId, clarityProjectId ]); return null; } //# sourceMappingURL=Analytics.js.map