Skip to content

Commit 06b01be

Browse files
authored
docs: add analytics to banner (#3703)
## Description Adds a dedicated GTM container for banner tracking.
1 parent 83910b3 commit 06b01be

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

docs/src/components/TopPromoRotator/index.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import React, { type ReactNode, useEffect, useMemo, useState } from 'react';
44
import HandIcon from '../HandIcon';
55
import styles from './styles.module.css';
66

7+
declare global {
8+
interface Window {
9+
dataLayer?: unknown[];
10+
}
11+
}
12+
713
type Promo = {
814
key: string;
915
href: string;
@@ -50,6 +56,32 @@ export default function TopPromoRotator() {
5056

5157
const [index, setIndex] = useState(0);
5258

59+
useEffect(() => {
60+
if (typeof window === 'undefined' || typeof document === 'undefined') {
61+
return;
62+
}
63+
64+
const existingScript = document.querySelector<HTMLScriptElement>(
65+
'script[src*="www.googletagmanager.com/gtm.js?id=GTM-WV2G3SQL"]',
66+
);
67+
68+
if (existingScript) return;
69+
70+
(function (w: Window, d: Document, s: string, l: string, i: string) {
71+
w.dataLayer = w.dataLayer || [];
72+
w.dataLayer.push({
73+
'gtm.start': new Date().getTime(),
74+
event: 'gtm.js',
75+
});
76+
const f = d.getElementsByTagName(s)[0] as HTMLScriptElement;
77+
const j = d.createElement(s) as HTMLScriptElement;
78+
const dl = l !== 'dataLayer' ? `&l=${l}` : '';
79+
j.async = true;
80+
j.src = `https://www.googletagmanager.com/gtm.js?id=${i}${dl}`;
81+
f.parentNode?.insertBefore(j, f);
82+
})(window, document, 'script', 'dataLayer', 'GTM-WV2G3SQL');
83+
}, []);
84+
5385
useEffect(() => {
5486
const id = window.setInterval(() => {
5587
setIndex(i => (i + 1) % promos.length);

0 commit comments

Comments
 (0)