@tracelog/lib 2.8.1 → 2.8.2

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,6 +1,6 @@
1
1
  const dr = 9e5;
2
2
  const ur = 120, hr = 49152, fr = 100, mr = 500, gr = 200;
3
- const Er = 1e3, Sr = 500, pr = 1e3;
3
+ const Er = 1e3, pr = 500, Sr = 1e3;
4
4
  const b = "data-tlog", At = [
5
5
  "button",
6
6
  "a",
@@ -301,7 +301,7 @@ const xt = () => {
301
301
  default:
302
302
  return Ke;
303
303
  }
304
- }, Yt = 1e3, qt = 50, Jt = "2.8.0", Zt = Jt, dt = () => typeof window < "u" && typeof sessionStorage < "u", es = () => {
304
+ }, Yt = 1e3, qt = 50, Jt = "2.8.1", Zt = Jt, dt = () => typeof window < "u" && typeof sessionStorage < "u", es = () => {
305
305
  try {
306
306
  const r = new URLSearchParams(window.location.search);
307
307
  r.delete(nt);
@@ -1108,6 +1108,21 @@ class et extends _ {
1108
1108
  }
1109
1109
  ), !0) : this.sendQueueSyncInternal(e);
1110
1110
  }
1111
+ /**
1112
+ * Persists events to localStorage for recovery without sending.
1113
+ *
1114
+ * Used when an async send is already in-flight to avoid sending the same
1115
+ * events through two paths (fetch + sendBeacon) with different idempotency tokens.
1116
+ * `ensureBatchMetadata()` assigns a stable token before persisting.
1117
+ * On next page load, `recoverPersistedEvents()` sends with the persisted token.
1118
+ *
1119
+ * @param body - Event queue to persist
1120
+ */
1121
+ persistForRecovery(e) {
1122
+ if (this.shouldSkipSend()) return;
1123
+ const t = this.ensureBatchMetadata(e);
1124
+ this.persistEventsWithFailureCount(t, 0, !0);
1125
+ }
1111
1126
  /**
1112
1127
  * Sends events asynchronously using `fetch()` API with automatic persistence on failure.
1113
1128
  *
@@ -1741,7 +1756,7 @@ class et extends _ {
1741
1756
  }), this.lastPermanentErrorLog = { statusCode: t.statusCode, timestamp: s });
1742
1757
  }
1743
1758
  }
1744
- class Ss extends _ {
1759
+ class ps extends _ {
1745
1760
  bootTime;
1746
1761
  bootTimestamp;
1747
1762
  hasPerformanceNow;
@@ -1890,7 +1905,7 @@ class Ss extends _ {
1890
1905
  };
1891
1906
  }
1892
1907
  }
1893
- const ps = new Set(Object.values(u));
1908
+ const Ss = new Set(Object.values(u));
1894
1909
  class Ts extends _ {
1895
1910
  dataSenders;
1896
1911
  emitter;
@@ -1930,7 +1945,7 @@ class Ts extends _ {
1930
1945
  * @param fetchCredentials - Fetch credentials mode for custom backend. @default 'include'
1931
1946
  */
1932
1947
  constructor(e, t = null, s = {}, n = {}, i, o = "include") {
1933
- super(), this.emitter = t, this.transformers = s, this.timeManager = new Ss(), this.dataSenders = [];
1948
+ super(), this.emitter = t, this.transformers = s, this.timeManager = new ps(), this.dataSenders = [];
1934
1949
  const l = this.get("collectApiUrls");
1935
1950
  l?.saas && this.dataSenders.push(new et(e, "saas", l.saas, s)), l?.custom && this.dataSenders.push(
1936
1951
  new et(
@@ -2061,7 +2076,7 @@ class Ts extends _ {
2061
2076
  a("error", "Event type is required - event will be ignored");
2062
2077
  return;
2063
2078
  }
2064
- if (!ps.has(e)) {
2079
+ if (!Ss.has(e)) {
2065
2080
  a("error", "Invalid event type - event will be ignored", {
2066
2081
  data: { type: e }
2067
2082
  });
@@ -2091,25 +2106,25 @@ class Ts extends _ {
2091
2106
  data: { sessionId: g }
2092
2107
  }), !T && !this.checkRateLimit())
2093
2108
  return;
2094
- const S = e;
2109
+ const p = e;
2095
2110
  if (!T) {
2096
2111
  if (this.sessionEventCounts.total >= 1e3) {
2097
2112
  a("warn", "Session event limit reached", {
2098
2113
  data: {
2099
- type: S,
2114
+ type: p,
2100
2115
  total: this.sessionEventCounts.total,
2101
2116
  limit: 1e3
2102
2117
  }
2103
2118
  });
2104
2119
  return;
2105
2120
  }
2106
- const v = this.getTypeLimitForEvent(S);
2121
+ const v = this.getTypeLimitForEvent(p);
2107
2122
  if (v) {
2108
- const he = this.sessionEventCounts[S];
2123
+ const he = this.sessionEventCounts[p];
2109
2124
  if (he !== void 0 && he >= v) {
2110
2125
  a("warn", "Session event type limit reached", {
2111
2126
  data: {
2112
- type: S,
2127
+ type: p,
2113
2128
  count: he,
2114
2129
  limit: v
2115
2130
  }
@@ -2118,13 +2133,13 @@ class Ts extends _ {
2118
2133
  }
2119
2134
  }
2120
2135
  }
2121
- if (S === u.CUSTOM && o?.name) {
2136
+ if (p === u.CUSTOM && o?.name) {
2122
2137
  const v = this.get("config")?.maxSameEventPerMinute ?? 60;
2123
2138
  if (!this.checkPerEventRateLimit(o.name, v))
2124
2139
  return;
2125
2140
  }
2126
- const Ue = S === u.SESSION_START, K = t || this.get("pageUrl"), x = this.buildEventPayload({
2127
- type: S,
2141
+ const Ue = p === u.SESSION_START, K = t || this.get("pageUrl"), x = this.buildEventPayload({
2142
+ type: p,
2128
2143
  page_url: K,
2129
2144
  from_page_url: s,
2130
2145
  scroll_data: n,
@@ -2152,7 +2167,7 @@ class Ts extends _ {
2152
2167
  }
2153
2168
  if (!this.isDuplicateEvent(x)) {
2154
2169
  if (this.get("mode") === ie.QA) {
2155
- if (S === u.CUSTOM && o) {
2170
+ if (p === u.CUSTOM && o) {
2156
2171
  a("info", `Custom Event: ${o.name}`, {
2157
2172
  visibility: "qa",
2158
2173
  data: {
@@ -2162,7 +2177,7 @@ class Ts extends _ {
2162
2177
  }), this.emitEvent(x);
2163
2178
  return;
2164
2179
  }
2165
- if (S === u.VIEWPORT_VISIBLE && d) {
2180
+ if (p === u.VIEWPORT_VISIBLE && d) {
2166
2181
  const v = d.name || d.id || d.selector;
2167
2182
  a("info", `Viewport Visible: ${v}`, {
2168
2183
  visibility: "qa",
@@ -2178,7 +2193,7 @@ class Ts extends _ {
2178
2193
  }
2179
2194
  }
2180
2195
  if (this.addToQueue(x), !T) {
2181
- this.sessionEventCounts.total++, this.sessionEventCounts[S] !== void 0 && this.sessionEventCounts[S]++;
2196
+ this.sessionEventCounts.total++, this.sessionEventCounts[p] !== void 0 && this.sessionEventCounts[p]++;
2182
2197
  const v = this.get("sessionId");
2183
2198
  v && this.saveSessionCountsDebounced && this.saveSessionCountsDebounced(v);
2184
2199
  }
@@ -2447,6 +2462,13 @@ class Ts extends _ {
2447
2462
  const t = this.buildEventsPayload(), s = [...this.eventsQueue], n = s.map((i) => i.id);
2448
2463
  if (this.dataSenders.length === 0)
2449
2464
  return this.removeProcessedEvents(n), this.clearSendTimeout(), this.emitEventsQueue(t), e ? !0 : Promise.resolve(!0);
2465
+ if (e && this.sendInProgress) {
2466
+ for (const i of this.dataSenders)
2467
+ i.persistForRecovery(t);
2468
+ return a("debug", "Sync flush deferred: async send in progress, events persisted for recovery", {
2469
+ data: { eventCount: n.length }
2470
+ }), !0;
2471
+ }
2450
2472
  if (e) {
2451
2473
  const o = this.dataSenders.map((l) => l.sendEventsQueueSync(t)).some((l) => l);
2452
2474
  return o ? (this.removeProcessedEvents(n), this.clearSendTimeout(), this.emitEventsQueue(t)) : (this.clearSendTimeout(), a("debug", "Sync flush complete failure, events kept in queue for retry", {
@@ -2546,9 +2568,9 @@ class Ts extends _ {
2546
2568
  ...e.page_view && { page_view: e.page_view },
2547
2569
  ...o && { utm: o }
2548
2570
  };
2549
- const c = this.get("collectApiUrls"), d = !!c?.custom, f = !!c?.saas, g = d || f, T = d && f, S = this.transformers.beforeSend;
2550
- if (S && (!g || d && !T)) {
2551
- const K = gt(l, S, "EventManager");
2571
+ const c = this.get("collectApiUrls"), d = !!c?.custom, f = !!c?.saas, g = d || f, T = d && f, p = this.transformers.beforeSend;
2572
+ if (p && (!g || d && !T)) {
2573
+ const K = gt(l, p, "EventManager");
2552
2574
  if (K === null)
2553
2575
  return null;
2554
2576
  l = K;
@@ -3816,11 +3838,11 @@ class As extends _ {
3816
3838
  const c = this.getViewportHeight(t), d = this.getScrollHeight(t), f = this.getScrollDirection(i, s), g = this.calculateScrollDepth(i, d, c);
3817
3839
  let T;
3818
3840
  n > 0 ? T = o - n : e.firstScrollEventTime !== null ? T = o - e.firstScrollEventTime : T = 250;
3819
- const S = Math.round(l / T * 1e3);
3841
+ const p = Math.round(l / T * 1e3);
3820
3842
  return g > e.maxDepthReached && (e.maxDepthReached = g), e.lastScrollPos = i, {
3821
3843
  depth: g,
3822
3844
  direction: f,
3823
- velocity: S,
3845
+ velocity: p,
3824
3846
  max_depth_reached: e.maxDepthReached
3825
3847
  };
3826
3848
  }
@@ -5088,8 +5110,8 @@ class Os extends _ {
5088
5110
  }
5089
5111
  }
5090
5112
  const k = [], M = [];
5091
- let D = null, h = null, R = !1, p = !1, P = null;
5092
- const Ps = async (r) => typeof window > "u" || typeof document > "u" ? { sessionId: "" } : (p = !1, window.__traceLogDisabled === !0 ? { sessionId: "" } : h ? { sessionId: h.getSessionId() ?? "" } : (R && P || (R = !0, P = (async () => {
5113
+ let D = null, h = null, R = !1, S = !1, P = null;
5114
+ const Ps = async (r) => typeof window > "u" || typeof document > "u" ? { sessionId: "" } : (S = !1, window.__traceLogDisabled === !0 ? { sessionId: "" } : h ? { sessionId: h.getSessionId() ?? "" } : (R && P || (R = !0, P = (async () => {
5093
5115
  try {
5094
5116
  const e = us(r ?? {}), t = new Os();
5095
5117
  try {
@@ -5121,7 +5143,7 @@ const Ps = async (r) => typeof window > "u" || typeof document > "u" ? { session
5121
5143
  if (!(typeof window > "u" || typeof document > "u")) {
5122
5144
  if (!h)
5123
5145
  throw new Error("[TraceLog] TraceLog not initialized. Please call init() first.");
5124
- if (p)
5146
+ if (S)
5125
5147
  throw new Error("[TraceLog] Cannot send events while TraceLog is being destroyed");
5126
5148
  h.sendCustomEvent(r, e);
5127
5149
  }
@@ -5152,7 +5174,7 @@ function Us(r, e) {
5152
5174
  t !== -1 && M.splice(t, 1), M.push({ hook: r, fn: e });
5153
5175
  return;
5154
5176
  }
5155
- if (p)
5177
+ if (S)
5156
5178
  throw new Error("[TraceLog] Cannot set transformers while TraceLog is being destroyed");
5157
5179
  r === "beforeSend" ? h.setTransformer("beforeSend", e) : h.setTransformer("beforeBatch", e);
5158
5180
  }
@@ -5164,7 +5186,7 @@ const Hs = (r) => {
5164
5186
  e !== -1 && M.splice(e, 1);
5165
5187
  return;
5166
5188
  }
5167
- if (p)
5189
+ if (S)
5168
5190
  throw new Error("[TraceLog] Cannot remove transformers while TraceLog is being destroyed");
5169
5191
  h.removeTransformer(r);
5170
5192
  }
@@ -5176,7 +5198,7 @@ const Hs = (r) => {
5176
5198
  D = r;
5177
5199
  return;
5178
5200
  }
5179
- if (p)
5201
+ if (S)
5180
5202
  throw new Error("[TraceLog] Cannot set custom headers while TraceLog is being destroyed");
5181
5203
  h.setCustomHeaders(r);
5182
5204
  }
@@ -5186,23 +5208,23 @@ const Hs = (r) => {
5186
5208
  D = null;
5187
5209
  return;
5188
5210
  }
5189
- if (p)
5211
+ if (S)
5190
5212
  throw new Error("[TraceLog] Cannot remove custom headers while TraceLog is being destroyed");
5191
5213
  h.removeCustomHeaders();
5192
5214
  }
5193
5215
  }, $s = () => typeof window > "u" || typeof document > "u" ? !1 : h !== null, Bs = () => typeof window > "u" || typeof document > "u" || !h ? null : h.getSessionId(), Ws = () => {
5194
5216
  if (!(typeof window > "u" || typeof document > "u")) {
5195
- if (p)
5217
+ if (S)
5196
5218
  throw new Error("[TraceLog] Destroy operation already in progress");
5197
5219
  if (!h) {
5198
- p = !1;
5220
+ S = !1;
5199
5221
  return;
5200
5222
  }
5201
- p = !0;
5223
+ S = !0;
5202
5224
  try {
5203
- h.destroy(), h = null, R = !1, P = null, k.length = 0, M.length = 0, D = null, p = !1;
5225
+ h.destroy(), h = null, R = !1, P = null, k.length = 0, M.length = 0, D = null, S = !1;
5204
5226
  } catch (r) {
5205
- h = null, R = !1, P = null, k.length = 0, M.length = 0, D = null, p = !1, a("warn", "Error during destroy, forced cleanup completed", { error: r });
5227
+ h = null, R = !1, P = null, k.length = 0, M.length = 0, D = null, S = !1, a("warn", "Error during destroy, forced cleanup completed", { error: r });
5206
5228
  }
5207
5229
  }
5208
5230
  }, Xs = (r) => {
@@ -5211,7 +5233,7 @@ const Hs = (r) => {
5211
5233
  if (!(typeof window > "u" || typeof document > "u")) {
5212
5234
  if (!h)
5213
5235
  throw new Error("[TraceLog] TraceLog not initialized. Please call init() first.");
5214
- if (p)
5236
+ if (S)
5215
5237
  throw new Error("[TraceLog] Cannot update metadata while TraceLog is being destroyed");
5216
5238
  h.updateGlobalMetadata(r);
5217
5239
  }
@@ -5219,7 +5241,7 @@ const Hs = (r) => {
5219
5241
  if (!(typeof window > "u" || typeof document > "u")) {
5220
5242
  if (!h)
5221
5243
  throw new Error("[TraceLog] TraceLog not initialized. Please call init() first.");
5222
- if (p)
5244
+ if (S)
5223
5245
  throw new Error("[TraceLog] Cannot update metadata while TraceLog is being destroyed");
5224
5246
  h.mergeGlobalMetadata(r);
5225
5247
  }
@@ -5233,7 +5255,7 @@ const Hs = (r) => {
5233
5255
  a("warn", "identify() userId exceeds 256 characters");
5234
5256
  return;
5235
5257
  }
5236
- if (p) {
5258
+ if (S) {
5237
5259
  a("warn", "Cannot identify while TraceLog is being destroyed");
5238
5260
  return;
5239
5261
  }
@@ -5260,7 +5282,7 @@ const Hs = (r) => {
5260
5282
  }
5261
5283
  return;
5262
5284
  }
5263
- if (p)
5285
+ if (S)
5264
5286
  throw new Error("[TraceLog] Cannot reset identity while TraceLog is being destroyed");
5265
5287
  await h.resetIdentity();
5266
5288
  }
@@ -5282,9 +5304,9 @@ const Hs = (r) => {
5282
5304
  identify: zs,
5283
5305
  resetIdentity: Qs
5284
5306
  };
5285
- var Le, C, G, St, le, pt = -1, V = function(r) {
5307
+ var Le, C, G, pt, le, St = -1, V = function(r) {
5286
5308
  addEventListener("pageshow", (function(e) {
5287
- e.persisted && (pt = e.timeStamp, r(e));
5309
+ e.persisted && (St = e.timeStamp, r(e));
5288
5310
  }), !0);
5289
5311
  }, De = function() {
5290
5312
  var r = self.performance && performance.getEntriesByType && performance.getEntriesByType("navigation")[0];
@@ -5294,7 +5316,7 @@ var Le, C, G, St, le, pt = -1, V = function(r) {
5294
5316
  return r && r.activationStart || 0;
5295
5317
  }, y = function(r, e) {
5296
5318
  var t = De(), s = "navigate";
5297
- return pt >= 0 ? s = "back-forward-cache" : t && (document.prerendering || de() > 0 ? s = "prerender" : document.wasDiscarded ? s = "restore" : t.type && (s = t.type.replace(/_/g, "-"))), { name: r, value: e === void 0 ? -1 : e, rating: "good", delta: 0, entries: [], id: "v4-".concat(Date.now(), "-").concat(Math.floor(8999999999999 * Math.random()) + 1e12), navigationType: s };
5319
+ return St >= 0 ? s = "back-forward-cache" : t && (document.prerendering || de() > 0 ? s = "prerender" : document.wasDiscarded ? s = "restore" : t.type && (s = t.type.replace(/_/g, "-"))), { name: r, value: e === void 0 ? -1 : e, rating: "good", delta: 0, entries: [], id: "v4-".concat(Date.now(), "-").concat(Math.floor(8999999999999 * Math.random()) + 1e12), navigationType: s };
5298
5320
  }, F = function(r, e, t) {
5299
5321
  try {
5300
5322
  if (PerformanceObserver.supportedEntryTypes.includes(r)) {
@@ -5380,9 +5402,9 @@ var Le, C, G, St, le, pt = -1, V = function(r) {
5380
5402
  }));
5381
5403
  })), setTimeout(t, 0));
5382
5404
  })));
5383
- }, It = 0, Se = 1 / 0, J = 0, qs = function(r) {
5405
+ }, It = 0, pe = 1 / 0, J = 0, qs = function(r) {
5384
5406
  r.forEach((function(e) {
5385
- e.interactionId && (Se = Math.min(Se, e.interactionId), J = Math.max(J, e.interactionId), It = J ? (J - Se) / 7 + 1 : 0);
5407
+ e.interactionId && (pe = Math.min(pe, e.interactionId), J = Math.max(J, e.interactionId), It = J ? (J - pe) / 7 + 1 : 0);
5386
5408
  }));
5387
5409
  }, vt = function() {
5388
5410
  return Le ? It : performance.interactionCount || 0;
@@ -5429,7 +5451,7 @@ var Le, C, G, St, le, pt = -1, V = function(r) {
5429
5451
  _t = vt(), A.length = 0, te.clear(), n = y("INP"), s = w(r, n, Re, e.reportAllChanges);
5430
5452
  })));
5431
5453
  })));
5432
- }, Ne = [2500, 4e3], pe = {}, rr = function(r, e) {
5454
+ }, Ne = [2500, 4e3], Se = {}, rr = function(r, e) {
5433
5455
  e = e || {}, Q((function() {
5434
5456
  var t, s = Ve(), n = y("LCP"), i = function(c) {
5435
5457
  e.reportAllChanges || (c = c.slice(-1)), c.forEach((function(d) {
@@ -5439,7 +5461,7 @@ var Le, C, G, St, le, pt = -1, V = function(r) {
5439
5461
  if (o) {
5440
5462
  t = w(r, n, Ne, e.reportAllChanges);
5441
5463
  var l = ue((function() {
5442
- pe[n.id] || (i(o.takeRecords()), o.disconnect(), pe[n.id] = !0, t(!0));
5464
+ Se[n.id] || (i(o.takeRecords()), o.disconnect(), Se[n.id] = !0, t(!0));
5443
5465
  }));
5444
5466
  ["keydown", "click"].forEach((function(c) {
5445
5467
  addEventListener(c, (function() {
@@ -5447,7 +5469,7 @@ var Le, C, G, St, le, pt = -1, V = function(r) {
5447
5469
  }), { once: !0, capture: !0 });
5448
5470
  })), z(l), V((function(c) {
5449
5471
  n = y("LCP"), t = w(r, n, Ne, e.reportAllChanges), ke((function() {
5450
- n.value = performance.now() - c.timeStamp, pe[n.id] = !0, t(!0);
5472
+ n.value = performance.now() - c.timeStamp, Se[n.id] = !0, t(!0);
5451
5473
  }));
5452
5474
  }));
5453
5475
  }
@@ -5468,9 +5490,9 @@ var Le, C, G, St, le, pt = -1, V = function(r) {
5468
5490
  })));
5469
5491
  }));
5470
5492
  }, W = { passive: !0, capture: !0 }, or = /* @__PURE__ */ new Date(), rt = function(r, e) {
5471
- C || (C = e, G = r, St = /* @__PURE__ */ new Date(), bt(removeEventListener), wt());
5493
+ C || (C = e, G = r, pt = /* @__PURE__ */ new Date(), bt(removeEventListener), wt());
5472
5494
  }, wt = function() {
5473
- if (G >= 0 && G < St - or) {
5495
+ if (G >= 0 && G < pt - or) {
5474
5496
  var r = { entryType: "first-input", name: C.type, target: C.target, cancelable: C.cancelable, startTime: C.timeStamp, processingStart: C.timeStamp + G };
5475
5497
  le.forEach((function(e) {
5476
5498
  e(r);
@@ -5534,14 +5556,14 @@ export {
5534
5556
  u as EventType,
5535
5557
  vr as InitializationTimeoutError,
5536
5558
  N as IntegrationValidationError,
5537
- pr as MAX_ARRAY_LENGTH,
5559
+ Sr as MAX_ARRAY_LENGTH,
5538
5560
  mr as MAX_CUSTOM_EVENT_ARRAY_SIZE,
5539
5561
  fr as MAX_CUSTOM_EVENT_KEYS,
5540
5562
  ur as MAX_CUSTOM_EVENT_NAME_LENGTH,
5541
5563
  hr as MAX_CUSTOM_EVENT_STRING_SIZE,
5542
5564
  gr as MAX_NESTED_OBJECT_KEYS,
5543
5565
  Er as MAX_STRING_LENGTH,
5544
- Sr as MAX_STRING_LENGTH_IN_ARRAY,
5566
+ pr as MAX_STRING_LENGTH_IN_ARRAY,
5545
5567
  ie as Mode,
5546
5568
  lt as PII_PATTERNS,
5547
5569
  O as PermanentError,