@extn/segi-js 0.2.11 → 0.2.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cdn.global.js +26 -26
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -0
- package/dist/index.mjs +2 -0
- package/dist/replay/index.d.mts +5 -5
- package/dist/replay/index.d.ts +5 -5
- package/dist/replay/index.js +45 -185
- package/dist/replay/index.mjs +23 -163
- package/dist/{types-DfmnOA9D.d.mts → types-CsgcNSc1.d.mts} +5 -5
- package/dist/{types-DfmnOA9D.d.ts → types-CsgcNSc1.d.ts} +5 -5
- package/package.json +1 -1
package/dist/replay/index.mjs
CHANGED
|
@@ -1,142 +1,3 @@
|
|
|
1
|
-
// src/replay/heatmap.ts
|
|
2
|
-
var FLUSH_CLICK_INTERVAL_MS = 5e3;
|
|
3
|
-
var FLUSH_CLICK_BUFFER = 50;
|
|
4
|
-
var SCROLL_THROTTLE_MS = 1e3;
|
|
5
|
-
function deviceClass(w) {
|
|
6
|
-
if (w < 640) return "mobile";
|
|
7
|
-
if (w < 1024) return "tablet";
|
|
8
|
-
return "desktop";
|
|
9
|
-
}
|
|
10
|
-
function isMaskedElement(el, maskClass) {
|
|
11
|
-
let cur = el;
|
|
12
|
-
while (cur) {
|
|
13
|
-
if (cur.classList?.contains(maskClass)) return true;
|
|
14
|
-
cur = cur.parentElement;
|
|
15
|
-
}
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
function buildSelector(el) {
|
|
19
|
-
try {
|
|
20
|
-
if (el.id) return `#${el.id}`;
|
|
21
|
-
const tag = el.tagName?.toLowerCase();
|
|
22
|
-
if (!tag) return void 0;
|
|
23
|
-
const cls = (el.className && typeof el.className === "string" ? el.className.split(/\s+/).filter(Boolean).slice(0, 2) : []).map((c) => `.${c}`);
|
|
24
|
-
return `${tag}${cls.join("")}`.slice(0, 512);
|
|
25
|
-
} catch {
|
|
26
|
-
return void 0;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
function startHeatmap(args) {
|
|
30
|
-
const maskClass = args.options.mask?.class ?? "segi-mask";
|
|
31
|
-
let paused = false;
|
|
32
|
-
const buffer = [];
|
|
33
|
-
let lastScrollPct = -1;
|
|
34
|
-
let lastScrollPostAt = 0;
|
|
35
|
-
const flushClicks = (opts = {}) => {
|
|
36
|
-
if (buffer.length === 0) return;
|
|
37
|
-
const batch = buffer.splice(0, buffer.length);
|
|
38
|
-
args.sendClicks(batch, Boolean(opts.useBeacon));
|
|
39
|
-
};
|
|
40
|
-
const interval = setInterval(() => {
|
|
41
|
-
if (!paused) flushClicks();
|
|
42
|
-
}, FLUSH_CLICK_INTERVAL_MS);
|
|
43
|
-
const onClick = (e) => {
|
|
44
|
-
if (paused) return;
|
|
45
|
-
try {
|
|
46
|
-
const target = e.target;
|
|
47
|
-
if (!target) return;
|
|
48
|
-
if (isMaskedElement(target, maskClass)) return;
|
|
49
|
-
const docW = Math.max(
|
|
50
|
-
document.documentElement.scrollWidth,
|
|
51
|
-
document.body?.scrollWidth ?? 0,
|
|
52
|
-
1
|
|
53
|
-
);
|
|
54
|
-
const docH = Math.max(
|
|
55
|
-
document.documentElement.scrollHeight,
|
|
56
|
-
document.body?.scrollHeight ?? 0,
|
|
57
|
-
1
|
|
58
|
-
);
|
|
59
|
-
const pageX = e.pageX;
|
|
60
|
-
const pageY = e.pageY;
|
|
61
|
-
const w = window.innerWidth;
|
|
62
|
-
const row = {
|
|
63
|
-
sessionUid: args.sessionUid,
|
|
64
|
-
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
65
|
-
urlPath: location.pathname.slice(0, 255),
|
|
66
|
-
device: deviceClass(w),
|
|
67
|
-
viewportW: w,
|
|
68
|
-
viewportH: window.innerHeight,
|
|
69
|
-
x: Math.round(e.clientX),
|
|
70
|
-
y: Math.round(e.clientY),
|
|
71
|
-
pageX: Math.round(pageX),
|
|
72
|
-
pageY: Math.round(pageY),
|
|
73
|
-
relX: Math.max(0, Math.min(1, pageX / docW)),
|
|
74
|
-
relY: Math.max(0, Math.min(1, pageY / docH)),
|
|
75
|
-
selector: buildSelector(target),
|
|
76
|
-
tag: target.tagName?.toLowerCase().slice(0, 32),
|
|
77
|
-
text: (target.textContent ?? void 0)?.trim().slice(0, 128) || void 0
|
|
78
|
-
};
|
|
79
|
-
buffer.push(row);
|
|
80
|
-
if (buffer.length >= FLUSH_CLICK_BUFFER) flushClicks();
|
|
81
|
-
} catch {
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
const onScroll = () => {
|
|
85
|
-
if (paused) return;
|
|
86
|
-
const now = Date.now();
|
|
87
|
-
if (now - lastScrollPostAt < SCROLL_THROTTLE_MS) return;
|
|
88
|
-
try {
|
|
89
|
-
const docH = Math.max(
|
|
90
|
-
document.documentElement.scrollHeight,
|
|
91
|
-
document.body?.scrollHeight ?? 0,
|
|
92
|
-
1
|
|
93
|
-
);
|
|
94
|
-
const viewH = window.innerHeight;
|
|
95
|
-
const maxY = Math.round(window.scrollY + viewH);
|
|
96
|
-
const pct = Math.max(0, Math.min(100, Math.round(maxY / docH * 100)));
|
|
97
|
-
if (pct <= lastScrollPct) return;
|
|
98
|
-
lastScrollPct = pct;
|
|
99
|
-
lastScrollPostAt = now;
|
|
100
|
-
const w = window.innerWidth;
|
|
101
|
-
args.sendScroll(
|
|
102
|
-
{
|
|
103
|
-
sessionUid: args.sessionUid,
|
|
104
|
-
urlPath: location.pathname.slice(0, 255),
|
|
105
|
-
device: deviceClass(w),
|
|
106
|
-
viewportH: viewH,
|
|
107
|
-
docH,
|
|
108
|
-
maxScrollY: maxY,
|
|
109
|
-
maxScrollPct: pct
|
|
110
|
-
},
|
|
111
|
-
false
|
|
112
|
-
);
|
|
113
|
-
} catch {
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
const onPageHide = () => {
|
|
117
|
-
flushClicks({ useBeacon: true });
|
|
118
|
-
};
|
|
119
|
-
window.addEventListener("click", onClick, true);
|
|
120
|
-
window.addEventListener("scroll", onScroll, { capture: true, passive: true });
|
|
121
|
-
window.addEventListener("pagehide", onPageHide);
|
|
122
|
-
return {
|
|
123
|
-
pause() {
|
|
124
|
-
paused = true;
|
|
125
|
-
},
|
|
126
|
-
resume() {
|
|
127
|
-
paused = false;
|
|
128
|
-
},
|
|
129
|
-
stop() {
|
|
130
|
-
clearInterval(interval);
|
|
131
|
-
window.removeEventListener("click", onClick, true);
|
|
132
|
-
window.removeEventListener("scroll", onScroll, true);
|
|
133
|
-
window.removeEventListener("pagehide", onPageHide);
|
|
134
|
-
flushClicks({ useBeacon: true });
|
|
135
|
-
},
|
|
136
|
-
flushClicks
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
|
|
140
1
|
// src/replay/session.ts
|
|
141
2
|
var UID_KEY = "_bd_session_uid";
|
|
142
3
|
var UID_TS_KEY = "_bd_session_at";
|
|
@@ -248,8 +109,21 @@ function beaconTrace(cfg, path, body) {
|
|
|
248
109
|
function withReplay(options = {}) {
|
|
249
110
|
return { __segi_replay: true, options };
|
|
250
111
|
}
|
|
112
|
+
async function gzipString(str) {
|
|
113
|
+
try {
|
|
114
|
+
if (typeof CompressionStream === "undefined") return null;
|
|
115
|
+
const cs = new CompressionStream("gzip");
|
|
116
|
+
const writer = cs.writable.getWriter();
|
|
117
|
+
void writer.write(new TextEncoder().encode(str));
|
|
118
|
+
void writer.close();
|
|
119
|
+
const buf = await new Response(cs.readable).arrayBuffer();
|
|
120
|
+
return new Uint8Array(buf);
|
|
121
|
+
} catch {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
251
125
|
var active = null;
|
|
252
|
-
function
|
|
126
|
+
function deviceClass() {
|
|
253
127
|
if (typeof window === "undefined") return "desktop";
|
|
254
128
|
const w = window.innerWidth;
|
|
255
129
|
if (w < 640) return "mobile";
|
|
@@ -286,7 +160,7 @@ async function activateReplay(args) {
|
|
|
286
160
|
sessionUid,
|
|
287
161
|
anonId,
|
|
288
162
|
startedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
289
|
-
device:
|
|
163
|
+
device: deviceClass(),
|
|
290
164
|
viewportW: window.innerWidth,
|
|
291
165
|
viewportH: window.innerHeight,
|
|
292
166
|
ua: navigator.userAgent?.slice(0, 512),
|
|
@@ -314,7 +188,6 @@ async function activateReplay(args) {
|
|
|
314
188
|
sessionUid,
|
|
315
189
|
config: transportCfg,
|
|
316
190
|
options,
|
|
317
|
-
heatmap: null,
|
|
318
191
|
rrweb: null,
|
|
319
192
|
replayBlocked: false,
|
|
320
193
|
paused: true,
|
|
@@ -326,22 +199,10 @@ async function activateReplay(args) {
|
|
|
326
199
|
};
|
|
327
200
|
return;
|
|
328
201
|
}
|
|
329
|
-
const heatmap = startHeatmap({
|
|
330
|
-
sessionUid,
|
|
331
|
-
options,
|
|
332
|
-
sendClicks: (rows, useBeacon) => {
|
|
333
|
-
if (useBeacon) beaconTrace(transportCfg, "/heatmap/clicks", { clicks: rows });
|
|
334
|
-
else void postTrace(transportCfg, "/heatmap/clicks", { clicks: rows });
|
|
335
|
-
},
|
|
336
|
-
sendScroll: (row) => {
|
|
337
|
-
void postTrace(transportCfg, "/heatmap/scroll", row);
|
|
338
|
-
}
|
|
339
|
-
});
|
|
340
202
|
active = {
|
|
341
203
|
sessionUid,
|
|
342
204
|
config: transportCfg,
|
|
343
205
|
options,
|
|
344
|
-
heatmap,
|
|
345
206
|
rrweb: null,
|
|
346
207
|
replayBlocked: false,
|
|
347
208
|
paused: false,
|
|
@@ -404,13 +265,18 @@ async function activateReplay(args) {
|
|
|
404
265
|
return;
|
|
405
266
|
}
|
|
406
267
|
try {
|
|
268
|
+
const gz = await gzipString(bodyStr);
|
|
407
269
|
const res = await fetch(`${base}/api/trace/replay`, {
|
|
408
270
|
method: "POST",
|
|
409
|
-
headers: {
|
|
271
|
+
headers: gz ? {
|
|
272
|
+
"Content-Type": "application/json",
|
|
273
|
+
"Content-Encoding": "gzip",
|
|
274
|
+
"X-Segi-Project-Key": transportCfg.projectKey
|
|
275
|
+
} : {
|
|
410
276
|
"Content-Type": "application/json",
|
|
411
277
|
"X-Segi-Project-Key": transportCfg.projectKey
|
|
412
278
|
},
|
|
413
|
-
body: bodyStr
|
|
279
|
+
body: gz ?? bodyStr
|
|
414
280
|
});
|
|
415
281
|
if (res.status === 429) {
|
|
416
282
|
if (active) {
|
|
@@ -457,8 +323,6 @@ async function activateReplay(args) {
|
|
|
457
323
|
active.rrweb?.flush(true);
|
|
458
324
|
active.rrweb?.stop();
|
|
459
325
|
active.rrweb = null;
|
|
460
|
-
active.heatmap?.stop();
|
|
461
|
-
active.heatmap = null;
|
|
462
326
|
beaconTrace(active.config, "/session/end", {
|
|
463
327
|
sessionUid: active.sessionUid,
|
|
464
328
|
endedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
@@ -472,12 +336,10 @@ async function activateReplay(args) {
|
|
|
472
336
|
}
|
|
473
337
|
var replayApi = {
|
|
474
338
|
pause() {
|
|
475
|
-
active?.heatmap?.pause();
|
|
476
339
|
active?.rrweb?.pause();
|
|
477
340
|
if (active) active.paused = true;
|
|
478
341
|
},
|
|
479
342
|
resume() {
|
|
480
|
-
active?.heatmap?.resume();
|
|
481
343
|
active?.rrweb?.resume();
|
|
482
344
|
if (active) active.paused = false;
|
|
483
345
|
bumpSessionActivity();
|
|
@@ -490,7 +352,7 @@ var replayApi = {
|
|
|
490
352
|
anonId: getOrCreateAnonId(),
|
|
491
353
|
userId: active.userId,
|
|
492
354
|
startedAt: active.startedAt,
|
|
493
|
-
device:
|
|
355
|
+
device: deviceClass(),
|
|
494
356
|
viewportW: window.innerWidth,
|
|
495
357
|
viewportH: window.innerHeight
|
|
496
358
|
});
|
|
@@ -520,14 +382,12 @@ var replayApi = {
|
|
|
520
382
|
const nowBlocked = isPathBlocked(active.options, location.pathname);
|
|
521
383
|
if (nowBlocked) {
|
|
522
384
|
if (!active.paused) {
|
|
523
|
-
active.heatmap?.pause();
|
|
524
385
|
active.rrweb?.pause();
|
|
525
386
|
active.paused = true;
|
|
526
387
|
}
|
|
527
388
|
return;
|
|
528
389
|
}
|
|
529
390
|
if (active.paused) {
|
|
530
|
-
active.heatmap?.resume();
|
|
531
391
|
active.rrweb?.resume();
|
|
532
392
|
active.paused = false;
|
|
533
393
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Replay
|
|
2
|
+
* Replay collector — public option types and internal DTOs.
|
|
3
3
|
* Mirrors apps/api/src/trace/trace.types.ts at the field level.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* Covers session start/end, page_view / custom trace events, and rrweb DOM
|
|
6
|
+
* replay. (Heatmap collection — click/scroll/move — was removed alongside the
|
|
7
|
+
* server-side /api/trace/heatmap/* endpoints.)
|
|
8
8
|
*/
|
|
9
9
|
interface ReplayOptions {
|
|
10
10
|
/** Toggle the whole module. Setting `false` causes withReplay() to return null. */
|
|
@@ -13,7 +13,7 @@ interface ReplayOptions {
|
|
|
13
13
|
sample?: number;
|
|
14
14
|
/** Path-prefix deny-list (in addition to the server-side per-project list). */
|
|
15
15
|
blockRoutes?: string[];
|
|
16
|
-
/** rrweb
|
|
16
|
+
/** rrweb mask. Class is recursively checked up the ancestor chain. */
|
|
17
17
|
mask?: {
|
|
18
18
|
inputs?: "all" | "sensitive" | "none";
|
|
19
19
|
class?: string;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Replay
|
|
2
|
+
* Replay collector — public option types and internal DTOs.
|
|
3
3
|
* Mirrors apps/api/src/trace/trace.types.ts at the field level.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* Covers session start/end, page_view / custom trace events, and rrweb DOM
|
|
6
|
+
* replay. (Heatmap collection — click/scroll/move — was removed alongside the
|
|
7
|
+
* server-side /api/trace/heatmap/* endpoints.)
|
|
8
8
|
*/
|
|
9
9
|
interface ReplayOptions {
|
|
10
10
|
/** Toggle the whole module. Setting `false` causes withReplay() to return null. */
|
|
@@ -13,7 +13,7 @@ interface ReplayOptions {
|
|
|
13
13
|
sample?: number;
|
|
14
14
|
/** Path-prefix deny-list (in addition to the server-side per-project list). */
|
|
15
15
|
blockRoutes?: string[];
|
|
16
|
-
/** rrweb
|
|
16
|
+
/** rrweb mask. Class is recursively checked up the ancestor chain. */
|
|
17
17
|
mask?: {
|
|
18
18
|
inputs?: "all" | "sensitive" | "none";
|
|
19
19
|
class?: string;
|