From 7784914dfbc439009a4a5be8465cafb6e57078ad Mon Sep 17 00:00:00 2001 From: Florian Sommariva Date: Wed, 18 Oct 2023 10:36:24 +0200 Subject: [PATCH 1/2] Fix dynamic acceptance/refusal of GA cookies --- docs/customization-scripts-GDPR.md | 17 ++++++++++------- .../components/Layout/useExternalScripts.tsx | 16 +++++++++------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/docs/customization-scripts-GDPR.md b/docs/customization-scripts-GDPR.md index 204522f43..ea85a7dbd 100644 --- a/docs/customization-scripts-GDPR.md +++ b/docs/customization-scripts-GDPR.md @@ -111,14 +111,17 @@ To avoid this, one solution is to check the user's acceptance in the cookies. > // Send visit on each dynamic page change window.next.router.events.on('routeChangeComplete', function(url) { ++ (function(allowsGTMCookies) { ++ // Tell Google if it can use the service ++ window['ga-disable-'] = !allowsGTMCookies; ++ if (allowsGTMCookies) { + window.dataLayer.push({ + event: "pageview", + page: url, + }) ++ } + // Check the user's acceptance of "google-tag-manager" in the cookies -+ if (JSON.parse(new URLSearchParams(document.cookie.replaceAll('; ', '&')).get('orejime') ?? null)?.["google-tag-manager"] !== true) { -+ return; -+ } - window.dataLayer.push({ - event: "pageview", - page: url, - }) ++ })(JSON.parse(new URLSearchParams(document.cookie.replaceAll('; ', '&')).get('orejime') ?? null)?.["google-tag-manager"]); }); ``` diff --git a/frontend/src/components/Layout/useExternalScripts.tsx b/frontend/src/components/Layout/useExternalScripts.tsx index 22b0316a9..1c89fcfec 100644 --- a/frontend/src/components/Layout/useExternalScripts.tsx +++ b/frontend/src/components/Layout/useExternalScripts.tsx @@ -77,13 +77,15 @@ export const useExternalsScripts = (executeOnLoad = false) => { gtag('config', '${googleAnalyticsId}'); window.next.router.events.on('routeChangeComplete', function(url) { - if (JSON.parse(new URLSearchParams(document.cookie.replaceAll('; ', '&')).get('orejime') ?? null)?.["google-tag-manager"] !== true) { - return; - } - window.dataLayer.push({ - event: "pageview", - page: url, - }) + (function(allowsGTMCookies) { + window['ga-disable-${googleAnalyticsId}'] = !allowsGTMCookies; + if (allowsGTMCookies) { + window.dataLayer.push({ + event: "pageview", + page: url, + }) + } + })(JSON.parse(new URLSearchParams(document.cookie.replaceAll('; ', '&')).get('orejime') ?? null)?.["google-tag-manager"]); }); ` From c0241bba16c4242043a63b5d2e8c1ba17667dcc7 Mon Sep 17 00:00:00 2001 From: Florian Sommariva Date: Wed, 18 Oct 2023 10:41:27 +0200 Subject: [PATCH 2/2] Show only the GDPR notice is the user has not yet given explicit consent --- frontend/src/components/Layout/useExternalScripts.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/components/Layout/useExternalScripts.tsx b/frontend/src/components/Layout/useExternalScripts.tsx index 1c89fcfec..58729be42 100644 --- a/frontend/src/components/Layout/useExternalScripts.tsx +++ b/frontend/src/components/Layout/useExternalScripts.tsx @@ -116,7 +116,7 @@ export const useExternalsScripts = (executeOnLoad = false) => { // @ts-ignore the lib is not typed const Orejime = await import('orejime'); const { show } = Orejime.init(orejimeConfig(consentList, locale, privacyPolicyLink)); - if (!executeOnLoad || !document.cookie.includes('orejime=')) { + if (!executeOnLoad) { show(); } }