From 527698544ca5416fc40e56d5574ea1febfc34fbb Mon Sep 17 00:00:00 2001 From: Arvid Norlin Date: Mon, 4 Mar 2024 08:08:22 +0100 Subject: [PATCH] fix: add checks to tracking consent --- app/[lang]/(live)/layout.tsx | 3 +++ app/[lang]/(live-current)/layout.tsx | 7 +++-- components/Current/Tracking.tsx | 39 ++++++++++++++++++++++++---- types/window.d.ts | 10 +++++++ 4 files changed, 50 insertions(+), 9 deletions(-) diff --git a/app/[lang]/(live)/layout.tsx b/app/[lang]/(live)/layout.tsx index 6f898423d..5ff5a71ed 100644 --- a/app/[lang]/(live)/layout.tsx +++ b/app/[lang]/(live)/layout.tsx @@ -28,6 +28,9 @@ export default function RootLayout({ id="Cookiebot" src="https://consent.cookiebot.com/uc.js" /> + diff --git a/app/[lang]/(live-current)/layout.tsx b/app/[lang]/(live-current)/layout.tsx index 8d2611f3b..2ec0ef18d 100644 --- a/app/[lang]/(live-current)/layout.tsx +++ b/app/[lang]/(live-current)/layout.tsx @@ -64,10 +64,9 @@ export default function RootLayout({ src="/_static/dist/js/main-ng.js?336b801d6b38eff10884" strategy="lazyOnload" /> */} - + diff --git a/components/Current/Tracking.tsx b/components/Current/Tracking.tsx index 074c7e203..ab740f6d3 100644 --- a/components/Current/Tracking.tsx +++ b/components/Current/Tracking.tsx @@ -42,8 +42,8 @@ function createPageObject(trackingData: TrackingData) { const sitesections = getSiteSections(segments) const { host: domain, href: fullurl, origin } = window.location const page_obj = { - pagename: segments.join('|'), - pagetype: 'contentpage', + pagename: segments.join("|"), + pagetype: "contentpage", pageurl: origin + trackingData.pathName, fullurl, createDate: trackingData.createdDate, @@ -66,11 +66,40 @@ export default function Tracking({ pageData }: TrackingProps) { const queryString = useSearchParams().toString() useEffect(() => { - const trackingData = { ...pageData, pathName, queryString } - const pageObject = createPageObject(trackingData) + if (window.datalayer) { + const trackingData = { ...pageData, pathName, queryString } + const pageObject = createPageObject(trackingData) - window.datalayer.page = pageObject + window.datalayer.page = pageObject + } }, [pathName, queryString, pageData]) + useEffect(() => { + // handle consent + window.addEventListener( + "CookiebotOnAccept", + function CookiebotCallback_OnAccept(e) { + if (window.Cookiebot?.changed && window.adobe) { + if ( + window._satellite.cookie + .get("CookieConsent") + .includes("statistics:true") + ) { + window.adobe.optIn.approve( + window.adobe.OptInCategories.ANALYTICS, + true + ) + } else { + window.adobe.optIn.deny( + window.adobe.OptInCategories.ANALYTICS, + true + ) + } + window.adobe.optIn.complete() + } + } + ) + }, []) + return null } diff --git a/types/window.d.ts b/types/window.d.ts index 8e2462b1f..86fde9b5c 100644 --- a/types/window.d.ts +++ b/types/window.d.ts @@ -1,3 +1,13 @@ interface Window { datalayer: { [key: string]: any } + _satellite: { cookie: { get: (s: string) => string } } + adobe: { + OptInCategories: { ANALYTICS: string } + optIn: { + approve: (s: string, b: boolean) => {} + deny: (s: string, b: boolean) => {} + complete: () => {} + } + } + Cookiebot: { changed: boolean; consented: boolean } }