-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathgatsby-browser.js
More file actions
137 lines (116 loc) · 3.61 KB
/
Copy pathgatsby-browser.js
File metadata and controls
137 lines (116 loc) · 3.61 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import './src/styles/main.css';
const UTM_PARAMS = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'fbclid', 'ttclid', 'wbraid'];
const SIGNUP_HOST = 'dashboard.novu.co';
const CAL_NAMESPACE = 'novu-meeting';
let demoBookingTrackingInitialized = false;
function getStoredUtmParams() {
try {
const stored = sessionStorage.getItem('novu_utm_params');
if (!stored) return {};
const parsed = JSON.parse(stored);
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {};
return Object.fromEntries(
UTM_PARAMS.flatMap((key) => {
const value = parsed[key];
return typeof value === 'string' && value ? [[key, value]] : [];
})
);
} catch {
return {};
}
}
function captureUtmParams() {
const params = new URLSearchParams(window.location.search);
const utms = {};
UTM_PARAMS.forEach((key) => {
const value = params.get(key);
if (value) utms[key] = value;
});
if (Object.keys(utms).length > 0) {
try {
const existing = getStoredUtmParams();
const merged = { ...existing, ...utms };
sessionStorage.setItem('novu_utm_params', JSON.stringify(merged));
} catch {
// sessionStorage unavailable
}
}
}
function forwardUtmToSignupLinks() {
const utms = getStoredUtmParams();
if (Object.keys(utms).length === 0) return;
document.querySelectorAll('a[href]').forEach((link) => {
try {
const url = new URL(link.href);
if (url.hostname !== SIGNUP_HOST) return;
Object.entries(utms).forEach(([key, value]) => {
if (!url.searchParams.has(key)) {
url.searchParams.set(key, value);
}
});
link.href = url.toString();
} catch {
// skip malformed URLs
}
});
}
async function initDemoBookingTracking() {
if (demoBookingTrackingInitialized) return;
const { getCalApi } = await import('@calcom/embed-react');
const cal = await getCalApi({ namespace: CAL_NAMESPACE });
cal('on', {
action: 'bookingSuccessful',
callback: () => {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'demo_booked',
demo_booking: {
source: 'cal.com',
namespace: CAL_NAMESPACE,
...getStoredUtmParams(),
},
});
},
});
demoBookingTrackingInitialized = true;
}
export const onClientEntry = () => {
const start = () => {
initDemoBookingTracking().catch(() => {});
};
if (typeof window.requestIdleCallback === 'function') {
window.requestIdleCallback(start, { timeout: 3000 });
} else {
window.setTimeout(start, 2000);
}
};
export const onRouteUpdate = () => {
if (process.env.NODE_ENV === 'production' && typeof window.plausible !== 'undefined') {
window.plausible('pageview');
}
captureUtmParams();
requestAnimationFrame(forwardUtmToSignupLinks);
};
export const shouldUpdateScroll = ({ routerProps: { location }, getSavedScrollPosition }) => {
if (location.state && location.state.preventScroll === true) {
return false;
}
if (location.hash) {
window.history.scrollRestoration = 'auto';
return true;
}
// Fix for Gatsby 5 issue with scroll-behavior - https://github.com/gatsbyjs/gatsby/issues/38201
window.history.scrollRestoration = 'manual';
const currentPosition = getSavedScrollPosition(location, location.key);
const top = currentPosition ? currentPosition[1] : 0;
window.setTimeout(() => {
window.requestAnimationFrame(() => {
try {
window.scrollTo({ top, behavior: 'instant' });
} catch (e) {
window.scrollTo(top);
}
});
}, 0);
return false;
};