-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Expand file tree
/
Copy pathindex.js
More file actions
175 lines (143 loc) · 4.3 KB
/
Copy pathindex.js
File metadata and controls
175 lines (143 loc) · 4.3 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { record } from 'rrweb';
import { getRecordConsolePlugin } from '@rrweb/rrweb-plugin-console-record';
(window => {
const { document } = window;
const { currentScript } = document;
if (!currentScript) return;
const _data = 'data-';
const attr = currentScript.getAttribute.bind(currentScript);
const config = value => attr(`${_data}${value}`);
const website = config(`website-id`);
const hostUrl = config(`host-url`);
const sampleRate = parseFloat(config(`sample-rate`) || '0.15');
const maskLevel = config(`mask-level`) || 'moderate';
const maxDuration = parseInt(config(`max-duration`) || '300000', 10);
const blockSelector = config(`block-selector`) || '';
const recordConsole = config(`record-console`) === 'true';
if (!website) return;
// Sample rate check
if (sampleRate < 1 && Math.random() > sampleRate) return;
const host =
hostUrl || '__COLLECT_API_HOST__' || currentScript.src.split('/').slice(0, -1).join('/');
const endpoint = `${host.replace(/\/$/, '')}__COLLECT_REPLAY_ENDPOINT__`;
const FLUSH_EVENT_COUNT = 100;
const FLUSH_INTERVAL = 10000;
let eventBuffer = [];
let stopFn = null;
let flushTimer = null;
let startTime = null;
let stopped = false;
const sendEvents = (events, useKeepalive = false) => {
const session = window.umami?.getSession?.();
if (!session?.cache) return;
const body = JSON.stringify({
type: 'record',
payload: {
website,
events,
timestamp: Math.floor(Date.now() / 1000),
},
});
// keepalive has a 64KB body limit — only use it for small payloads on unload
const keepalive = useKeepalive && body.length < 60000;
return fetch(endpoint, {
keepalive,
method: 'POST',
body,
headers: {
'Content-Type': 'application/json',
'x-umami-cache': session.cache,
},
credentials: 'omit',
}).catch(() => {});
};
const flush = (useKeepalive = false) => {
if (!eventBuffer.length) return;
const events = eventBuffer;
eventBuffer = [];
sendEvents(events, useKeepalive);
};
const scheduleFlush = () => {
if (flushTimer) clearTimeout(flushTimer);
flushTimer = setTimeout(flush, FLUSH_INTERVAL);
};
const stop = () => {
if (stopped) return;
stopped = true;
if (flushTimer) clearTimeout(flushTimer);
flush();
if (stopFn) stopFn();
};
const getMaskConfig = level => {
switch (level) {
case 'strict':
return {
maskAllInputs: true,
maskTextSelector: '*',
};
default: // moderate
return {
maskAllInputs: true,
};
}
};
const waitForSession = (attempts = 0) => {
if (attempts > 50) return;
const session = window.umami?.getSession?.();
if (session?.cache) {
beginRecording();
} else {
setTimeout(() => waitForSession(attempts + 1), 100);
}
};
const beginRecording = () => {
startTime = Date.now();
const maskConfig = getMaskConfig(maskLevel);
const plugins = [];
if (recordConsole) {
plugins.push(getRecordConsolePlugin());
}
stopFn = record({
emit(event) {
if (stopped) return;
if (Date.now() - startTime > maxDuration) {
stop();
return;
}
eventBuffer.push(event);
if (eventBuffer.length >= FLUSH_EVENT_COUNT) {
flush();
}
scheduleFlush();
},
...maskConfig,
plugins,
inlineStylesheet: true,
slimDOMOptions: {
script: true,
comment: true,
headMetaDescKeywords: true,
headMetaSocial: true,
headMetaRobots: true,
headMetaHttpEquiv: true,
headMetaAuthorship: true,
headMetaVerification: true,
},
recordCanvas: false,
recordCrossOriginIframes: false,
checkoutEveryNms: 30000,
...(blockSelector && { blockSelector }),
});
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') flush(true);
});
window.addEventListener('beforeunload', () => flush(true));
};
if (document.readyState === 'complete') {
waitForSession();
} else {
document.addEventListener('readystatechange', () => {
if (document.readyState === 'complete') waitForSession();
});
}
})(window);