/** * CSA Advisory - Advanced Consent Manager (Google v2 + Audit) * * ARCHITECTURE: * 1. Default: All Google Tags are blocked (Denied). * 2. Interaction: User accepts via Banner. * 3. Action A (Marketing): Calls gtag('update', 'granted') -> Fires GA4/Ads. * 4. Action B (Legal): Sends Audit Log to Compliance API (Port 5007). */ const CONSENT_API = "http://localhost:5007/api/v1/audit/consent"; const COOKIE_NAME = "csa_consent_v2"; const CookieManager = { init: function() { // Check LocalStorage const saved = localStorage.getItem(COOKIE_NAME); if (!saved) { this.showBanner(); } else { const consent = JSON.parse(saved); // Re-apply consent on every page load to ensure GTM catches it this.updateGoogle(consent.status); } }, showBanner: function() { if(document.getElementById('csa-cookie-banner')) return; const banner = document.createElement('div'); banner.id = 'csa-cookie-banner'; banner.className = 'fixed-bottom bg-white border-top shadow-lg p-4 animate__animated animate__slideInUp'; banner.style.zIndex = '99999'; banner.innerHTML = `
Privacy Settings

We use cookies to analyze traffic and personalize content (Google Ads/GA4). By clicking "Accept All", you consent to the use of all cookies and the transfer of data to Google. We log your consent for legal compliance. Read Policy.

`; document.body.appendChild(banner); }, acceptAll: function() { this.updateGoogle('granted'); this.saveChoice('granted'); this.logAudit('granted'); this.hideBanner(); // TRIGGER GTM EVENT: Needed if GTM is waiting for a custom event window.dataLayer.push({'event': 'consent_granted'}); }, denyAll: function() { this.updateGoogle('denied'); this.saveChoice('denied'); this.logAudit('denied'); this.hideBanner(); }, updateGoogle: function(status) { console.log(`[CSA Consent] Setting Google Signals to: ${status}`); // Google Consent Mode v2 Command function gtag(){dataLayer.push(arguments);} gtag('consent', 'update', { 'ad_storage': status, 'ad_user_data': status, 'ad_personalization': status, 'analytics_storage': status }); }, saveChoice: function(status) { const data = { status: status, timestamp: new Date().toISOString(), id: crypto.randomUUID() }; localStorage.setItem(COOKIE_NAME, JSON.stringify(data)); }, logAudit: function(status) { // Fire-and-forget audit log const saved = JSON.parse(localStorage.getItem(COOKIE_NAME)); if(!saved) return; fetch(CONSENT_API, { method: 'POST', mode: 'cors', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ consentId: saved.id, status: status, userAgent: navigator.userAgent }) }).catch(err => console.warn("Audit log unreachable (Dev Mode?)")); }, hideBanner: function() { const el = document.getElementById('csa-cookie-banner'); if(el) el.remove(); } }; document.addEventListener('DOMContentLoaded', () => { CookieManager.init(); });