@levo-so/core 0.1.24 → 0.1.25

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.
@@ -2,8 +2,8 @@ export type WorkspaceID = `W${string}`;
2
2
  export declare const workspaceIdRegex: RegExp;
3
3
  export interface IPageContext {
4
4
  url: string;
5
- referrer?: string;
6
5
  id?: string;
6
+ referrer?: string;
7
7
  title?: string;
8
8
  }
9
9
  export interface ILevoConfig {
@@ -28,6 +28,7 @@ declare class LevoClient implements ILevoConfig {
28
28
  get NODE_ENV(): string;
29
29
  get APP_MODE(): "production" | "staging" | undefined;
30
30
  updateWorkspace(workspace: WorkspaceID): void;
31
+ getPageContext(): IPageContext | null | undefined;
31
32
  updatePageContext(pageContext: IPageContext | null): void;
32
33
  get fetch(): import("wretch/addons/queryString").QueryStringAddon & import("wretch").Wretch<import("wretch/addons/queryString").QueryStringAddon, unknown, undefined>;
33
34
  }
package/dist/index.js CHANGED
@@ -55,6 +55,9 @@ var LevoClient = class {
55
55
  updateWorkspace(workspace) {
56
56
  this.#workspace = workspace;
57
57
  }
58
+ getPageContext() {
59
+ return this.#pageContext;
60
+ }
58
61
  updatePageContext(pageContext) {
59
62
  this.#pageContext = pageContext;
60
63
  }
@@ -70,10 +73,11 @@ var LevoClient = class {
70
73
  };
71
74
  if (this.workspace) {
72
75
  options.headers = {
76
+ ...options?.headers || {},
73
77
  "Levo-Workspace": this.workspace
74
78
  };
75
79
  }
76
- if (typeof this.#pageContext === "object" && this.#pageContext && Object.keys(this.#pageContext).length > 0) {
80
+ if (this.#pageContext && typeof this.#pageContext === "object" && Object.keys(this.#pageContext).length > 0) {
77
81
  options.headers = {
78
82
  ...options?.headers || {},
79
83
  "Levo-Page-Context": JSON.stringify(this.#pageContext)
@@ -26,5 +26,7 @@ export declare class LevoAudience extends LevoPlugin {
26
26
  properties?: ILevoAudience.Properties;
27
27
  target: HTMLElement;
28
28
  }) => void;
29
+ getCurrentTabId(): string | null;
30
+ getTabCount(): number | null;
29
31
  }
30
32
  //# sourceMappingURL=LevoAudience.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"LevoAudience.d.ts","sourceRoot":"","sources":["../../src/plugins/LevoAudience.ts"],"names":[],"mappings":"AAEA,OAAO,EAAa,KAAK,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAG9D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAIvD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,qBAAa,YAAa,SAAQ,UAAU;;IAC3C,QAAQ,EAAE,iBAAiB,GAAG,IAAI,CAAQ;IAK1C,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG;IAkB9B,YAAY,UAAS;IACrB,YAAY,EAAE,GAAG,EAAE,CAAM;IAEnB,QAAQ,CAAC,UAAU,EAAE,aAAa,CAAC,UAAU;IAmGnD,WAAW;;;;;;;;;;;IAmEX,qBAAqB;IAqBf,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,CAAC,UAAU;IAuB/D,MAAM,gBAAiB,aAAa,CAAC,UAAU,UAsB7C;IAEF,KAAK,4BAGF;QACF,UAAU,CAAC,EAAE,aAAa,CAAC,UAAU,CAAC;QACtC,MAAM,EAAE,WAAW,CAAC;KACpB,UAOC;CACF"}
1
+ {"version":3,"file":"LevoAudience.d.ts","sourceRoot":"","sources":["../../src/plugins/LevoAudience.ts"],"names":[],"mappings":"AAEA,OAAO,EAAa,KAAK,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAG9D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAIvD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,qBAAa,YAAa,SAAQ,UAAU;;IAC1C,QAAQ,EAAE,iBAAiB,GAAG,IAAI,CAAQ;IAK1C,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG;IA2B9B,YAAY,UAAS;IACrB,YAAY,EAAE,GAAG,EAAE,CAAM;IAEnB,QAAQ,CAAC,UAAU,EAAE,aAAa,CAAC,UAAU;IAuGnD,WAAW;;;;;;;;;;;IAmEX,qBAAqB;IAqBf,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,CAAC,UAAU;IAyB/D,MAAM,gBAAiB,aAAa,CAAC,UAAU,UAyB7C;IAEF,KAAK,4BAGF;QACD,UAAU,CAAC,EAAE,aAAa,CAAC,UAAU,CAAC;QACtC,MAAM,EAAE,WAAW,CAAC;KACrB,UAOC;IAEF,eAAe;IAef,WAAW;CAYZ"}
@@ -215,12 +215,19 @@ var LevoAudience = class extends LevoPlugin {
215
215
  // 60 seconds till we consider user idle
216
216
  request(url, data) {
217
217
  const headers = {
218
+ Accept: "application/json",
218
219
  "Content-Type": "application/json"
219
220
  };
220
221
  if (this.client.workspace) {
221
222
  headers["Levo-Workspace"] = this.client.workspace;
222
223
  }
223
- return wretch(url).options({
224
+ const pageContext = this.client.getPageContext();
225
+ if (pageContext) {
226
+ headers["Levo-Page-Context"] = JSON.stringify(pageContext);
227
+ }
228
+ return wretch(url, {
229
+ credentials: "include"
230
+ }).options({
224
231
  keepAlive: true
225
232
  }).headers(headers).post(data).json();
226
233
  }
@@ -252,7 +259,9 @@ var LevoAudience = class extends LevoPlugin {
252
259
  resource: payloadProperties?.resource || "page",
253
260
  identifier: payloadProperties?.identifier || properties?.id,
254
261
  workspace_id: this.client.workspace,
255
- created_at: (/* @__PURE__ */ new Date()).toISOString()
262
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
263
+ tab_id: this.getCurrentTabId(),
264
+ tab_count: this.getTabCount()
256
265
  }).catch((e) => getLevoError(e));
257
266
  },
258
267
  page: ({ payload }) => {
@@ -285,7 +294,9 @@ var LevoAudience = class extends LevoPlugin {
285
294
  pathname: window.location.pathname,
286
295
  page_title: document.title,
287
296
  url: window.location.href,
288
- created_at: (/* @__PURE__ */ new Date()).toISOString()
297
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
298
+ tab_id: this.getCurrentTabId(),
299
+ tab_count: this.getTabCount()
289
300
  }
290
301
  ).then(() => {
291
302
  this.isIdentified = true;
@@ -369,7 +380,9 @@ var LevoAudience = class extends LevoPlugin {
369
380
  resource: properties?.resource || "page",
370
381
  identifier: properties?.identifier || properties?.id,
371
382
  workspace_id: this.client.workspace,
372
- created_at: (/* @__PURE__ */ new Date()).toISOString()
383
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
384
+ tab_id: this.getCurrentTabId(),
385
+ tab_count: this.getTabCount()
373
386
  });
374
387
  }
375
388
  bounce = (properties) => {
@@ -377,6 +390,7 @@ var LevoAudience = class extends LevoPlugin {
377
390
  event: "page.bounce",
378
391
  properties: {
379
392
  ...properties,
393
+ tab_count: this.getTabCount,
380
394
  resource: void 0,
381
395
  identifier: void 0
382
396
  },
@@ -387,7 +401,9 @@ var LevoAudience = class extends LevoPlugin {
387
401
  resource: properties?.resource || "page",
388
402
  identifier: properties?.identifier || properties?.id,
389
403
  workspace_id: this.client.workspace,
390
- created_at: (/* @__PURE__ */ new Date()).toISOString()
404
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
405
+ tab_id: this.getCurrentTabId(),
406
+ tab_count: this.getTabCount()
391
407
  };
392
408
  navigator.sendBeacon(
393
409
  `${this?.client?.insightsUrl}/v1/insights/event/collect`,
@@ -404,6 +420,28 @@ var LevoAudience = class extends LevoPlugin {
404
420
  label
405
421
  });
406
422
  };
423
+ getCurrentTabId() {
424
+ try {
425
+ if (typeof window === "undefined" || !("sessionStorage" in window)) {
426
+ return null;
427
+ }
428
+ const CURRENT_TAB_LOCAL_STORAGE_KEY = "lv_insights_ct";
429
+ return window.sessionStorage.getItem(CURRENT_TAB_LOCAL_STORAGE_KEY) || null;
430
+ } catch (_) {
431
+ return null;
432
+ }
433
+ }
434
+ getTabCount() {
435
+ try {
436
+ if (typeof window === "undefined" || !("localStorage" in window)) {
437
+ return null;
438
+ }
439
+ const OT_LOCAL_STORAGE_KEY = "lv_insights_ot";
440
+ return Number(window.localStorage.getItem(OT_LOCAL_STORAGE_KEY)) || 0;
441
+ } catch (_) {
442
+ return 0;
443
+ }
444
+ }
407
445
  };
408
446
 
409
447
  export { LevoAudience };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@levo-so/core",
3
- "version": "0.1.24",
3
+ "version": "0.1.25",
4
4
  "author": "Levo Engineering <devs@theinternetfolks.com>",
5
5
  "description": "Levo common utilities",
6
6
  "type": "module",