-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
gatsby-browser.js
32 lines (30 loc) · 1.21 KB
/
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
import "./fonts.css";
document.addEventListener("DOMContentLoaded", () => {
/** init gtm after 3500 seconds - this could be adjusted */
setTimeout(initGTM, 3500);
});
document.addEventListener("scroll", initGTMOnEvent);
document.addEventListener("mousemove", initGTMOnEvent);
document.addEventListener("touchstart", initGTMOnEvent);
function initGTMOnEvent(event) {
initGTM();
event.currentTarget.removeEventListener(event.type, initGTMOnEvent); // remove the event listener that got triggered
}
function initGTM() {
if (window.gtmDidInit) {
return false;
}
window.gtmDidInit = true; // flag to ensure script does not get added to DOM more than once.
const script = document.createElement("script");
script.type = "text/javascript";
script.async = true;
// ensure PageViews is always tracked (on script load)
script.onload = () => {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({ event: "gtm.js", "gtm.start": new Date().getTime(), "gtm.uniqueEventId": 0 });
};
script.src = "https://www.googletagmanager.com/gtm.js?id=GTM-PS26QB9";
document.head.appendChild(script);
}
export { wrapRootElement } from "./root-wrapper";
export { wrapPageElement } from "./page-wrapper";