Repository navigation
Expand file tree
/
Copy pathgatsby-browser.js
More file actions
60 lines (52 loc) · 1.68 KB
/
Copy pathgatsby-browser.js
File metadata and controls
60 lines (52 loc) · 1.68 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
60
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 STORAGE_KEY = "cookie_consent";
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();
if (window.gtmDidInit) {
event.currentTarget.removeEventListener(event.type, initGTMOnEvent);
}
}
function hasGTMConsent() {
return localStorage.getItem(STORAGE_KEY) === "accepted";
}
function initGTM() {
if (!hasGTMConsent()) {
return false;
}
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);
}
window.addEventListener("cookie-consent-changed", (event) => {
if (event.detail === "accepted") {
initGTM();
}
});
export { wrapRootElement } from "./root-wrapper";
export { wrapPageElement } from "./page-wrapper";