@extn/segi-js 0.2.14 → 0.2.15
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/README.md +48 -11
- package/dist/cdn.global.js +29 -29
- package/dist/index.d.mts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +41 -3
- package/dist/index.mjs +40 -2
- package/dist/replay/index.d.mts +7 -0
- package/dist/replay/index.d.ts +7 -0
- package/dist/replay/index.js +117 -32
- package/dist/replay/index.mjs +95 -10
- package/package.json +2 -1
package/dist/replay/index.mjs
CHANGED
|
@@ -2,9 +2,24 @@
|
|
|
2
2
|
var UID_KEY = "_bd_session_uid";
|
|
3
3
|
var UID_TS_KEY = "_bd_session_at";
|
|
4
4
|
var ANON_KEY = "_bd_anon";
|
|
5
|
+
var PROGRESS_KEY = "_segi_replay_progress";
|
|
5
6
|
var IDLE_MS = 30 * 6e4;
|
|
6
7
|
var memUid = null;
|
|
7
8
|
var memAnon = null;
|
|
9
|
+
function readReplayProgress(sessionUid) {
|
|
10
|
+
try {
|
|
11
|
+
const p = JSON.parse(sessionStorage.getItem(PROGRESS_KEY) || "null");
|
|
12
|
+
if (p?.sessionUid === sessionUid && Number.isFinite(Date.parse(p.startedAt)) && [p.clickCount, p.pagesVisited].every((n) => Number.isSafeInteger(n) && n >= 0)) return p;
|
|
13
|
+
} catch {
|
|
14
|
+
}
|
|
15
|
+
return { sessionUid, startedAt: (/* @__PURE__ */ new Date()).toISOString(), clickCount: 0, pagesVisited: 0 };
|
|
16
|
+
}
|
|
17
|
+
function writeReplayProgress(progress) {
|
|
18
|
+
try {
|
|
19
|
+
sessionStorage.setItem(PROGRESS_KEY, JSON.stringify(progress));
|
|
20
|
+
} catch {
|
|
21
|
+
}
|
|
22
|
+
}
|
|
8
23
|
function uuid() {
|
|
9
24
|
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
10
25
|
return crypto.randomUUID();
|
|
@@ -37,7 +52,7 @@ function writeSession(uid, at) {
|
|
|
37
52
|
function getOrCreateSessionUid() {
|
|
38
53
|
const now = Date.now();
|
|
39
54
|
const existing = readSession();
|
|
40
|
-
if (existing && now - existing.at < IDLE_MS) {
|
|
55
|
+
if (existing && now - existing.at < IDLE_MS && now - Date.parse(readReplayProgress(existing.uid).startedAt) < IDLE_MS) {
|
|
41
56
|
writeSession(existing.uid, now);
|
|
42
57
|
return existing.uid;
|
|
43
58
|
}
|
|
@@ -123,6 +138,28 @@ async function gzipString(str) {
|
|
|
123
138
|
}
|
|
124
139
|
}
|
|
125
140
|
var active = null;
|
|
141
|
+
var activationEpoch = 0;
|
|
142
|
+
var cleanup = [];
|
|
143
|
+
var pendingErrors = [];
|
|
144
|
+
function saveProgress() {
|
|
145
|
+
if (!active) return;
|
|
146
|
+
writeReplayProgress({
|
|
147
|
+
sessionUid: active.sessionUid,
|
|
148
|
+
startedAt: active.startedAt,
|
|
149
|
+
clickCount: active.clickCount,
|
|
150
|
+
pagesVisited: active.pagesVisited
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
function sendError(signal) {
|
|
154
|
+
if (!active || !active.recordingEnabled || active.paused) return;
|
|
155
|
+
void postTrace(active.config, "/events", { events: [{
|
|
156
|
+
sessionUid: active.sessionUid,
|
|
157
|
+
ts: signal.ts,
|
|
158
|
+
url: signal.url,
|
|
159
|
+
type: "error",
|
|
160
|
+
name: "sdk_error"
|
|
161
|
+
}] });
|
|
162
|
+
}
|
|
126
163
|
var pendingUser = void 0;
|
|
127
164
|
function deviceClass() {
|
|
128
165
|
if (typeof window === "undefined") return "desktop";
|
|
@@ -146,6 +183,7 @@ function isPathBlocked(opts, path) {
|
|
|
146
183
|
async function activateReplay(args) {
|
|
147
184
|
if (active) return;
|
|
148
185
|
if (typeof window === "undefined") return;
|
|
186
|
+
const epoch = ++activationEpoch;
|
|
149
187
|
const options = args.config.options;
|
|
150
188
|
if (options.enabled === false) return;
|
|
151
189
|
const sample = Math.max(0, Math.min(100, options.sample ?? 100));
|
|
@@ -153,6 +191,7 @@ async function activateReplay(args) {
|
|
|
153
191
|
if (isPathBlocked(options, location.pathname)) return;
|
|
154
192
|
const sessionUid = getOrCreateSessionUid();
|
|
155
193
|
const anonId = getOrCreateAnonId();
|
|
194
|
+
const progress = readReplayProgress(sessionUid);
|
|
156
195
|
const transportCfg = {
|
|
157
196
|
endpoint: options.endpoint ?? args.endpoint,
|
|
158
197
|
projectKey: args.projectKey
|
|
@@ -160,7 +199,7 @@ async function activateReplay(args) {
|
|
|
160
199
|
const dto = {
|
|
161
200
|
sessionUid,
|
|
162
201
|
anonId,
|
|
163
|
-
startedAt:
|
|
202
|
+
startedAt: progress.startedAt,
|
|
164
203
|
device: deviceClass(),
|
|
165
204
|
viewportW: window.innerWidth,
|
|
166
205
|
viewportH: window.innerHeight,
|
|
@@ -175,6 +214,19 @@ async function activateReplay(args) {
|
|
|
175
214
|
fbclid: readQuery("fbclid"),
|
|
176
215
|
gclid: readQuery("gclid")
|
|
177
216
|
};
|
|
217
|
+
const onClick = (event) => {
|
|
218
|
+
if (!event.isTrusted || epoch !== activationEpoch) return;
|
|
219
|
+
if (!active) {
|
|
220
|
+
progress.clickCount++;
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (active.paused || active.replayBlocked || !active.recordingEnabled) return;
|
|
224
|
+
active.clickCount++;
|
|
225
|
+
saveProgress();
|
|
226
|
+
bumpSessionActivity();
|
|
227
|
+
};
|
|
228
|
+
window.addEventListener("click", onClick, true);
|
|
229
|
+
cleanup.push(() => window.removeEventListener("click", onClick, true));
|
|
178
230
|
let recordingEnabled = true;
|
|
179
231
|
try {
|
|
180
232
|
const res = await postTrace(transportCfg, "/session/start", dto);
|
|
@@ -184,7 +236,10 @@ async function activateReplay(args) {
|
|
|
184
236
|
}
|
|
185
237
|
} catch {
|
|
186
238
|
}
|
|
239
|
+
if (epoch !== activationEpoch) return;
|
|
187
240
|
if (!recordingEnabled) {
|
|
241
|
+
window.removeEventListener("click", onClick, true);
|
|
242
|
+
pendingErrors = [];
|
|
188
243
|
active = {
|
|
189
244
|
sessionUid,
|
|
190
245
|
config: transportCfg,
|
|
@@ -210,10 +265,13 @@ async function activateReplay(args) {
|
|
|
210
265
|
paused: false,
|
|
211
266
|
userId: null,
|
|
212
267
|
startedAt: dto.startedAt,
|
|
213
|
-
pagesVisited: 1,
|
|
214
|
-
clickCount:
|
|
268
|
+
pagesVisited: progress.pagesVisited + 1,
|
|
269
|
+
clickCount: progress.clickCount,
|
|
215
270
|
recordingEnabled: true
|
|
216
271
|
};
|
|
272
|
+
saveProgress();
|
|
273
|
+
for (const error of pendingErrors) sendError(error);
|
|
274
|
+
pendingErrors = [];
|
|
217
275
|
if (pendingUser !== void 0) replayApi.setUser(pendingUser);
|
|
218
276
|
let lastSessionBump = Date.now();
|
|
219
277
|
const bumpIfStale = () => {
|
|
@@ -224,6 +282,7 @@ async function activateReplay(args) {
|
|
|
224
282
|
};
|
|
225
283
|
for (const ev of ["mousedown", "keydown", "scroll", "touchstart"]) {
|
|
226
284
|
window.addEventListener(ev, bumpIfStale, { passive: true, capture: true });
|
|
285
|
+
cleanup.push(() => window.removeEventListener(ev, bumpIfStale, true));
|
|
227
286
|
}
|
|
228
287
|
const w = window;
|
|
229
288
|
const scheduleRrwebStart = (run) => {
|
|
@@ -247,11 +306,15 @@ async function activateReplay(args) {
|
|
|
247
306
|
void import("../rrweb-O2KFXCVA.mjs").then(
|
|
248
307
|
({ startRrweb }) => new Promise((resolve) => {
|
|
249
308
|
scheduleRrwebStart(() => {
|
|
309
|
+
if (epoch !== activationEpoch) {
|
|
310
|
+
resolve(null);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
250
313
|
startRrweb({
|
|
251
314
|
sessionUid,
|
|
252
315
|
options,
|
|
253
316
|
sendChunk: async (chunk, useBeacon) => {
|
|
254
|
-
if (!active || active.replayBlocked) return;
|
|
317
|
+
if (epoch !== activationEpoch || !active || active.replayBlocked) return;
|
|
255
318
|
const bodyStr = `{"sessionUid":${JSON.stringify(sessionUid)},"chunk":[${chunk.join(",")}]}`;
|
|
256
319
|
const base = (transportCfg.endpoint || "https://segiapi.extn.ai").replace(/\/+$/, "").replace(/\/api\/ingest\/events$/, "").replace(/\/api\/ingest$/, "").replace(/\/api$/, "");
|
|
257
320
|
if (useBeacon) {
|
|
@@ -295,7 +358,8 @@ async function activateReplay(args) {
|
|
|
295
358
|
});
|
|
296
359
|
})
|
|
297
360
|
).then((handles) => {
|
|
298
|
-
if (handles && active) active.rrweb = handles;
|
|
361
|
+
if (handles && active && epoch === activationEpoch) active.rrweb = handles;
|
|
362
|
+
else handles?.stop();
|
|
299
363
|
}).catch(() => {
|
|
300
364
|
});
|
|
301
365
|
void postTrace(transportCfg, "/events", {
|
|
@@ -309,7 +373,7 @@ async function activateReplay(args) {
|
|
|
309
373
|
}
|
|
310
374
|
]
|
|
311
375
|
});
|
|
312
|
-
|
|
376
|
+
const onPageHide = () => {
|
|
313
377
|
if (!active) return;
|
|
314
378
|
active.rrweb?.flush(true);
|
|
315
379
|
beaconTrace(active.config, "/session/end", {
|
|
@@ -319,9 +383,11 @@ async function activateReplay(args) {
|
|
|
319
383
|
pagesVisited: active.pagesVisited,
|
|
320
384
|
clickCount: active.clickCount
|
|
321
385
|
});
|
|
322
|
-
}
|
|
386
|
+
};
|
|
387
|
+
window.addEventListener("pagehide", onPageHide);
|
|
388
|
+
cleanup.push(() => window.removeEventListener("pagehide", onPageHide));
|
|
323
389
|
const MAX_SESSION_DURATION_MS = 30 * 6e4;
|
|
324
|
-
setTimeout(() => {
|
|
390
|
+
const endTimer = setTimeout(() => {
|
|
325
391
|
if (!active) return;
|
|
326
392
|
active.rrweb?.flush(true);
|
|
327
393
|
active.rrweb?.stop();
|
|
@@ -335,9 +401,27 @@ async function activateReplay(args) {
|
|
|
335
401
|
});
|
|
336
402
|
active.paused = true;
|
|
337
403
|
active.replayBlocked = true;
|
|
338
|
-
}, MAX_SESSION_DURATION_MS);
|
|
404
|
+
}, Math.max(0, MAX_SESSION_DURATION_MS - (Date.now() - Date.parse(progress.startedAt))));
|
|
405
|
+
cleanup.push(() => clearTimeout(endTimer));
|
|
339
406
|
}
|
|
340
407
|
var replayApi = {
|
|
408
|
+
/** Internal bridge from the error SDK; no stack/message is copied to trace. */
|
|
409
|
+
_captureError(signal) {
|
|
410
|
+
if (!active) {
|
|
411
|
+
if (pendingErrors.length < 20) pendingErrors.push(signal);
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
sendError(signal);
|
|
415
|
+
},
|
|
416
|
+
_stop() {
|
|
417
|
+
activationEpoch++;
|
|
418
|
+
active?.rrweb?.stop();
|
|
419
|
+
for (const remove of cleanup) remove();
|
|
420
|
+
cleanup = [];
|
|
421
|
+
active = null;
|
|
422
|
+
pendingErrors = [];
|
|
423
|
+
pendingUser = void 0;
|
|
424
|
+
},
|
|
341
425
|
pause() {
|
|
342
426
|
active?.rrweb?.pause();
|
|
343
427
|
if (active) active.paused = true;
|
|
@@ -396,6 +480,7 @@ var replayApi = {
|
|
|
396
480
|
active.paused = false;
|
|
397
481
|
}
|
|
398
482
|
active.pagesVisited += 1;
|
|
483
|
+
saveProgress();
|
|
399
484
|
active.rrweb?.takeFullSnapshot();
|
|
400
485
|
void postTrace(active.config, "/events", {
|
|
401
486
|
events: [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@extn/segi-js",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.15",
|
|
4
4
|
"description": "Browser JavaScript SDK for the segi error monitoring service",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "tsup",
|
|
40
|
+
"test:browser": "node test/browser-runtime.cjs",
|
|
40
41
|
"dev": "tsup --watch",
|
|
41
42
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
42
43
|
"clean": "rm -rf dist"
|