From d600af5e919a87d11473428988a8f7a1c53bac04 Mon Sep 17 00:00:00 2001 From: hobbes7878 Date: Wed, 17 Sep 2025 17:52:20 +0100 Subject: [PATCH] use GTM dataLayer to register pageviews --- src/components/Analytics/providers/ga.ts | 32 ++++++------------------ 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/src/components/Analytics/providers/ga.ts b/src/components/Analytics/providers/ga.ts index 159f1d55..b32ace4e 100644 --- a/src/components/Analytics/providers/ga.ts +++ b/src/components/Analytics/providers/ga.ts @@ -1,31 +1,13 @@ -// Reuters Google Tag ID -const GOOGLE_TAG_ID = 'G-WBSR7WLTGD'; - export default () => { - try { - window.dataLayer = window.dataLayer || []; - if (!window.gtag) { - /** @type {Gtag.Gtag} */ - window.gtag = function () { - // eslint-disable-next-line prefer-rest-params - window.dataLayer.push(arguments); - }; - window.gtag('js', new Date()); - // config event registers a pageview by default - window.gtag('config', GOOGLE_TAG_ID, { - send_page_view: false, - }); - registerPageview(); - } - } catch (e) { - console.warn(`Error initialising Google Analytics: ${e}`); - } + registerPageview(); }; export const registerPageview = () => { - if (typeof window === 'undefined' || !window.gtag) return; - window.gtag('event', 'page_view', { - page_location: window.location.origin + window.location.pathname, - page_title: document?.title, + if (typeof window === 'undefined' || !window.dataLayer) return; + + window.dataLayer.push({ + event: 'page_view', + page_location: window.location.href, + page_title: document.title, }); };