@extn/segi-js 0.2.9 → 0.2.12

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.
@@ -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";
@@ -249,7 +110,7 @@ function withReplay(options = {}) {
249
110
  return { __segi_replay: true, options };
250
111
  }
251
112
  var active = null;
252
- function deviceClass2() {
113
+ function deviceClass() {
253
114
  if (typeof window === "undefined") return "desktop";
254
115
  const w = window.innerWidth;
255
116
  if (w < 640) return "mobile";
@@ -286,7 +147,7 @@ async function activateReplay(args) {
286
147
  sessionUid,
287
148
  anonId,
288
149
  startedAt: (/* @__PURE__ */ new Date()).toISOString(),
289
- device: deviceClass2(),
150
+ device: deviceClass(),
290
151
  viewportW: window.innerWidth,
291
152
  viewportH: window.innerHeight,
292
153
  ua: navigator.userAgent?.slice(0, 512),
@@ -314,7 +175,6 @@ async function activateReplay(args) {
314
175
  sessionUid,
315
176
  config: transportCfg,
316
177
  options,
317
- heatmap: null,
318
178
  rrweb: null,
319
179
  replayBlocked: false,
320
180
  paused: true,
@@ -326,22 +186,10 @@ async function activateReplay(args) {
326
186
  };
327
187
  return;
328
188
  }
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
189
  active = {
341
190
  sessionUid,
342
191
  config: transportCfg,
343
192
  options,
344
- heatmap,
345
193
  rrweb: null,
346
194
  replayBlocked: false,
347
195
  paused: false,
@@ -351,6 +199,16 @@ async function activateReplay(args) {
351
199
  clickCount: 0,
352
200
  recordingEnabled: true
353
201
  };
202
+ let lastSessionBump = Date.now();
203
+ const bumpIfStale = () => {
204
+ const now = Date.now();
205
+ if (now - lastSessionBump < 6e4) return;
206
+ lastSessionBump = now;
207
+ bumpSessionActivity();
208
+ };
209
+ for (const ev of ["mousedown", "keydown", "scroll", "touchstart"]) {
210
+ window.addEventListener(ev, bumpIfStale, { passive: true, capture: true });
211
+ }
354
212
  const w = window;
355
213
  const scheduleRrwebStart = (run) => {
356
214
  let fired = false;
@@ -447,8 +305,6 @@ async function activateReplay(args) {
447
305
  active.rrweb?.flush(true);
448
306
  active.rrweb?.stop();
449
307
  active.rrweb = null;
450
- active.heatmap?.stop();
451
- active.heatmap = null;
452
308
  beaconTrace(active.config, "/session/end", {
453
309
  sessionUid: active.sessionUid,
454
310
  endedAt: (/* @__PURE__ */ new Date()).toISOString(),
@@ -462,12 +318,10 @@ async function activateReplay(args) {
462
318
  }
463
319
  var replayApi = {
464
320
  pause() {
465
- active?.heatmap?.pause();
466
321
  active?.rrweb?.pause();
467
322
  if (active) active.paused = true;
468
323
  },
469
324
  resume() {
470
- active?.heatmap?.resume();
471
325
  active?.rrweb?.resume();
472
326
  if (active) active.paused = false;
473
327
  bumpSessionActivity();
@@ -480,7 +334,7 @@ var replayApi = {
480
334
  anonId: getOrCreateAnonId(),
481
335
  userId: active.userId,
482
336
  startedAt: active.startedAt,
483
- device: deviceClass2(),
337
+ device: deviceClass(),
484
338
  viewportW: window.innerWidth,
485
339
  viewportH: window.innerHeight
486
340
  });
@@ -506,17 +360,16 @@ var replayApi = {
506
360
  */
507
361
  notifyRouteChange(url, title) {
508
362
  if (!active) return;
363
+ bumpSessionActivity();
509
364
  const nowBlocked = isPathBlocked(active.options, location.pathname);
510
365
  if (nowBlocked) {
511
366
  if (!active.paused) {
512
- active.heatmap?.pause();
513
367
  active.rrweb?.pause();
514
368
  active.paused = true;
515
369
  }
516
370
  return;
517
371
  }
518
372
  if (active.paused) {
519
- active.heatmap?.resume();
520
373
  active.rrweb?.resume();
521
374
  active.paused = false;
522
375
  }
@@ -1,10 +1,10 @@
1
1
  /**
2
- * Replay/heatmap collector — public option types and internal DTOs.
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
- * This first pass ships heatmap clicks + scroll + page_view events. rrweb
6
- * DOM replay lands in a follow-up (it requires the rrweb peer dependency
7
- * and the S3 ingest path on the server, which is currently 501-stubbed).
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 / heatmap mask. Class is recursively checked up the ancestor chain. */
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/heatmap collector — public option types and internal DTOs.
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
- * This first pass ships heatmap clicks + scroll + page_view events. rrweb
6
- * DOM replay lands in a follow-up (it requires the rrweb peer dependency
7
- * and the S3 ingest path on the server, which is currently 501-stubbed).
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 / heatmap mask. Class is recursively checked up the ancestor chain. */
16
+ /** rrweb mask. Class is recursively checked up the ancestor chain. */
17
17
  mask?: {
18
18
  inputs?: "all" | "sensitive" | "none";
19
19
  class?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@extn/segi-js",
3
- "version": "0.2.9",
3
+ "version": "0.2.12",
4
4
  "description": "Browser JavaScript SDK for the segi error monitoring service",
5
5
  "license": "UNLICENSED",
6
6
  "main": "./dist/index.js",