-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-browser.js
42 lines (34 loc) · 1016 Bytes
/
gatsby-browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import './src/fonts/fonts.css';
const initGTM = () => {
if (window.gtmDidInit) {
return false;
}
window.gtmDidInit = true;
const script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = `https://www.googletagmanager.com/gtm.js?id=${process.env.GATSBY_GOOGLE_TAG_ID}`;
script.onload = () => {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'gtm.js',
'gtm.start': new Date().getTime(),
'gtm.uniqueEventId': 0,
});
};
document.head.appendChild(script);
};
const initGTMOnEvent = (event) => {
initGTM();
event.currentTarget.removeEventListener(event.type, initGTMOnEvent);
};
export const onClientEntry = () => {
document.onreadystatechange = () => {
if (document.readyState !== 'loading') {
setTimeout(initGTM, 1000);
}
};
['scroll', 'mousemove', 'touchstart'].forEach((eventType) => {
document.addEventListener(eventType, initGTMOnEvent);
});
};