From 6764a0e220d6445e238194ebe3921508a1d37350 Mon Sep 17 00:00:00 2001 From: Matt Eason Date: Tue, 21 Nov 2023 16:59:41 +0000 Subject: [PATCH] Fix error when declining analytics cookies --- .../public/assets/scripts/cookieControl.js | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/resources/public/assets/scripts/cookieControl.js b/src/main/resources/public/assets/scripts/cookieControl.js index 38132b9d..0aa6b7b6 100644 --- a/src/main/resources/public/assets/scripts/cookieControl.js +++ b/src/main/resources/public/assets/scripts/cookieControl.js @@ -33,15 +33,21 @@ denyConsentScripts: [ // Google Analytics function() { - function gtag(){dataLayer.push(arguments);} + // dataLayer will only exist on window if they've already consented to analytics. If that's the case, we update + // consent preferences in the Google tag to ensure cookies aren't recreated + if(window.hasOwnProperty("dataLayer")) { + function gtag() { + dataLayer.push(arguments); + } - gtag("consent", "default", { - ad_storage: "denied", - analytics_storage: "denied", - functionality_storage: "denied", - personalization_storage: "denied", - security_storage: "denied" - }); + gtag("consent", "default", { + ad_storage: "denied", + analytics_storage: "denied", + functionality_storage: "denied", + personalization_storage: "denied", + security_storage: "denied" + }); + } } ], setCookie: function (name,value,days) {