diff --git a/app/javascript/packs/baseTracking.js b/app/javascript/packs/baseTracking.js index caa54454d..2adeef299 100644 --- a/app/javascript/packs/baseTracking.js +++ b/app/javascript/packs/baseTracking.js @@ -1,4 +1,5 @@ /*eslint-disable prefer-rest-params*/ +/* global isTouchDevice */ function initializeBaseTracking() { showCookieConsentBanner(); @@ -7,11 +8,12 @@ function initializeBaseTracking() { trackCustomImpressions(); } +// Google Anlytics 3 is deprecated, and mostly not supported, but some sites may still be using it for now. function trackGoogleAnalytics3() { let wait = 0; let addedGA = false; const gaTrackingCode = document.body.dataset.gaTracking; - if (gaTrackingCode) { + if (gaTrackingCode && localStorage.getItem('cookie_status') === 'allowed') { const waitingOnGA = setInterval(() => { if (!addedGA) { (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ @@ -33,6 +35,8 @@ function trackGoogleAnalytics3() { } }, 25); eventListening(); + } else if (gaTrackingCode) { + fallbackActivityRecording(); } } @@ -60,9 +64,13 @@ function trackGoogleAnalytics4() { window['gtag'] = window['gtag'] || function () { window.dataLayer.push(arguments) } - + const consent = localStorage.getItem('cookie_status') === 'allowed' ? 'granted' : 'denied'; gtag('js', new Date()); gtag('config', ga4MeasurementCode, { 'anonymize_ip': true }); + gtag('consent', 'default', { + 'ad_storage': consent, + 'analytics_storage': consent + }); clearInterval(waitingOnGA4); } if (wait > 85) { @@ -181,7 +189,7 @@ function trackCustomImpressions() { function showCookieConsentBanner() { // if current url includes ?cookietest=true - if (window.location.href.includes('cookietest=true')) { + if (shouldShowCookieBanner()) { // show modal with cookie consent const cookieDiv = document.getElementById('cookie-consent'); @@ -190,7 +198,7 @@ function showCookieConsentBanner() {