|
| 1 | +/** |
| 2 | + * Usercentrics Cookie Consent Management |
| 3 | + * |
| 4 | + * Handles Cookie Monster consent events and Google Consent Mode v2 integration. |
| 5 | + * |
| 6 | + * Note: Usercentrics and Cookie Monster are loaded by Adobe Launch. |
| 7 | + * This script only sets up event listeners to respond to consent changes. |
| 8 | + * |
| 9 | + * Adobe Launch loads: |
| 10 | + * - Usercentrics banner (settings ID: xUbnOnjLm) |
| 11 | + * - Cookie Monster script |
| 12 | + * - Environment-specific configurations |
| 13 | + * |
| 14 | + * @version 2.0.0 - Production optimized |
| 15 | + */ |
| 16 | + |
| 17 | +window.UserCentrics = { |
| 18 | + /** |
| 19 | + * Initialize Cookie Monster listeners |
| 20 | + */ |
| 21 | + init() { |
| 22 | + // Ensure dataLayer exists |
| 23 | + window.dataLayer = window.dataLayer || []; |
| 24 | + |
| 25 | + // Create gtag function for Google Consent Mode v2 |
| 26 | + this.createGtagFunction(); |
| 27 | + |
| 28 | + // Wait for Cookie Monster to be available (loaded by Adobe Launch) |
| 29 | + this.waitForCookieMonster(); |
| 30 | + }, |
| 31 | + |
| 32 | + /** |
| 33 | + * Wait for Cookie Monster to load (from Adobe Launch) |
| 34 | + */ |
| 35 | + waitForCookieMonster() { |
| 36 | + const self = this; |
| 37 | + const maxAttempts = 50; // 5 seconds max |
| 38 | + let attempts = 0; |
| 39 | + |
| 40 | + const checkCookieMonster = setInterval(() => { |
| 41 | + attempts++; |
| 42 | + |
| 43 | + if (window.cookieMonster) { |
| 44 | + clearInterval(checkCookieMonster); |
| 45 | + self.setupCookieListeners(); |
| 46 | + self.restoreSavedConsent(); |
| 47 | + self.dispatchReadyEvent(); |
| 48 | + } else if (attempts >= maxAttempts) { |
| 49 | + clearInterval(checkCookieMonster); |
| 50 | + } |
| 51 | + }, 100); |
| 52 | + }, |
| 53 | + |
| 54 | + /** |
| 55 | + * Create gtag function for Google Consent Mode v2 communication |
| 56 | + */ |
| 57 | + createGtagFunction() { |
| 58 | + window.dataLayer = window.dataLayer || []; |
| 59 | + if (typeof window.gtag !== 'function') { |
| 60 | + window.gtag = function () { |
| 61 | + window.dataLayer.push(arguments); |
| 62 | + }; |
| 63 | + } |
| 64 | + }, |
| 65 | + |
| 66 | + /** |
| 67 | + * Set up Usercentrics consent change listeners |
| 68 | + * Listens to UC_UI_CMP_EVENT for all consent changes (accept/reject/save) |
| 69 | + */ |
| 70 | + setupCookieListeners() { |
| 71 | + const self = this; |
| 72 | + |
| 73 | + // Listen for all Usercentrics consent events |
| 74 | + window.addEventListener('UC_UI_CMP_EVENT', (event) => { |
| 75 | + if (event.detail) { |
| 76 | + const { type } = event.detail; |
| 77 | + |
| 78 | + // Handle all consent-changing events |
| 79 | + if ( |
| 80 | + type === 'ACCEPT_ALL' || |
| 81 | + type === 'DENY_ALL' || |
| 82 | + type === 'SAVE' |
| 83 | + ) { |
| 84 | + self.updateAllConsentStates(); |
| 85 | + } |
| 86 | + } |
| 87 | + }); |
| 88 | + }, |
| 89 | + |
| 90 | + /** |
| 91 | + * Restore saved consent preferences on page load |
| 92 | + * Uses polling to wait for Cookie Monster to load consent data |
| 93 | + */ |
| 94 | + restoreSavedConsent() { |
| 95 | + const self = this; |
| 96 | + |
| 97 | + // Check if user has previously saved consent |
| 98 | + if (window.cookieMonster.hasInteracted()) { |
| 99 | + // Poll until Cookie Monster has loaded the consent data |
| 100 | + let attempts = 0; |
| 101 | + const maxAttempts = 10; // 1 second max |
| 102 | + |
| 103 | + const checkReady = setInterval(() => { |
| 104 | + attempts++; |
| 105 | + |
| 106 | + // Test if data is loaded by checking if required cookies return true |
| 107 | + const dataLoaded = |
| 108 | + window.cookieMonster.permitted('reqd') === true; |
| 109 | + |
| 110 | + if (dataLoaded || attempts >= maxAttempts) { |
| 111 | + clearInterval(checkReady); |
| 112 | + self.updateAllConsentStates(); |
| 113 | + } |
| 114 | + }, 100); |
| 115 | + } |
| 116 | + }, |
| 117 | + |
| 118 | + /** |
| 119 | + * Update all consent states in GTM based on current Cookie Monster permissions |
| 120 | + */ |
| 121 | + updateAllConsentStates() { |
| 122 | + // Check Targeting cookies |
| 123 | + const targPermitted = window.cookieMonster.permitted('targ'); |
| 124 | + gtag('consent', 'update', { |
| 125 | + ad_storage: targPermitted ? 'granted' : 'denied', |
| 126 | + ad_user_data: targPermitted ? 'granted' : 'denied', |
| 127 | + ad_personalization: targPermitted ? 'granted' : 'denied', |
| 128 | + personalization_storage: targPermitted ? 'granted' : 'denied', |
| 129 | + }); |
| 130 | + |
| 131 | + // Check Performance cookies |
| 132 | + const perfPermitted = window.cookieMonster.permitted('perf'); |
| 133 | + gtag('consent', 'update', { |
| 134 | + analytics_storage: perfPermitted ? 'granted' : 'denied', |
| 135 | + }); |
| 136 | + |
| 137 | + // Check Functional cookies |
| 138 | + const fnctPermitted = window.cookieMonster.permitted('fnct'); |
| 139 | + gtag('consent', 'update', { |
| 140 | + functionality_storage: fnctPermitted ? 'granted' : 'denied', |
| 141 | + }); |
| 142 | + |
| 143 | + // Push custom dataLayer event for GTM triggers |
| 144 | + // This allows GTM to fire tags conditionally based on consent state |
| 145 | + window.dataLayer.push({ |
| 146 | + event: 'consent_update', |
| 147 | + consent_state: { |
| 148 | + ad_storage: targPermitted ? 'granted' : 'denied', |
| 149 | + ad_user_data: targPermitted ? 'granted' : 'denied', |
| 150 | + ad_personalization: targPermitted ? 'granted' : 'denied', |
| 151 | + personalization_storage: targPermitted ? 'granted' : 'denied', |
| 152 | + analytics_storage: perfPermitted ? 'granted' : 'denied', |
| 153 | + functionality_storage: fnctPermitted ? 'granted' : 'denied', |
| 154 | + }, |
| 155 | + }); |
| 156 | + }, |
| 157 | + |
| 158 | + /** |
| 159 | + * Dispatch custom event when Cookie Monster is ready |
| 160 | + */ |
| 161 | + dispatchReadyEvent() { |
| 162 | + window.cookieMonsterReady = true; |
| 163 | + |
| 164 | + if (typeof CustomEvent !== 'undefined') { |
| 165 | + const event = new CustomEvent('cookieMonsterReady', { |
| 166 | + detail: { |
| 167 | + version: window.cookieMonster.VERSION || 'unknown', |
| 168 | + engine: window.cookieMonster.ENGINE || 'unknown', |
| 169 | + }, |
| 170 | + }); |
| 171 | + document.dispatchEvent(event); |
| 172 | + } |
| 173 | + }, |
| 174 | +}; |
| 175 | + |
| 176 | +/** |
| 177 | + * Auto-initialize when DOM is ready |
| 178 | + */ |
| 179 | +if (document.readyState === 'loading') { |
| 180 | + document.addEventListener('DOMContentLoaded', () => UserCentrics.init()); |
| 181 | +} else { |
| 182 | + // DOM already loaded |
| 183 | + UserCentrics.init(); |
| 184 | +} |
0 commit comments