-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathgatsby-browser.js
More file actions
59 lines (48 loc) · 1.56 KB
/
gatsby-browser.js
File metadata and controls
59 lines (48 loc) · 1.56 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import "./fonts.css";
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
export const disableCorePrefetching = () =>
process.env.NODE_ENV === "development";
const GTM_DELAY_MS = 3500;
const GTM_INIT_EVENTS = ["scroll", "mousemove", "touchstart"];
function addInitListeners() {
GTM_INIT_EVENTS.forEach((eventName) => {
document.addEventListener(eventName, initGTMOnEvent);
});
}
function removeInitListeners() {
GTM_INIT_EVENTS.forEach((eventName) => {
document.removeEventListener(eventName, initGTMOnEvent);
});
}
document.addEventListener("DOMContentLoaded", () => {
// Initialize GTM after a short delay unless user interaction triggers it first.
setTimeout(initGTM, GTM_DELAY_MS);
});
addInitListeners();
function initGTMOnEvent() {
initGTM();
}
function initGTM() {
if (window.gtmDidInit) {
return false;
}
window.gtmDidInit = true; // flag to ensure script does not get added to DOM more than once.
removeInitListeners();
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";