|
86 | 86 | } |
87 | 87 | } |
88 | 88 |
|
| 89 | + // Arm listeners and call fn on the first user interaction |
| 90 | + function onFirstInteraction(fn) { |
| 91 | + var fired = false; |
| 92 | + function go() { |
| 93 | + if (fired) return; |
| 94 | + fired = true; |
| 95 | + ['scroll', 'click', 'keydown', 'touchstart'].forEach(function (ev) { |
| 96 | + window.removeEventListener(ev, go, { passive: true }); |
| 97 | + }); |
| 98 | + fn(); |
| 99 | + } |
| 100 | + ['scroll', 'click', 'keydown', 'touchstart'].forEach(function (ev) { |
| 101 | + window.addEventListener(ev, go, { once: true, passive: true }); |
| 102 | + }); |
| 103 | + } |
| 104 | + |
89 | 105 | // Load Google Analytics (GA4 + Consent Mode v2) |
90 | 106 | function loadGoogleAnalytics() { |
91 | 107 | const gaMeasurementId = getGAMeasurementId(); |
|
144 | 160 | loadMicrosoftClarity(); |
145 | 161 | } |
146 | 162 |
|
| 163 | + // Ensure Consent Mode defaults are set, then load GA on first interaction |
| 164 | + function scheduleGoogleAnalytics() { |
| 165 | + initConsentDefaults(); |
| 166 | + onFirstInteraction(loadGoogleAnalytics); |
| 167 | + } |
| 168 | + |
147 | 169 | // Handle accept button click |
148 | 170 | function handleAccept() { |
149 | 171 | setConsentStatus(COOKIE_CONSENT_ACCEPTED); |
150 | 172 | hideCookieBanner(); |
151 | | - loadGoogleAnalytics(); |
| 173 | + scheduleGoogleAnalytics(); |
152 | 174 | } |
153 | 175 |
|
154 | 176 | // Handle decline button click |
|
189 | 211 | // No consent given yet, show banner |
190 | 212 | showCookieBanner(); |
191 | 213 | } else if (consentStatus === COOKIE_CONSENT_ACCEPTED) { |
192 | | - loadGoogleAnalytics(); |
| 214 | + scheduleGoogleAnalytics(); |
193 | 215 | } |
194 | 216 | // If declined, do nothing |
195 | 217 |
|
|
0 commit comments