-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.js
More file actions
351 lines (312 loc) · 10.3 KB
/
Copy pathusage.js
File metadata and controls
351 lines (312 loc) · 10.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* First-party, cookieless usage measurement for Sredstva.
*
* Public visitors receive no analytics cookie or persistent identifier. The
* random tab session ID below exists only in memory. The sole storage exception
* is an explicit owner opt-out flag set from the private /stats page.
*
* DNT/GPC signals do not switch the counter off: those signals govern
* cross-site tracking and sale of personal data, and this measurement stores
* no identifier at all. The disclosure in /extension-privacy/ says the same —
* keep the two in sync.
*/
(() => {
'use strict';
const OPT_OUT_KEY = 'sredstva-usage-optout';
const MAX_QUEUE = 200;
const MAX_BATCH = 40;
const ENGAGE_WINDOW = 15000;
function ownerOptedOut() {
try {
return localStorage.getItem(OPT_OUT_KEY) === '1';
} catch (_) {
return false;
}
}
if (ownerOptedOut() || typeof fetch !== 'function') {
window.havcUsage = () => {};
window.havcUsageStatus = {
enabled: false,
reason: ownerOptedOut() ? 'owner-optout' : 'fetch-unavailable',
};
return;
}
const randomPart = () => Math.random().toString(36).slice(2, 12);
const sessionId = (crypto.randomUUID && crypto.randomUUID()) ||
`${Date.now().toString(36)}_${randomPart()}_${randomPart()}`;
const now = () => performance.now();
const started = now();
let referrerHost = '';
try {
if (document.referrer) {
const host = new URL(document.referrer).host;
if (host !== location.host) referrerHost = host;
}
} catch (_) {
// An unreadable referrer is treated as direct.
}
let queue = [];
let flushTimer = null;
let retryTimer = null;
let retryDelay = 1000;
let fetchesInFlight = 0;
let endSent = false;
let pageWasHidden = document.visibilityState !== 'visible';
function clearScheduledFlush() {
if (flushTimer !== null) clearTimeout(flushTimer);
flushTimer = null;
}
function scheduleFlush(delay = 5000) {
if (flushTimer !== null || retryTimer !== null || !queue.length) return;
flushTimer = setTimeout(() => {
flushTimer = null;
flush(false);
}, delay);
}
function scheduleRetry() {
if (retryTimer !== null || !queue.length) return;
retryTimer = setTimeout(() => {
retryTimer = null;
flush(false);
}, retryDelay);
retryDelay = Math.min(retryDelay * 2, 30000);
}
function restoreBatch(batch) {
queue = batch.concat(queue).slice(0, MAX_QUEUE);
scheduleRetry();
}
function bodyFor(batch) {
return JSON.stringify({ s: sessionId, ref: referrerHost, e: batch });
}
function sendWithFetch(batch) {
fetchesInFlight++;
return fetch('/api/u', {
method: 'POST',
body: bodyFor(batch),
keepalive: true,
headers: { 'content-type': 'application/json' },
credentials: 'same-origin',
})
.then((response) => {
if (!response.ok) throw new Error(`usage beacon failed: ${response.status}`);
retryDelay = 1000;
return true;
})
.catch(() => {
restoreBatch(batch);
return false;
})
.finally(() => {
fetchesInFlight--;
if (queue.length && retryTimer === null) scheduleFlush(250);
});
}
function flush(preferBeacon) {
if (!queue.length) return Promise.resolve(true);
if (!preferBeacon && fetchesInFlight > 0) {
scheduleFlush(250);
return Promise.resolve(false);
}
clearScheduledFlush();
if (retryTimer !== null) {
clearTimeout(retryTimer);
retryTimer = null;
}
const batch = queue.splice(0, MAX_BATCH);
if (preferBeacon && typeof navigator.sendBeacon === 'function') {
try {
const accepted = navigator.sendBeacon(
'/api/u',
new Blob([bodyFor(batch)], { type: 'application/json' }),
);
if (accepted) {
retryDelay = 1000;
if (queue.length) scheduleFlush(250);
return Promise.resolve(true);
}
} catch (_) {
// A false return or exception falls through to keepalive fetch.
}
}
return sendWithFetch(batch);
}
window.havcUsage = (name, detail, value, project) => {
if (typeof name !== 'string' || !name) return;
const event = {
n: name,
t: Math.max(0, Math.round(now() - started)),
};
if (typeof detail === 'string' && detail) event.d = detail.slice(0, 80);
if (Number.isFinite(value)) event.v = Math.round(value);
if (typeof project === 'string' && project) event.p = project.slice(0, 80);
queue.push(event);
if (queue.length > MAX_QUEUE) queue.shift();
if (queue.length >= 20) flush(false);
else scheduleFlush();
};
window.havcUsageStatus = { enabled: true, storage: 'none', session: 'tab-memory' };
// Count the page even if registry data later fails to load.
window.havcUsage('page_view', 'app');
// Tab activity and dwell ------------------------------------------------
let visibleMs = 0;
let engagedMs = 0;
let returns = 0;
let visibleSince = document.visibilityState === 'visible' ? now() : null;
let engagedSince = null;
let lastInput = 0;
function settleEngagement(at) {
if (engagedSince === null) return;
engagedMs += Math.max(0, Math.min(at, lastInput + ENGAGE_WINDOW) - engagedSince);
engagedSince = null;
}
function onInput() {
const at = now();
if (visibleSince === null) return;
if (engagedSince !== null && at - lastInput > ENGAGE_WINDOW) settleEngagement(at);
if (engagedSince === null) engagedSince = at;
lastInput = at;
}
for (const eventName of ['pointerdown', 'keydown', 'wheel', 'touchstart', 'scroll']) {
window.addEventListener(eventName, onInput, { passive: true, capture: true });
}
function settleVisibility(at = now()) {
settleEngagement(at);
if (visibleSince !== null) {
visibleMs += Math.max(0, at - visibleSince);
visibleSince = null;
}
}
function dwellSnapshot(at = now()) {
const visible = visibleMs + (visibleSince !== null ? Math.max(0, at - visibleSince) : 0);
const engaged = engagedMs + (engagedSince !== null
? Math.max(0, Math.min(at, lastInput + ENGAGE_WINDOW) - engagedSince)
: 0);
return `${Math.round(visible)}|${Math.round(Math.min(engaged, visible))}|${returns}`;
}
// Performance ----------------------------------------------------------
const observers = [];
const reportedVitals = new Set();
let lcp = null;
let cls = 0;
let clsWindow = 0;
let clsWindowStart = 0;
let clsLastAt = 0;
const interactions = new Map();
let lcpSupported = false;
let clsSupported = false;
let inpSupported = false;
function observe(type, callback, options = {}) {
if (typeof PerformanceObserver !== 'function') return false;
try {
const observer = new PerformanceObserver((list) => callback(list.getEntries()));
observer.observe({ type, buffered: true, ...options });
observers.push(observer);
return true;
} catch (_) {
return false;
}
}
try {
const navigation = performance.getEntriesByType('navigation')[0];
if (navigation && Number.isFinite(navigation.responseStart)) {
window.havcUsage('web_vital', 'ttfb', navigation.responseStart);
reportedVitals.add('ttfb');
}
} catch (_) {}
observe('paint', (entries) => {
const entry = entries.find((candidate) => candidate.name === 'first-contentful-paint');
if (entry && !reportedVitals.has('fcp')) {
reportedVitals.add('fcp');
window.havcUsage('web_vital', 'fcp', entry.startTime);
}
});
lcpSupported = observe('largest-contentful-paint', (entries) => {
const entry = entries[entries.length - 1];
if (entry) lcp = entry.startTime;
});
clsSupported = observe('layout-shift', (entries) => {
for (const entry of entries) {
if (entry.hadRecentInput) continue;
if (!clsWindowStart || entry.startTime - clsLastAt > 1000 || entry.startTime - clsWindowStart > 5000) {
clsWindowStart = entry.startTime;
clsWindow = entry.value;
} else {
clsWindow += entry.value;
}
clsLastAt = entry.startTime;
cls = Math.max(cls, clsWindow);
}
});
inpSupported = observe('event', (entries) => {
for (const entry of entries) {
if (!entry.interactionId || !Number.isFinite(entry.duration)) continue;
interactions.set(
entry.interactionId,
Math.max(interactions.get(entry.interactionId) || 0, entry.duration),
);
}
}, { durationThreshold: 40 });
function estimatedInp() {
const values = [...interactions.values()].sort((a, b) => b - a);
if (!values.length) return null;
return values[Math.min(values.length - 1, Math.floor(values.length / 50))];
}
function reportFinalVitals() {
if (lcpSupported && lcp !== null && !reportedVitals.has('lcp')) {
reportedVitals.add('lcp');
window.havcUsage('web_vital', 'lcp', lcp);
}
if (clsSupported && !reportedVitals.has('cls')) {
reportedVitals.add('cls');
window.havcUsage('web_vital', 'cls', cls * 1000);
}
const inp = estimatedInp();
if (inpSupported && inp !== null && !reportedVitals.has('inp')) {
reportedVitals.add('inp');
window.havcUsage('web_vital', 'inp', inp);
}
}
// Lifecycle ------------------------------------------------------------
function onLeave() {
const at = now();
settleVisibility(at);
reportFinalVitals();
if (!endSent) {
endSent = true;
window.havcUsage('session_end', dwellSnapshot(at), at - started);
}
flush(true);
}
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
pageWasHidden = true;
onLeave();
return;
}
const at = now();
if (pageWasHidden) returns++;
pageWasHidden = false;
endSent = false;
if (visibleSince === null) visibleSince = at;
});
window.addEventListener('pagehide', onLeave);
window.addEventListener('pageshow', (event) => {
if (!event.persisted) return;
const at = now();
endSent = false;
if (document.visibilityState === 'visible' && visibleSince === null) visibleSince = at;
if (pageWasHidden) {
returns++;
pageWasHidden = false;
}
window.havcUsage('page_view', 'app');
});
if (window.__HAVC_USAGE_TEST__) {
window.__havcUsageTest = {
flush,
queueSize: () => queue.length,
sessionId,
inFlight: () => fetchesInFlight,
};
}
})();