From 6cf47dc7eef9d006b95b4d9f80d30cd28a7c4c8e Mon Sep 17 00:00:00 2001 From: hobbes7878 Date: Wed, 17 Sep 2025 17:14:07 +0100 Subject: [PATCH 1/4] improves Google Tag Manager implementation --- src/components/Analytics/Analytics.svelte | 25 +++---------------- src/components/Analytics/GTM.svelte | 30 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 22 deletions(-) create mode 100644 src/components/Analytics/GTM.svelte diff --git a/src/components/Analytics/Analytics.svelte b/src/components/Analytics/Analytics.svelte index 75afbe74..1a2cfcb5 100644 --- a/src/components/Analytics/Analytics.svelte +++ b/src/components/Analytics/Analytics.svelte @@ -1,10 +1,5 @@ - - - - {@html `<${'script'}>${GTM_SCRIPT}`} - - - - - - + diff --git a/src/components/Analytics/GTM.svelte b/src/components/Analytics/GTM.svelte new file mode 100644 index 00000000..a00fdcbe --- /dev/null +++ b/src/components/Analytics/GTM.svelte @@ -0,0 +1,30 @@ + + + + + + + + + + + + + From 1402aacede1addd2a3e3d83ccbe5259d255ad210 Mon Sep 17 00:00:00 2001 From: hobbes7878 Date: Wed, 17 Sep 2025 17:16:50 +0100 Subject: [PATCH 2/4] docs(changeset): Fix for GTM tags --- .changeset/crazy-birds-report.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/crazy-birds-report.md diff --git a/.changeset/crazy-birds-report.md b/.changeset/crazy-birds-report.md new file mode 100644 index 00000000..61eff273 --- /dev/null +++ b/.changeset/crazy-birds-report.md @@ -0,0 +1,5 @@ +--- +'@reuters-graphics/graphics-components': patch +--- + +Fix for GTM tags From d600af5e919a87d11473428988a8f7a1c53bac04 Mon Sep 17 00:00:00 2001 From: hobbes7878 Date: Wed, 17 Sep 2025 17:52:20 +0100 Subject: [PATCH 3/4] 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, }); }; From 6b9cb80f33ddcc7432e421cc2f73261d0e97cf6a Mon Sep 17 00:00:00 2001 From: hobbes7878 Date: Thu, 18 Sep 2025 09:04:38 +0100 Subject: [PATCH 4/4] cleanup --- src/components/Analytics/providers/ga.ts | 32 ++++++++++++++++++------ 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/components/Analytics/providers/ga.ts b/src/components/Analytics/providers/ga.ts index b32ace4e..159f1d55 100644 --- a/src/components/Analytics/providers/ga.ts +++ b/src/components/Analytics/providers/ga.ts @@ -1,13 +1,31 @@ +// Reuters Google Tag ID +const GOOGLE_TAG_ID = 'G-WBSR7WLTGD'; + export default () => { - registerPageview(); + 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}`); + } }; export const registerPageview = () => { - if (typeof window === 'undefined' || !window.dataLayer) return; - - window.dataLayer.push({ - event: 'page_view', - page_location: window.location.href, - page_title: document.title, + if (typeof window === 'undefined' || !window.gtag) return; + window.gtag('event', 'page_view', { + page_location: window.location.origin + window.location.pathname, + page_title: document?.title, }); };