// site/analytics.jsx — thin wrapper around gtag() for CTA tracking. // Usage: trackEvent('cta_hero_primary', { section: 'hero', lang: 'it' }) // Events are sent only if the GA snippet loaded; if consent is "denied", // Consent Mode v2 buffers them server-side without storing _ga cookies. function trackEvent(eventName, params) { try { if (typeof window.gtag === 'function') { // Auto-attach current language so GA4 can segment IT vs EN without // requiring every call site to pass it explicitly. const lang = (document.documentElement.lang || 'it').toLowerCase().slice(0, 2); const base = { lang: lang }; // Auto-attach active experiment variants (none on EN; sticky on IT). if (window.__experiment_hero_v1) { base.experiment_hero_v1 = window.__experiment_hero_v1; } window.gtag('event', eventName, Object.assign(base, params || {})); } } catch (e) { /* swallow — analytics must never break the page */ } } // Called by the consent banner when the user accepts "all". function grantAnalyticsConsent() { try { if (typeof window.gtag === 'function') { window.gtag('consent', 'update', { 'analytics_storage': 'granted' }); } } catch (e) {} } Object.assign(window, { trackEvent, grantAnalyticsConsent });