(() => { try { window.analyzify = window.analyzify || {}; window.analyzify.consent_version = "2.2"; // Initialize consent flags window.analyzify.consent_flag = window.analyzify.consent_flag || { default: false, update: false, initial: false }; window.analyzify.consent_flag.initial = true; analyzify.log(`consent_active: ${window.analyzify.consent_active}`, 'an_consent', 'consentActive'); analyzify.log(`gcm_active: ${window.analyzify.gcm_active}`, 'an_consent', 'gcmActive'); analyzify.log(`cookiebot_active: ${window.analyzify.cookiebot_active}`, 'an_consent', 'cookiebotActive'); const analyzifyDebugLogger = (status) => { const shCustomerPrivacy = Shopify?.customerPrivacy; if (!shCustomerPrivacy) { console.log('[Analyzify Debug] Shopify.customerPrivacy not found.'); return; } console.group(`%c[Analyzify Debug] Shopify.customerPrivacy State: ${status.toUpperCase()}`, 'background-color: #f0f0f0; color: #333; font-weight: bold; padding: 2px 5px; border-radius: 3px;'); console.log(`Timestamp: ${new Date().toISOString()}`); for (const key in shCustomerPrivacy) { if (Object.prototype.hasOwnProperty.call(shCustomerPrivacy, key)) { const property = shCustomerPrivacy[key]; const type = typeof property; let value; try { if (type === 'function') { value = property(); } console.log(`${key} (${type}):`, value); } catch (e) { console.log(`${key} (${type}): Error getting value -> ${e.message}`); } } } console.groupEnd(); }; if (window.analyzify.consent_active) { window.dataLayer = window.dataLayer || []; function gtag() { dataLayer.push(arguments); } const ConsentManager = { isInitialized: false, consentReady: false, consentQueue: [], queueConsentAction: function (callback) { if (this.consentReady) { callback(window.analyzify.current_consent); } else { this.consentQueue.push(callback); } }, processConsentQueue: function () { if (!this.consentReady) this.consentReady = true; while (this.consentQueue.length > 0) { const callback = this.consentQueue.shift(); try { callback(window.analyzify.current_consent); } catch (error) { console.error('Error processing queued consent action:', error); } } }, getRegionalDefault: function (callback) { try { if (Shopify.customerPrivacy.userCanBeTracked() === true) { return callback({ ad_storage: 'granted', analytics_storage: 'granted', ad_user_data: 'granted', ad_personalization: 'granted', personalization_storage: 'granted', functionality_storage: 'granted', security_storage: 'granted', sale_of_data: 'denied' }); } else { return callback({ ad_storage: 'denied', analytics_storage: 'denied', ad_user_data: 'denied', ad_personalization: 'denied', personalization_storage: 'denied', functionality_storage: 'granted', security_storage: 'granted', sale_of_data: 'denied' }); } } catch (e) { return callback(null); } }, getConsentPrefs: function (callback) { try { const visitorConsentObj = Shopify.customerPrivacy.currentVisitorConsent(); const currentConsent = { ad_storage: visitorConsentObj.marketing === 'yes' ? 'granted' : 'denied', analytics_storage: visitorConsentObj.analytics === 'yes' ? 'granted' : 'denied', ad_user_data: visitorConsentObj.marketing === 'yes' ? 'granted' : 'denied', ad_personalization: visitorConsentObj.marketing === 'yes' ? 'granted' : 'denied', personalization_storage: visitorConsentObj.preferences === 'yes' ? 'granted' : 'denied', functionality_storage: 'granted', security_storage: 'granted', sale_of_data: visitorConsentObj.sale_of_data === 'yes' ? 'granted' : 'denied', }; analyzify.log("Consent preferences retrieved.", 'an_consent', 'getConsentPrefs'); return callback(currentConsent); } catch (error) { console.error('Error getting consent:', error); return callback(null); } }, dispatchUpdate: function (consent, status) { document.dispatchEvent(new CustomEvent('AnalyzifyConsent', { detail: { consent, status } })); analyzify.log("Consent updated.", 'an_consent', 'dispatchUpdate'); }, init: function () { if (this.isInitialized) return; this.isInitialized = true; const initialize = () => { this.getRegionalDefault((consent) => { if (consent && !this.consentReady) { window.analyzify.current_consent = consent; this.dispatchUpdate(consent, 'default'); this.processConsentQueue(); } }); }; const waitForApi = (callback, retries = 0) => { if (Shopify?.customerPrivacy?.userCanBeTracked !== undefined) { callback(); analyzify.log("Shopify Customer Privacy API available.", 'an_consent', 'waitForApi'); } else if (retries < 20) { setTimeout(() => waitForApi(callback, retries + 1), 100); analyzify.log("Shopify Customer Privacy API not available. Retrying...", 'an_consent', 'waitForApi'); } else { analyzify.log("Shopify Customer Privacy API not available.", 'an_consent', 'waitForApi'); } }; waitForApi(initialize); document.addEventListener("visitorConsentCollected", () => { this.getConsentPrefs((consent) => { if (consent) { window.analyzify.current_consent = consent; this.dispatchUpdate(consent, 'update'); } }); }); }, onChange: function (callback) { document.addEventListener('AnalyzifyConsent', (e) => { if (e.detail?.consent) callback(e.detail.consent); analyzify.log("AnalyzifyConsent event received.", 'an_consent', 'onChange'); }); } }; ConsentManager.init(); window.analyzify.consentManager = ConsentManager; window.AnalyzifyConsentInit = (status) => { if (window.analyzify.consent_flag[status] === true) return; window.analyzify.consentManager.queueConsentAction((consent) => { if (consent) ConsentManager.dispatchUpdate(consent, status); analyzify.log("AnalyzifyConsent event received.", 'an_consent', 'AnalyzifyConsentInit'); }); }; document.addEventListener('AnalyzifyConsent', (e) => { if (e.detail && e.detail.status && e.detail.consent) { e.detail.consent.analyzify_consent = ( e.detail.consent?.ad_storage === "granted" && e.detail.consent?.analytics_storage === "granted" ) ? "granted" : "denied"; if (window.analyzify && analyzify.logging) { console.group('Analyzify Consent'); console.log('e.detail.consent', e.detail.consent); console.log('e.detail.status', e.detail.status); console.groupEnd(); } if (window.analyzify && analyzify.gcm_active) { gtag("consent", e.detail.status, e.detail.consent); gtag("event", "consentUpdate", e.detail.consent); dataLayer.push({ event: 'consentUpdate', status: e.detail.status, consentSettings: e.detail.consent }); analyzify.log("Consent updated (gcm_active)", 'an_consent', 'AnalyzifyConsent'); } analyzify.log("AnalyzifyConsent event received.", 'an_consent', 'AnalyzifyConsent'); } }); if (window.analyzify.cookiebot_active) { /** * Loads Shopify's consent tracking API to ensure it's available. */ const loadFeatures = () => { try { window.Shopify.loadFeatures( [{ name: "consent-tracking-api", version: "0.1" }], (error) => { if (error) { console.error('Error loading Shopify features:', error); throw error; } analyzify.log('Shopify consent-tracking-api loaded.', 'an_consent', 'loadFeatures'); } ); } catch (error) { console.error('Error initializing Shopify features:', error); } }; if (window.Shopify?.loadFeatures) { loadFeatures(); } /** * Handles the Cookiebot consent ready event and sets Shopify's tracking consent. * https://www.cookiebot.com/en/developer/ */ window.addEventListener("CookiebotOnConsentReady", () => { try { analyzify.log('CookiebotOnConsentReady triggered', 'an_consent', 'cookiebotOnConsentReady'); const C = window.Cookiebot.consent; if (C) { const existConsentShopify = setInterval(() => { if (window.Shopify && window.Shopify.customerPrivacy) { clearInterval(existConsentShopify); window.Shopify.customerPrivacy.setTrackingConsent( { analytics: C?.statistics || false, marketing: C?.marketing || false, preferences: C?.preferences || false, sale_of_data: C?.marketing || false, }, () => { analyzify.log("Consent captured from Cookiebot and set in Shopify.", 'an_consent', 'cookiebotOnConsentReady'); window.analyzify.current_consent = { ad_storage: C?.marketing ? "granted" : "denied", analytics_storage: C?.statistics ? "granted" : "denied", ad_user_data: C?.marketing ? "granted" : "denied", ad_personalization: C?.marketing ? "granted" : "denied", personalization_storage: C?.preferences ? "granted" : "denied", // Correctly mapped from preferences functionality_storage: "granted", wait_for_update: 500, }; // The AnalyzifyConsent event will be dispatched automatically // by the "visitorConsentCollected" listener that ConsentManager set up. } ); } }, 100); } } catch (error) { console.error('Error handling Cookiebot consent:', error); } }); } } } catch (error) { console.error('Error initializing consent:', error); } })();