@levo-so/core 0.1.22 → 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.
@@ -1,11 +1,18 @@
1
1
  export type WorkspaceID = `W${string}`;
2
2
  export declare const workspaceIdRegex: RegExp;
3
+ export interface IPageContext {
4
+ url: string;
5
+ id?: string;
6
+ referrer?: string;
7
+ title?: string;
8
+ }
3
9
  export interface ILevoConfig {
4
10
  workspace: WorkspaceID | null;
5
11
  apiUrl: string;
6
12
  insightsUrl: string;
7
13
  NODE_ENV: string;
8
14
  APP_MODE?: "production" | "staging";
15
+ pageContext?: IPageContext | null;
9
16
  }
10
17
  /**
11
18
  * @description LevoClient is class that provides a way to initialize the Levo SDK.
@@ -21,6 +28,8 @@ declare class LevoClient implements ILevoConfig {
21
28
  get NODE_ENV(): string;
22
29
  get APP_MODE(): "production" | "staging" | undefined;
23
30
  updateWorkspace(workspace: WorkspaceID): void;
31
+ getPageContext(): IPageContext | null | undefined;
32
+ updatePageContext(pageContext: IPageContext | null): void;
24
33
  get fetch(): import("wretch/addons/queryString").QueryStringAddon & import("wretch").Wretch<import("wretch/addons/queryString").QueryStringAddon, unknown, undefined>;
25
34
  }
26
35
  export { LevoClient, LevoClient as default };
package/dist/index.js CHANGED
@@ -11,6 +11,11 @@ var LevoClient = class {
11
11
  #insightsUrl;
12
12
  #NODE_ENV;
13
13
  #APP_MODE;
14
+ #pageContext = {
15
+ url: typeof window !== "undefined" ? window?.location?.href : "",
16
+ referrer: typeof document !== "undefined" ? document?.referrer : "",
17
+ title: typeof document !== "undefined" ? document?.title : ""
18
+ };
14
19
  constructor(props) {
15
20
  const defaultProps = {
16
21
  workspace: null,
@@ -50,6 +55,12 @@ var LevoClient = class {
50
55
  updateWorkspace(workspace) {
51
56
  this.#workspace = workspace;
52
57
  }
58
+ getPageContext() {
59
+ return this.#pageContext;
60
+ }
61
+ updatePageContext(pageContext) {
62
+ this.#pageContext = pageContext;
63
+ }
53
64
  get fetch() {
54
65
  const options = {
55
66
  credentials: "include",
@@ -62,9 +73,16 @@ var LevoClient = class {
62
73
  };
63
74
  if (this.workspace) {
64
75
  options.headers = {
76
+ ...options?.headers || {},
65
77
  "Levo-Workspace": this.workspace
66
78
  };
67
79
  }
80
+ if (this.#pageContext && typeof this.#pageContext === "object" && Object.keys(this.#pageContext).length > 0) {
81
+ options.headers = {
82
+ ...options?.headers || {},
83
+ "Levo-Page-Context": JSON.stringify(this.#pageContext)
84
+ };
85
+ }
68
86
  return wretch(this.apiUrl, options).addon(QueryStringAddon);
69
87
  }
70
88
  };
@@ -12,17 +12,21 @@ export declare class LevoAudience extends LevoPlugin {
12
12
  type?: undefined;
13
13
  referrer?: undefined;
14
14
  data?: undefined;
15
+ raw?: undefined;
15
16
  } | {
16
17
  type: any;
17
18
  referrer: any;
18
19
  data: any;
20
+ raw: string;
19
21
  };
20
22
  startActivityListener(): any;
21
- track(event: string, properties: ILevoAudience.Properties): Promise<void>;
23
+ track(event: string, properties: ILevoAudience.Properties): Promise<any>;
22
24
  bounce: (properties?: ILevoAudience.Properties) => void;
23
25
  click: ({ properties, target, }: {
24
26
  properties?: ILevoAudience.Properties;
25
27
  target: HTMLElement;
26
28
  }) => void;
29
+ getCurrentTabId(): string | null;
30
+ getTabCount(): number | null;
27
31
  }
28
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;;IAC1C,QAAQ,EAAE,iBAAiB,GAAG,IAAI,CAAQ;IAK1C,OAAO,CAAC,GAAG,EAAE,MAA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-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,10 +259,10 @@ 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()
256
- }).catch((e) => {
257
- return getLevoError(e);
258
- });
262
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
263
+ tab_id: this.getCurrentTabId(),
264
+ tab_count: this.getTabCount()
265
+ }).catch((e) => getLevoError(e));
259
266
  },
260
267
  page: ({ payload }) => {
261
268
  if (!this?.client?.insightsUrl) {
@@ -268,9 +275,7 @@ var LevoAudience = class extends LevoPlugin {
268
275
  title: document.title,
269
276
  identifier: properties.id,
270
277
  resource: "page"
271
- }).catch((e) => {
272
- return getLevoError(e);
273
- });
278
+ }).catch((e) => getLevoError(e));
274
279
  },
275
280
  identify: ({ payload }) => {
276
281
  if (!this?.client?.insightsUrl) {
@@ -289,19 +294,17 @@ var LevoAudience = class extends LevoPlugin {
289
294
  pathname: window.location.pathname,
290
295
  page_title: document.title,
291
296
  url: window.location.href,
292
- created_at: (/* @__PURE__ */ new Date()).toISOString()
297
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
298
+ tab_id: this.getCurrentTabId(),
299
+ tab_count: this.getTabCount()
293
300
  }
294
301
  ).then(() => {
295
302
  this.isIdentified = true;
296
303
  this.request(
297
304
  `${this?.client?.insightsUrl}/v1/insights/event/collect/bulk`,
298
305
  this.requestBatch
299
- ).catch((e) => {
300
- return getLevoError(e);
301
- });
302
- }).catch((e) => {
303
- return getLevoError(e);
304
- });
306
+ ).catch((e) => getLevoError(e));
307
+ }).catch((e) => getLevoError(e));
305
308
  }
306
309
  };
307
310
  this.instance = Analytics({
@@ -309,7 +312,6 @@ var LevoAudience = class extends LevoPlugin {
309
312
  plugins: [plugin]
310
313
  });
311
314
  const referrer = this.getReferrer();
312
- console.log("Identifying", referrer);
313
315
  await this.instance.identify("", {
314
316
  ...await getUserProperties(),
315
317
  referrer
@@ -339,7 +341,7 @@ var LevoAudience = class extends LevoPlugin {
339
341
  data[key] = data[key]?.[0];
340
342
  }
341
343
  }
342
- return { type, referrer, data };
344
+ return { type, referrer, data, raw: document.referrer };
343
345
  }
344
346
  startActivityListener() {
345
347
  if (typeof document !== "undefined") {
@@ -361,11 +363,10 @@ var LevoAudience = class extends LevoPlugin {
361
363
  return null;
362
364
  }
363
365
  async track(event, properties) {
364
- console.log("Tracking", event, properties);
365
366
  if (this.instance && this.isIdentified) {
366
- this.instance.track(event, properties);
367
+ return this.instance.track(event, properties);
367
368
  }
368
- this.requestBatch.push({
369
+ return this.requestBatch.push({
369
370
  event,
370
371
  properties: {
371
372
  ...properties,
@@ -379,7 +380,9 @@ var LevoAudience = class extends LevoPlugin {
379
380
  resource: properties?.resource || "page",
380
381
  identifier: properties?.identifier || properties?.id,
381
382
  workspace_id: this.client.workspace,
382
- created_at: (/* @__PURE__ */ new Date()).toISOString()
383
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
384
+ tab_id: this.getCurrentTabId(),
385
+ tab_count: this.getTabCount()
383
386
  });
384
387
  }
385
388
  bounce = (properties) => {
@@ -387,6 +390,7 @@ var LevoAudience = class extends LevoPlugin {
387
390
  event: "page.bounce",
388
391
  properties: {
389
392
  ...properties,
393
+ tab_count: this.getTabCount,
390
394
  resource: void 0,
391
395
  identifier: void 0
392
396
  },
@@ -397,7 +401,9 @@ var LevoAudience = class extends LevoPlugin {
397
401
  resource: properties?.resource || "page",
398
402
  identifier: properties?.identifier || properties?.id,
399
403
  workspace_id: this.client.workspace,
400
- created_at: (/* @__PURE__ */ new Date()).toISOString()
404
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
405
+ tab_id: this.getCurrentTabId(),
406
+ tab_count: this.getTabCount()
401
407
  };
402
408
  navigator.sendBeacon(
403
409
  `${this?.client?.insightsUrl}/v1/insights/event/collect`,
@@ -414,6 +420,28 @@ var LevoAudience = class extends LevoPlugin {
414
420
  label
415
421
  });
416
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
+ }
417
445
  };
418
446
 
419
447
  export { LevoAudience };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@levo-so/core",
3
- "version": "0.1.22",
3
+ "version": "0.1.25",
4
4
  "author": "Levo Engineering <devs@theinternetfolks.com>",
5
5
  "description": "Levo common utilities",
6
6
  "type": "module",