@levo-so/insights 0.1.109 → 0.1.113

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.
@@ -5,12 +5,10 @@
5
5
  export declare const STORAGE_KEYS: {
6
6
  /** Legacy: Lock key for identify operation (no longer used internally) */
7
7
  readonly identify: "lock:lv_aud_identify";
8
- /** Key for tracking open tab count */
8
+ /** Namespace prefix for per-tab heartbeat keys (`${open_tabs}:${tabId}`) — see tracking/currentTab.ts */
9
9
  readonly open_tabs: "lv_insights_ot";
10
10
  /** Key for current tab ID (sessionStorage) */
11
11
  readonly current_tab: "lv_insights_ct";
12
- /** Key for all tabs registry */
13
- readonly all_tabs: "lv_insights_at";
14
12
  /** Device JWT for direct mode (1yr expiry, rotated on /welcome, analytics-only scope) */
15
13
  readonly device_token: "lv_aud_device";
16
14
  /** Session JWT for direct mode (1yr expiry, rotated on /welcome, analytics-only scope) */
@@ -25,4 +23,13 @@ export declare const STORAGE_KEYS: {
25
23
  * ends, preventing stale isIdentified from blocking /welcome across restarts.
26
24
  */
27
25
  readonly session_flags: "lv_insights_sf";
26
+ /**
27
+ * sessionStorage key for the last non-direct attribution (channel/referrer/UTM)
28
+ * of the CURRENT browsing session. Used to carry the original source forward when
29
+ * the visitor-source library would otherwise report "internalLink" for same-site
30
+ * continued browsing after the server cuts a session at its 30-min idle threshold.
31
+ * Session-scoped on purpose — see utils/getReferrer.ts (a long-lived store would
32
+ * mis-attribute a fresh visit days later).
33
+ */
34
+ readonly attribution: "lv_insights_attr";
28
35
  };
package/dist/index.js CHANGED
@@ -1,24 +1,24 @@
1
- import { A as e, C as t, D as n, E as r, F as i, M as a, N as o, P as s, S as c, T as l, _ as u, c as d, f, g as p, h as m, i as h, j as g, k as _, l as v, m as y, n as b, o as x, s as S, u as C, v as w, w as T } from "./textSelection-eSp_MmZH.js";
2
- import { AnalyticsEvents as E, getLevoError as D } from "@levo-so/core";
3
- import { getVisitorSource as O } from "@analytics/visitor-source";
4
- import k from "get-user-locale";
5
- import { Analytics as A } from "analytics";
1
+ import { A as e, C as t, D as n, E as r, F as i, I as a, L as o, M as s, N as c, O as l, P as u, T as d, _ as f, c as p, d as m, g as h, h as g, i as _, j as v, l as y, n as b, o as x, p as S, s as C, u as w, v as T, w as E, y as ee } from "./textSelection-Cwq6lqKg.js";
2
+ import { AnalyticsEvents as D, getLevoError as O } from "@levo-so/core";
3
+ import { getVisitorSource as k } from "@analytics/visitor-source";
4
+ import A from "get-user-locale";
5
+ import { Analytics as j } from "analytics";
6
6
  //#region src/utils/rageclick.ts
7
- var j = 30, M = 1e3, N = 3, P = [], F = (e, t, n) => {
8
- let r = P[P.length - 1];
9
- if (r && Math.abs(e - r.x) + Math.abs(t - r.y) < j && n - r.timestamp < M) {
10
- if (P.push({
7
+ var M = 30, N = 1e3, P = 3, F = [], I = (e, t, n) => {
8
+ let r = F[F.length - 1];
9
+ if (r && Math.abs(e - r.x) + Math.abs(t - r.y) < M && n - r.timestamp < N) {
10
+ if (F.push({
11
11
  x: e,
12
12
  y: t,
13
13
  timestamp: n
14
- }), P.length >= N) return P = [], !0;
15
- } else P = [{
14
+ }), F.length >= P) return F = [], !0;
15
+ } else F = [{
16
16
  x: e,
17
17
  y: t,
18
18
  timestamp: n
19
19
  }];
20
20
  return !1;
21
- }, I = (e) => {
21
+ }, L = (e) => {
22
22
  let t = e, n = 0;
23
23
  for (; t && n < 10;) {
24
24
  let e = t.getAttribute?.("href");
@@ -26,8 +26,8 @@ var j = 30, M = 1e3, N = 3, P = [], F = (e, t, n) => {
26
26
  t = t.parentElement, n++;
27
27
  }
28
28
  return null;
29
- }, L = (e) => {
30
- let t = e.closest("a[href]"), n = t?.getAttribute("href") || I(e);
29
+ }, R = (e) => {
30
+ let t = e.closest("a[href]"), n = t?.getAttribute("href") || L(e);
31
31
  if (!n) return {
32
32
  type: null,
33
33
  href: null
@@ -54,15 +54,15 @@ var j = 30, M = 1e3, N = 3, P = [], F = (e, t, n) => {
54
54
  href: n
55
55
  };
56
56
  } catch (e) {
57
- return console.warn("[insights] Failed to classify link", D(e)), {
57
+ return console.warn("[insights] Failed to classify link", O(e)), {
58
58
  type: "internal",
59
59
  href: n
60
60
  };
61
61
  }
62
- }, R = (e) => {
62
+ }, z = (e) => {
63
63
  if (typeof window > "u") return () => {};
64
64
  let t = 0, n = null, r = (e, t = !1, n) => {
65
- let r = n ?? v(e), i = "clientX" in e ? e.clientX : e.changedTouches?.[0]?.clientX ?? 0, a = "clientY" in e ? e.clientY : e.changedTouches?.[0]?.clientY ?? 0, o = {
65
+ let r = n ?? w(e), i = "clientX" in e ? e.clientX : e.changedTouches?.[0]?.clientX ?? 0, a = "clientY" in e ? e.clientY : e.changedTouches?.[0]?.clientY ?? 0, o = {
66
66
  hostname: window.location.hostname,
67
67
  pathname: window.location.pathname,
68
68
  page_title: document.title,
@@ -71,9 +71,9 @@ var j = 30, M = 1e3, N = 3, P = [], F = (e, t, n) => {
71
71
  event_type: t ? "dblclick" : "click",
72
72
  button_type: "button" in e ? e.button : 0
73
73
  };
74
- return r instanceof HTMLElement && (o.rage_click = F(i, a, Date.now()), o = {
74
+ return r instanceof HTMLElement && (o.rage_click = I(i, a, Date.now()), o = {
75
75
  ...o,
76
- ...d(r)
76
+ ...y(r)
77
77
  }), o;
78
78
  }, i = (n, i, a) => {
79
79
  try {
@@ -86,25 +86,25 @@ var j = 30, M = 1e3, N = 3, P = [], F = (e, t, n) => {
86
86
  is_navigation: !0
87
87
  });
88
88
  } catch (e) {
89
- console.error("[insights] Failed to track navigation click", D(e));
89
+ console.error("[insights] Failed to track navigation click", O(e));
90
90
  }
91
91
  }, a = (n, i = !1, a) => {
92
92
  try {
93
- let o = a ?? v(n);
93
+ let o = a ?? w(n);
94
94
  if (!o || Date.now() - t < 300) return;
95
- let s = r(n, i, o), c = L(o);
95
+ let s = r(n, i, o), c = R(o);
96
96
  e.track("page.click", {
97
97
  ...s,
98
98
  href: c.href || void 0,
99
99
  link_type: c.type || void 0
100
100
  });
101
101
  } catch (e) {
102
- console.error("[insights] Failed to track click", D(e));
102
+ console.error("[insights] Failed to track click", O(e));
103
103
  }
104
104
  }, o = (e) => {
105
- let t = v(e);
105
+ let t = w(e);
106
106
  if (!t) return;
107
- let r = L(t);
107
+ let r = R(t);
108
108
  if (r.type === "external" || r.type === "download") {
109
109
  i(e, r, t);
110
110
  return;
@@ -117,13 +117,13 @@ var j = 30, M = 1e3, N = 3, P = [], F = (e, t, n) => {
117
117
  a(e, !1, t), n = null;
118
118
  }, 250);
119
119
  }, s = (e) => {
120
- n &&= (clearTimeout(n), null), a(e, !0, v(e) ?? void 0);
120
+ n &&= (clearTimeout(n), null), a(e, !0, w(e) ?? void 0);
121
121
  }, c = (n) => {
122
122
  try {
123
- let i = v(n);
123
+ let i = w(n);
124
124
  if (!i) return;
125
125
  t = Date.now();
126
- let a = L(i);
126
+ let a = R(i);
127
127
  if (a.type === "external" || a.type === "download") {
128
128
  let t = r(n, !1, i);
129
129
  e.track("page.click", {
@@ -143,52 +143,52 @@ var j = 30, M = 1e3, N = 3, P = [], F = (e, t, n) => {
143
143
  is_touch: !0
144
144
  });
145
145
  } catch (e) {
146
- console.error("[insights] Failed to track touch", D(e));
146
+ console.error("[insights] Failed to track touch", O(e));
147
147
  }
148
148
  };
149
149
  return document.addEventListener("click", o), document.addEventListener("dblclick", s), document.addEventListener("touchend", c, { passive: !0 }), () => {
150
150
  document.removeEventListener("click", o), document.removeEventListener("dblclick", s), document.removeEventListener("touchend", c), n && clearTimeout(n);
151
151
  };
152
- }, z = (e) => {
152
+ }, B = (e) => {
153
153
  if (typeof window > "u") return () => {};
154
154
  let t = (t) => (n) => {
155
155
  try {
156
- let r = C(n, t);
156
+ let r = m(n, t);
157
157
  r && e.track("page.copy", r);
158
158
  } catch (e) {
159
- console.error(`[insights] Failed to track clipboard ${t}`, D(e));
159
+ console.error(`[insights] Failed to track clipboard ${t}`, O(e));
160
160
  }
161
161
  }, n = t("copy"), r = t("cut");
162
162
  return document.addEventListener("copy", n, { capture: !0 }), document.addEventListener("cut", r, { capture: !0 }), () => {
163
163
  document.removeEventListener("copy", n, { capture: !0 }), document.removeEventListener("cut", r, { capture: !0 });
164
164
  };
165
- }, B = (e) => {
165
+ }, V = (e) => {
166
166
  if (typeof window > "u") return () => {};
167
167
  let t = (t) => (n) => {
168
168
  try {
169
- let r = C(n, t);
169
+ let r = m(n, t);
170
170
  if (r) {
171
171
  let n = t === "submit" ? "form.submit" : "form.change";
172
172
  e.track(n, r);
173
173
  }
174
174
  } catch (e) {
175
- console.error(`[insights] Failed to track form ${t}`, D(e));
175
+ console.error(`[insights] Failed to track form ${t}`, O(e));
176
176
  }
177
177
  }, n = t("change"), r = t("submit");
178
178
  return document.addEventListener("change", n, { capture: !0 }), document.addEventListener("submit", r, { capture: !0 }), () => {
179
179
  document.removeEventListener("change", n, { capture: !0 }), document.removeEventListener("submit", r, { capture: !0 });
180
180
  };
181
- }, V = (e) => {
181
+ }, H = (e) => {
182
182
  if (typeof window > "u") return () => {};
183
- let { page_id: t } = a.get(), n = t || "", r = typeof window < "u" ? window.location.href : "", i = () => {
183
+ let { page_id: t } = u.get(), n = t || "", r = typeof window < "u" ? window.location.href : "", i = () => {
184
184
  try {
185
185
  e.page({ resource: "page" });
186
186
  } catch (e) {
187
- console.error("[insights] Failed to send page view", D(e));
187
+ console.error("[insights] Failed to send page view", O(e));
188
188
  }
189
189
  };
190
190
  if (t) {
191
- let e = a.listen((e, t, r) => {
191
+ let e = u.listen((e, t, r) => {
192
192
  let { page_id: a } = e;
193
193
  r === "page_id" && a && n !== a && (n = a, i());
194
194
  });
@@ -209,7 +209,7 @@ var j = 30, M = 1e3, N = 3, P = [], F = (e, t, n) => {
209
209
  window.removeEventListener("popstate", e), window.history.pushState = t;
210
210
  };
211
211
  }
212
- }, H = async (e, t) => {
212
+ }, U = async (e, t) => {
213
213
  let n = null;
214
214
  try {
215
215
  let r = new AbortController();
@@ -223,7 +223,7 @@ var j = 30, M = 1e3, N = 3, P = [], F = (e, t, n) => {
223
223
  } finally {
224
224
  n && clearTimeout(n);
225
225
  }
226
- }, U = async (e) => {
226
+ }, W = async (e) => {
227
227
  let { mode: t = "auto", insightsUrl: n, directInsightsUrl: r, pingTimeout: i = 1e3, debug: a = !1 } = e;
228
228
  if (a) return {
229
229
  mode: "proxy",
@@ -238,7 +238,7 @@ var j = 30, M = 1e3, N = 3, P = [], F = (e, t, n) => {
238
238
  mode: "proxy",
239
239
  url: n ?? null
240
240
  };
241
- let [s, c] = await Promise.all([n ? H(`${n}/ping`, o) : Promise.resolve(!1), r ? H(`${r}/ping`, o) : Promise.resolve(!1)]);
241
+ let [s, c] = await Promise.all([n ? U(`${n}/ping`, o) : Promise.resolve(!1), r ? U(`${r}/ping`, o) : Promise.resolve(!1)]);
242
242
  return s ? {
243
243
  mode: "proxy",
244
244
  url: n ?? null
@@ -249,21 +249,61 @@ var j = 30, M = 1e3, N = 3, P = [], F = (e, t, n) => {
249
249
  mode: "proxy",
250
250
  url: n ?? null
251
251
  };
252
- }, W = () => {
253
- if (typeof document > "u") return {};
254
- let { type: e, referrer: t, data: n = {} } = O({
252
+ }, G = () => {
253
+ try {
254
+ let e = window.sessionStorage.getItem(s.attribution);
255
+ if (!e) return;
256
+ let t = JSON.parse(e);
257
+ return {
258
+ type: t.type,
259
+ referrer: t.referrer,
260
+ data: t.data
261
+ };
262
+ } catch {
263
+ return;
264
+ }
265
+ }, K = (e) => {
266
+ try {
267
+ window.sessionStorage.setItem(s.attribution, JSON.stringify(e));
268
+ } catch {}
269
+ }, q = () => {
270
+ if (typeof document > "u") return {
271
+ type: "",
272
+ referrer: {},
273
+ data: {},
274
+ raw: ""
275
+ };
276
+ let { type: e, referrer: t, data: n = {} } = k({
255
277
  referrer: document.referrer,
256
278
  currentUrl: window.location.href,
257
279
  mapper: null
258
280
  }), r = Object.keys(n);
259
281
  for (let e of r) n[e] && Array.isArray(n[e]) && n[e].length > 0 && (n[e] = n[e]?.[0]);
260
- return {
282
+ if (e === "internalLink") {
283
+ let e = G();
284
+ return e ? {
285
+ type: e.type,
286
+ referrer: e.referrer ?? {},
287
+ data: e.data ?? {},
288
+ raw: document.referrer
289
+ } : {
290
+ type: "direct",
291
+ referrer: t ?? {},
292
+ data: n,
293
+ raw: document.referrer
294
+ };
295
+ }
296
+ return e && e !== "direct" && K({
261
297
  type: e,
262
298
  referrer: t,
299
+ data: n
300
+ }), {
301
+ type: e,
302
+ referrer: t ?? {},
263
303
  data: n,
264
304
  raw: document.referrer
265
305
  };
266
- }, G = () => new Promise((e, t) => {
306
+ }, J = () => new Promise((e, t) => {
267
307
  let n = "Unknown";
268
308
  function r(t) {
269
309
  e({
@@ -360,12 +400,12 @@ var j = 30, M = 1e3, N = 3, P = [], F = (e, t, n) => {
360
400
  o() ? (n = "Safari", f()) : s() ? (n = i(), g()) : c() ? (n = "Firefox", _()) : l() ? (n = "Internet Explorer", v()) : t(/* @__PURE__ */ Error("detectIncognito cannot determine the browser"));
361
401
  }
362
402
  y();
363
- }), K = () => {
403
+ }), Y = () => {
364
404
  let e = document.createElement("canvas"), t = e?.getContext("webgl") || e?.getContext("experimental-webgl");
365
405
  if (!t) return null;
366
406
  let n = t?.getExtension("WEBGL_debug_renderer_info");
367
407
  return n ? t?.getParameter(n.UNMASKED_RENDERER_WEBGL) : null;
368
- }, q = async () => {
408
+ }, te = async () => {
369
409
  let e = "", t = "", n = !1, r = !1, i = {};
370
410
  if (!window) return {
371
411
  locale: e,
@@ -375,7 +415,7 @@ var j = 30, M = 1e3, N = 3, P = [], F = (e, t, n) => {
375
415
  properties: i
376
416
  };
377
417
  let [a, o, s] = await Promise.allSettled([
378
- G(),
418
+ J(),
379
419
  navigator.getBattery?.() ?? Promise.reject(),
380
420
  "mediaDevices" in navigator ? navigator.mediaDevices.enumerateDevices() : Promise.reject()
381
421
  ]);
@@ -387,7 +427,7 @@ var j = 30, M = 1e3, N = 3, P = [], F = (e, t, n) => {
387
427
  t = Intl.DateTimeFormat().resolvedOptions().timeZone;
388
428
  } catch {}
389
429
  try {
390
- e = k();
430
+ e = A();
391
431
  } catch {}
392
432
  let c = navigator, l = c.connection ?? c.mozConnection ?? c.webkitConnection;
393
433
  if (l && (i.network_type = l.effectiveType), i.width = window?.innerWidth, i.height = window?.innerHeight, i.pixel_ratio = window?.devicePixelRatio, screen && (i.screen_width = screen?.width, i.screen_height = screen?.height, i.color_depth = screen?.colorDepth, i.orientation = screen?.orientation?.type), o.status === "fulfilled") {
@@ -398,129 +438,129 @@ var j = 30, M = 1e3, N = 3, P = [], F = (e, t, n) => {
398
438
  let e = s.value;
399
439
  i.camera_supported = e.some((e) => e?.kind === "videoinput"), i.microphone_supported = e.some((e) => e?.kind === "audioinput");
400
440
  }
401
- return i.gpu = K(), {
441
+ return i.gpu = Y(), {
402
442
  locale: e,
403
443
  timezone: t,
404
444
  dark_mode: n,
405
445
  private_mode: r,
406
446
  properties: i
407
447
  };
408
- }, J = (e, t) => {
448
+ }, X = (e, t) => {
409
449
  if (e.get()) return t(), () => {};
410
450
  let n = e.listen((e) => {
411
451
  e && (n(), t());
412
452
  });
413
453
  return n;
414
- }, Y = (n, i, a) => {
415
- let { debug: s } = o();
416
- if (!n) return null;
417
- let c = {
418
- name: n.workspace,
454
+ }, ne = (t, r, a) => {
455
+ let { debug: o } = i();
456
+ if (!t) return null;
457
+ let s = {
458
+ name: t.workspace,
419
459
  track: ({ payload: e }) => {
420
460
  if (!a()) return;
421
461
  let t = e?.properties || {};
422
- i(e.event, t);
462
+ r(e.event, t);
423
463
  },
424
464
  page: ({ payload: e }) => {
425
- a() && (r(), i("page.view", {
465
+ a() && (n(), r("page.view", {
426
466
  ...e?.properties || {},
427
467
  resource: "page"
428
468
  }));
429
469
  },
430
- identify: ({ payload: r }) => {
470
+ identify: ({ payload: n }) => {
431
471
  if (!a()) return;
432
- let { workspace: i, site_id: s, debug: c } = o(), { mode: l, isIdentified: u, isIdentifying: d, sessionToken: f, deviceToken: p, insightsBaseUrl: m, identity: h } = _(), v = x(), y = _().allTabs.length;
433
- if (!m || !h) return;
472
+ let { workspace: r, site_id: o, debug: s } = i(), { mode: l, isIdentified: u, isIdentifying: f, sessionToken: p, deviceToken: m, insightsBaseUrl: h, identity: g } = e(), _ = x(), y = C();
473
+ if (!h || !g) return;
434
474
  let b = {
435
- private_mode: r.traits?.private_mode ?? h.privateMode ?? !1,
436
- dark_mode: r.traits?.dark_mode ?? h.darkMode ?? !1,
437
- timezone: r.traits?.timezone ?? h.timezone ?? "",
438
- locale: r.traits?.locale ?? h.locale ?? "",
439
- referrer: r.traits?.referrer ?? h.referrer ?? {
475
+ private_mode: n.traits?.private_mode ?? g.privateMode ?? !1,
476
+ dark_mode: n.traits?.dark_mode ?? g.darkMode ?? !1,
477
+ timezone: n.traits?.timezone ?? g.timezone ?? "",
478
+ locale: n.traits?.locale ?? g.locale ?? "",
479
+ referrer: n.traits?.referrer ?? g.referrer ?? {
440
480
  type: "",
441
481
  referrer: {},
442
482
  data: {}
443
483
  },
444
484
  properties: {
445
- ...h.properties,
446
- ...r.traits?.properties
485
+ ...g.properties,
486
+ ...n.traits?.properties
447
487
  },
448
- workspace_id: i ?? "",
449
- site_id: s ?? void 0,
488
+ workspace_id: r ?? "",
489
+ site_id: o ?? void 0,
450
490
  hostname: window.location.hostname,
451
491
  pathname: window.location.pathname,
452
492
  page_title: document.title,
453
493
  url: window.location.href,
454
494
  created_at: (/* @__PURE__ */ new Date()).toISOString(),
455
- tab_id: v ?? "",
495
+ tab_id: _ ?? "",
456
496
  tab_count: y ?? 0
457
497
  };
458
- if (c && console.log("[@levo-so/insights] Identification triggered:", {
498
+ if (s && console.log("[@levo-so/insights] Identification triggered:", {
459
499
  isIdentified: u,
460
- isIdentifying: d,
500
+ isIdentifying: f,
461
501
  welcomeInput: b
462
- }), u || d) return;
463
- if (e({ isIdentifying: !0 }), c) {
464
- console.log("[@levo-so/insights] Debug mode enabled. Mocking session identification."), e({
502
+ }), u || f) return;
503
+ if (v({ isIdentifying: !0 }), s) {
504
+ console.log("[@levo-so/insights] Debug mode enabled. Mocking session identification."), v({
465
505
  isIdentified: !0,
466
506
  isIdentifying: !1,
467
507
  sessionId: "debug-session",
468
508
  deviceId: "debug-device"
469
- }), t(T());
509
+ }), E(d());
470
510
  return;
471
511
  }
472
512
  let S = {
473
513
  Accept: "application/json",
474
514
  "Content-Type": "application/json",
475
- ...g({
476
- sessionToken: f,
477
- deviceToken: p,
515
+ ...c({
516
+ sessionToken: p,
517
+ deviceToken: m,
478
518
  mode: l
479
519
  })
480
520
  };
481
- n.fetch.url(m, !0).url("/v1/insights/event/welcome").options({
521
+ t.fetch.url(h, !0).url("/v1/insights/event/welcome").options({
482
522
  keepalive: !0,
483
523
  credentials: l === "proxy" ? "include" : "omit"
484
- }).headers(S).post(b).json().then((n) => {
524
+ }).headers(S).post(b).json().then((t) => {
485
525
  if (!a()) {
486
- e({ isIdentifying: !1 });
526
+ v({ isIdentifying: !1 });
487
527
  return;
488
528
  }
489
- let { isIdentified: r } = _();
490
- if (r) e({ isIdentifying: !1 });
529
+ let { isIdentified: n } = e();
530
+ if (n) v({ isIdentifying: !1 });
491
531
  else {
492
- let r = {
532
+ let e = {
493
533
  isIdentified: !0,
494
534
  isIdentifying: !1
495
535
  };
496
- if (n?.content?.data?.session && (r.sessionId = n?.content?.data?.session), n?.content?.data?.device && (r.deviceId = n?.content?.data?.device), l === "direct" && n?.content?.meta) {
497
- let e = n.content.meta;
498
- e.device_token && (r.deviceToken = e.device_token), e.session_token && (r.sessionToken = e.session_token);
536
+ if (t?.content?.data?.session && (e.sessionId = t?.content?.data?.session), t?.content?.data?.device && (e.deviceId = t?.content?.data?.device), l === "direct" && t?.content?.meta) {
537
+ let n = t.content.meta;
538
+ n.device_token && (e.deviceToken = n.device_token), n.session_token && (e.sessionToken = n.session_token);
499
539
  }
500
- e(r), t(T());
540
+ v(e), E(d());
501
541
  }
502
- }).catch((t) => {
503
- _().isIdentified || n.logger?.error("/welcome failed with error", { exception: D(t) }), e({ isIdentifying: !1 });
542
+ }).catch((n) => {
543
+ e().isIdentified || t.logger?.error("/welcome failed with error", { exception: O(n) }), v({ isIdentifying: !1 });
504
544
  });
505
545
  }
506
546
  };
507
- return A({
508
- app: n.workspace || "",
509
- debug: !!s,
510
- plugins: [c]
547
+ return j({
548
+ app: t.workspace || "",
549
+ debug: !!o,
550
+ plugins: [s]
511
551
  });
512
- }, X = null, Z = !1, Q = [], $ = (r, a) => {
513
- let { insightsUrl: d = null, debug: g = !1, site: v = null } = a;
514
- s({
515
- workspace: r.workspace,
516
- debug: g,
517
- site_id: v,
518
- insightsUrl: d ?? null
552
+ }, Z = null, Q = !1, $ = [], re = (n, s) => {
553
+ let { insightsUrl: c = null, debug: u = !1, site: m = null } = s;
554
+ a({
555
+ workspace: n.workspace,
556
+ debug: u,
557
+ site_id: m,
558
+ insightsUrl: c ?? null
519
559
  });
520
- let C = async (e, t) => {
560
+ let y = async (e, t) => {
521
561
  if (typeof window > "u") return;
522
- let { page_id: n } = o(), { allTabs: r } = _(), i = x(), a = r.length;
523
- l({
562
+ let { page_id: n } = i(), a = x(), o = C();
563
+ r({
524
564
  event: e,
525
565
  version: t?.version || 1,
526
566
  properties: {
@@ -534,82 +574,82 @@ var j = 30, M = 1e3, N = 3, P = [], F = (e, t, n) => {
534
574
  page_title: void 0,
535
575
  created_at: void 0
536
576
  },
537
- hostname: window.location.hostname,
538
- pathname: window.location.pathname,
539
577
  url: t?.url ?? window.location.href,
540
578
  page_title: t?.page_title ?? document.title,
541
579
  resource: t?.resource ?? "page",
542
- identifier: t?.id ?? n ?? void 0,
580
+ identifier: t?.identifier || n || void 0,
543
581
  created_at: t?.created_at ?? (/* @__PURE__ */ new Date()).toISOString(),
544
- tab_id: i ?? "",
545
- tab_count: a
582
+ hostname: window.location.hostname,
583
+ pathname: window.location.pathname,
584
+ tab_id: a ?? "",
585
+ tab_count: o
546
586
  });
547
- }, E = (e) => {
548
- !X || !Z || X.identify("", { withLock: e?.withLock ?? !0 });
587
+ }, w = (e) => {
588
+ !Z || !Q || Z.identify("", { withLock: e?.withLock ?? !0 });
549
589
  }, D = (e) => {
550
- let { page_id: t } = o();
551
- t !== e && i("page_id", e);
552
- }, O = () => Promise.allSettled([q(), Promise.resolve().then(() => W())]).then(([t, n]) => {
553
- let r = t.status === "fulfilled" ? t.value : null, i = n.status === "fulfilled" ? n.value : null, a = i?.type ? {
554
- type: i.type,
555
- referrer: i.referrer,
556
- data: i.data,
557
- raw: i.raw ?? ""
590
+ let { page_id: t } = i();
591
+ t !== e && o("page_id", e);
592
+ }, O = () => Promise.allSettled([te(), Promise.resolve().then(() => q())]).then(([e, t]) => {
593
+ let n = e.status === "fulfilled" ? e.value : null, r = t.status === "fulfilled" ? t.value : null, i = r?.type ? {
594
+ type: r.type,
595
+ referrer: r.referrer,
596
+ data: r.data,
597
+ raw: r.raw ?? ""
558
598
  } : null;
559
- e({ identity: {
560
- locale: r?.locale ?? null,
561
- timezone: r?.timezone ?? null,
562
- darkMode: r?.dark_mode ?? null,
563
- privateMode: r?.private_mode ?? null,
564
- referrer: a,
565
- properties: r?.properties ?? null
599
+ v({ identity: {
600
+ locale: n?.locale ?? null,
601
+ timezone: n?.timezone ?? null,
602
+ darkMode: n?.dark_mode ?? null,
603
+ privateMode: n?.private_mode ?? null,
604
+ referrer: i,
605
+ properties: n?.properties ?? null
566
606
  } });
567
607
  }), k = () => {
568
- let { insightsBaseUrl: t } = _();
569
- return t ? Promise.resolve() : U(a).then(({ mode: t, url: n }) => {
570
- Z && n && e({
571
- mode: t,
572
- insightsBaseUrl: n
608
+ let { insightsBaseUrl: t } = e();
609
+ return t ? Promise.resolve() : W(s).then(({ mode: e, url: t }) => {
610
+ Q && t && v({
611
+ mode: e,
612
+ insightsBaseUrl: t
573
613
  });
574
614
  }).catch(() => {
575
- Z && d && e({
615
+ Q && c && v({
576
616
  mode: "proxy",
577
- insightsBaseUrl: d
617
+ insightsBaseUrl: c
578
618
  });
579
619
  });
580
620
  };
581
621
  return {
582
622
  init: (t = {}) => {
583
623
  if (typeof window > "u") throw Error("[@levo-so/insights] init() should only be called on the client side.");
584
- let { pageId: i } = t;
585
- i && D(i), !Z && (r.NODE_ENV === "development" && !g || (Z = !0, Q.push(J(n, () => {
586
- if (!Z) return;
587
- let t = _();
588
- t.mode === "direct" && t.isIdentified && (!t.sessionToken || !t.deviceToken) && e({ isIdentified: !1 }), O().finally(() => {
589
- Z && u.set(!0);
624
+ let { pageId: r } = t;
625
+ r && D(r), !Q && (n.NODE_ENV === "development" && !u || (Q = !0, $.push(X(l, () => {
626
+ if (!Q) return;
627
+ let t = e();
628
+ t.mode === "direct" && t.isIdentified && (!t.sessionToken || !t.deviceToken) && v({ isIdentified: !1 }), O().finally(() => {
629
+ Q && T.set(!0);
590
630
  });
591
- })), d && Q.push(J(n, () => {
592
- Z && k().finally(() => {
593
- Z && m.set(!0);
631
+ })), c && $.push(X(l, () => {
632
+ Q && k().finally(() => {
633
+ Q && h.set(!0);
594
634
  });
595
- })), Q.push(J(p, () => {
596
- if (!Z || X) return;
597
- let e = Y(r, C, () => X === e);
598
- e && (X = e, Q.push(y(e), f(e), R(e), z(e), S(e), B(e), V(e), h(e), b(e)));
599
- })), Q.push(J(w, () => {
600
- !Z || !X || (E(), X.page({ resource: "page" }));
635
+ })), $.push(X(f, () => {
636
+ if (!Q || Z) return;
637
+ let e = ne(n, y, () => Z === e);
638
+ e && (Z = e, $.push(g(e), S(e), z(e), B(e), p(e), V(e), H(e), _(e), b(e)));
639
+ })), $.push(X(ee, () => {
640
+ !Q || !Z || (w(), Z.page({ resource: "page" }));
601
641
  }))));
602
642
  },
603
643
  destroy: () => {
604
- Z && (t(T()), c(), Q.forEach((e) => {
644
+ Q && (E(d()), t(), $.forEach((e) => {
605
645
  e();
606
- }), Q.length = 0, X = null, Z = !1, u.set(!1), m.set(!1));
646
+ }), $.length = 0, Z = null, Q = !1, T.set(!1), h.set(!1));
607
647
  },
608
- track: C,
648
+ track: y,
609
649
  get instance() {
610
- return X;
650
+ return Z;
611
651
  }
612
652
  };
613
653
  };
614
654
  //#endregion
615
- export { E as AnalyticsEvents, $ as createLevoInsights };
655
+ export { D as AnalyticsEvents, re as createLevoInsights };