@levo-so/core 0.1.33 → 0.1.35

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.
@@ -37,4 +37,33 @@ var getLevoError = (error) => {
37
37
  });
38
38
  };
39
39
 
40
- export { LevoError, getLevoError };
40
+ // src/utils/lock.ts
41
+ var LocalStorageLock = class {
42
+ key;
43
+ constructor(lockKey) {
44
+ this.key = `lock_${lockKey}`;
45
+ }
46
+ acquireLock(expiryMs = 5e3) {
47
+ const now = Date.now();
48
+ const lockValue = localStorage.getItem(this.key);
49
+ if (lockValue) {
50
+ const timestamp = Number(lockValue);
51
+ if (now - timestamp < expiryMs) {
52
+ return false;
53
+ }
54
+ }
55
+ localStorage.setItem(this.key, now.toString());
56
+ return true;
57
+ }
58
+ releaseLock() {
59
+ localStorage.removeItem(this.key);
60
+ }
61
+ isLocked(expiryMs = 5e3) {
62
+ const lockValue = localStorage.getItem(this.key);
63
+ if (!lockValue) return false;
64
+ const timestamp = Number(lockValue);
65
+ return Date.now() - timestamp < expiryMs;
66
+ }
67
+ };
68
+
69
+ export { LevoError, LocalStorageLock, getLevoError };
package/dist/index.d.ts CHANGED
@@ -5,4 +5,5 @@ export * from "./constants";
5
5
  export * from "./utils/formatImagePath";
6
6
  export * from "./utils/getLevoError";
7
7
  export * from "./utils/LevoError";
8
+ export * from "./utils/lock";
8
9
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { LevoError, getLevoError } from './chunk-VQMSBMEM.js';
1
+ export { LevoError, LocalStorageLock, getLevoError } from './chunk-OT564SO4.js';
2
2
  export { LevoOAuthProviderList } from './chunk-ZLLCLSGE.js';
3
3
  export { LevoPlugin } from './chunk-QJSKNLTV.js';
4
4
  import wretch from 'wretch';
@@ -25,7 +25,7 @@ export declare class LevoAudience extends LevoPlugin {
25
25
  seconds: number;
26
26
  } | null;
27
27
  bounce(properties: ILevoAudience.Properties): Promise<void>;
28
- track(event: string, properties: ILevoAudience.Properties): Promise<any>;
28
+ track<T extends ILevoAudience.Properties>(event: string, properties: T & ILevoAudience.Properties): Promise<any>;
29
29
  getCurrentTabId(): string;
30
30
  getTabCount(): number;
31
31
  }
@@ -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;AAMvD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,qBAAa,YAAa,SAAQ,UAAU;;IAC1C,QAAQ,EAAE,iBAAiB,GAAG,IAAI,CAAQ;IAC1C,aAAa,EAAE,GAAG,CAAQ;IAoB1B,OAAO,CAAC,YAAY,GAAG,OAAO,EAAE,WAAW,GAAG,OAAO,EACnD,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,WAAW;IA8Bb,QAAQ,CAAC,UAAU,EAAE,aAAa,CAAC,UAAU;IAuI7C,QAAQ;IAiBd,WAAW;;;;;;;;;;;IAmEX,qBAAqB;IAqBrB,iBAAiB,IAAI;QACnB,MAAM,EAAE,MAAM,GAAG,QAAQ,CAAC;QAC1B,OAAO,EAAE,MAAM,CAAC;KACjB,GAAG,IAAI;IAcF,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,UAAU;IAc3C,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,CAAC,UAAU;IAkC/D,eAAe;IAaf,WAAW;CAYZ"}
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;AAMvD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,qBAAa,YAAa,SAAQ,UAAU;;IAC1C,QAAQ,EAAE,iBAAiB,GAAG,IAAI,CAAQ;IAC1C,aAAa,EAAE,GAAG,CAAQ;IAoB1B,OAAO,CAAC,YAAY,GAAG,OAAO,EAAE,WAAW,GAAG,OAAO,EACnD,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,WAAW;IA8Bb,QAAQ,CAAC,UAAU,EAAE,aAAa,CAAC,UAAU;IA8J7C,QAAQ;IAed,WAAW;;;;;;;;;;;IAmEX,qBAAqB;IAqBrB,iBAAiB,IAAI;QACnB,MAAM,EAAE,MAAM,GAAG,QAAQ,CAAC;QAC1B,OAAO,EAAE,MAAM,CAAC;KACjB,GAAG,IAAI;IAcF,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,UAAU;IAc3C,KAAK,CAAC,CAAC,SAAS,aAAa,CAAC,UAAU,EAC5C,KAAK,EAAE,MAAM,EACb,UAAU,EAAE,CAAC,GAAG,aAAa,CAAC,UAAU;IAuC1C,eAAe;IAaf,WAAW;CAYZ"}
@@ -1,4 +1,4 @@
1
- import { getLevoError } from '../chunk-VQMSBMEM.js';
1
+ import { LocalStorageLock, getLevoError } from '../chunk-OT564SO4.js';
2
2
  import { LevoPlugin } from '../chunk-QJSKNLTV.js';
3
3
  import { onUserActivity } from '@analytics/activity-utils';
4
4
  import { getVisitorSource } from '@analytics/visitor-source';
@@ -229,34 +229,30 @@ var getUserProperties = async () => {
229
229
  properties.orientation = screen?.orientation?.type;
230
230
  }
231
231
  if (navigator && "getBattery" in navigator) {
232
- const battery = await navigator.getBattery();
232
+ const battery = await navigator?.getBattery();
233
233
  if (battery) {
234
- properties.battery_level = Number((battery.level * 100).toFixed(0));
235
- properties.battery_charging = battery.charging;
236
- properties.battery_discharging_time = battery.dischargingTime;
237
- properties.battery_full_charge_capacity = battery.fullChargeCapacity;
238
- properties.battery_charging_time = battery.chargingTime;
234
+ properties.battery_level = Number((battery?.level * 100).toFixed(0));
235
+ properties.battery_charging = battery?.charging;
236
+ properties.battery_discharging_time = battery?.dischargingTime;
237
+ properties.battery_full_charge_capacity = battery?.fullChargeCapacity;
238
+ properties.battery_charging_time = battery?.chargingTime;
239
239
  }
240
240
  }
241
241
  if (navigator && "deviceMemory" in navigator) {
242
- properties.device_memory = navigator.deviceMemory;
242
+ properties.device_memory = navigator?.deviceMemory;
243
243
  }
244
244
  const supportsTouch = "ontouchstart" in window || typeof navigator?.maxTouchPoints !== "undefined" && navigator?.maxTouchPoints > 0;
245
- if (supportsTouch) {
246
- properties.touch_support = true;
247
- } else {
248
- properties.touch_support = false;
249
- }
245
+ properties.touch_supported = supportsTouch;
250
246
  if ("hardwareConcurrency" in navigator) {
251
- properties.hardware_concurrency = navigator.hardwareConcurrency;
247
+ properties.hardware_concurrency = navigator?.hardwareConcurrency;
252
248
  }
253
249
  if (navigator && "mediaDevices" in navigator) {
254
- const mediaDevices = navigator.mediaDevices;
255
- const devices = await mediaDevices.enumerateDevices();
256
- const hasCamera = devices.some((device) => device.kind === "videoinput");
250
+ const mediaDevices = navigator?.mediaDevices;
251
+ const devices = await mediaDevices?.enumerateDevices();
252
+ const hasCamera = devices.some((device) => device?.kind === "videoinput");
257
253
  properties.camera_supported = hasCamera;
258
254
  const hasMicrophone = devices.some(
259
- (device) => device.kind === "audioinput"
255
+ (device) => device?.kind === "audioinput"
260
256
  );
261
257
  properties.microphone_supported = hasMicrophone;
262
258
  }
@@ -270,35 +266,6 @@ var getUserProperties = async () => {
270
266
  };
271
267
  };
272
268
 
273
- // src/utils/lock.ts
274
- var LocalStorageLock = class {
275
- key;
276
- constructor(lockKey) {
277
- this.key = `lock_${lockKey}`;
278
- }
279
- acquireLock(expiryMs = 5e3) {
280
- const now = Date.now();
281
- const lockValue = localStorage.getItem(this.key);
282
- if (lockValue) {
283
- const timestamp = Number(lockValue);
284
- if (now - timestamp < expiryMs) {
285
- return false;
286
- }
287
- }
288
- localStorage.setItem(this.key, now.toString());
289
- return true;
290
- }
291
- releaseLock() {
292
- localStorage.removeItem(this.key);
293
- }
294
- isLocked(expiryMs = 5e3) {
295
- const lockValue = localStorage.getItem(this.key);
296
- if (!lockValue) return false;
297
- const timestamp = Number(lockValue);
298
- return Date.now() - timestamp < expiryMs;
299
- }
300
- };
301
-
302
269
  // src/plugins/LevoAudience.ts
303
270
  var LevoAudience = class extends LevoPlugin {
304
271
  instance = null;
@@ -347,10 +314,12 @@ var LevoAudience = class extends LevoPlugin {
347
314
  session_id: this.#session,
348
315
  device_id: this.#device,
349
316
  event: payload.event,
317
+ version: payload.version || 1,
350
318
  workspace_id: this.client.workspace,
351
319
  properties: {
352
320
  ...properties,
353
321
  ...payloadProperties,
322
+ version: void 0,
354
323
  resource: void 0,
355
324
  identifier: void 0,
356
325
  hostname: void 0,
@@ -369,10 +338,17 @@ var LevoAudience = class extends LevoPlugin {
369
338
  tab_id: this.getCurrentTabId(),
370
339
  tab_count: this.getTabCount()
371
340
  };
372
- const isQueued = navigator.sendBeacon(
373
- `${this?.client?.insightsUrl}/v1/insights/event/collect`,
374
- JSON.stringify(collectInput)
375
- );
341
+ let isQueued = false;
342
+ try {
343
+ isQueued = navigator.sendBeacon(
344
+ `${this?.client?.insightsUrl}/v1/insights/event/collect`,
345
+ JSON.stringify(collectInput)
346
+ );
347
+ } catch (e) {
348
+ }
349
+ if (!isQueued) {
350
+ this.request("/v1/insights/event/collect", collectInput);
351
+ }
376
352
  this.#events.push({
377
353
  input: payload,
378
354
  payload: collectInput,
@@ -421,10 +397,18 @@ var LevoAudience = class extends LevoPlugin {
421
397
  this.#is_identified = true;
422
398
  this.#session = data.content.data.session;
423
399
  this.#device = data.content.data.device;
424
- navigator.sendBeacon(
425
- `${this?.client?.insightsUrl}/v1/insights/event/bulk?workspace=${this?.client?.workspace}`,
426
- JSON.stringify(this.#batch)
427
- );
400
+ let isQueued = false;
401
+ try {
402
+ isQueued = navigator.sendBeacon(
403
+ `${this?.client?.insightsUrl}/v1/insights/event/bulk?workspace=${this?.client?.workspace}`,
404
+ JSON.stringify(this.#batch)
405
+ );
406
+ } catch (e) {
407
+ }
408
+ if (!isQueued) {
409
+ this.request("/v1/insights/event/bulk", this.#batch);
410
+ }
411
+ this.#batch = [];
428
412
  identifyLock.releaseLock();
429
413
  }).catch((e) => {
430
414
  identifyLock.releaseLock();
@@ -450,7 +434,6 @@ var LevoAudience = class extends LevoPlugin {
450
434
  }
451
435
  const referrer = this.getReferrer();
452
436
  const traits = await getUserProperties();
453
- console.log(traits);
454
437
  await this.instance.identify("", {
455
438
  ...traits,
456
439
  referrer
@@ -521,13 +504,16 @@ var LevoAudience = class extends LevoPlugin {
521
504
  if (!this.client.workspace) {
522
505
  return;
523
506
  }
507
+ console.log(event, properties);
524
508
  if (this.instance && this.#is_identified) {
525
509
  return this.instance.track(event, properties);
526
510
  }
527
511
  return this.#batch.push({
528
512
  event,
513
+ version: properties?.version || 1,
529
514
  properties: {
530
515
  ...properties,
516
+ version: void 0,
531
517
  resource: void 0,
532
518
  identifier: void 0,
533
519
  hostname: void 0,
@@ -102,8 +102,8 @@ var LevoLogger = class extends LevoPlugin {
102
102
  super(client);
103
103
  this.batch = new LogBatch(batchOptions);
104
104
  this.context = context;
105
- this.logLevel = batchOptions?.level ?? (this.client.NODE_ENV === "production" ? 1 /* info */ : 0 /* debug */);
106
- this.enableRemote = batchOptions?.enableRemote ?? this.client.NODE_ENV === "production";
105
+ this.logLevel = batchOptions?.level ?? (this.client.NODE_ENV !== "development" ? 1 /* info */ : 0 /* debug */);
106
+ this.enableRemote = batchOptions?.enableRemote ?? this.client.NODE_ENV !== "development";
107
107
  }
108
108
  debug = (...args) => {
109
109
  this.log(0 /* debug */, args);
@@ -52,6 +52,7 @@ export declare namespace ILevoAudience {
52
52
  session_id?: string;
53
53
  device_id?: string;
54
54
  event: string;
55
+ version?: number;
55
56
  properties: Properties;
56
57
  resource: string;
57
58
  identifier: string | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"audience.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AC;QAChB,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;IAEF,KAAY,YAAY,GAAG,SAAS,GAAG;QACrC,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,KAAK,EAAE,MAAM,CAAC;QACd,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,UAAU,EAAE,UAAU,CAAC;QACvB,QAAQ,EAAE,MAAM,CAAC;QACjB,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;KAChC,CAAC;IAEF,KAAY,cAAc,GAAG;QAC3B,MAAM,EAAE,OAAO,CAAC;QAChB,UAAU,EAAE,OAAO,CAAC;QACpB,MAAM,EAAE,MAAM,CAAC;QACf,IAAI,EAAE,MAAM,CAAC;KACd,CAAC;CACH"}
@@ -1 +1 @@
1
- {"version":3,"file":"getUserProperties.d.ts","sourceRoot":"","sources":["../../src/utils/getUserProperties.ts"],"names":[],"mappings":"AAoBA,eAAO,MAAM,iBAAiB;;;;;;EAoH7B,CAAC"}
1
+ {"version":3,"file":"getUserProperties.d.ts","sourceRoot":"","sources":["../../src/utils/getUserProperties.ts"],"names":[],"mappings":"AAoBA,eAAO,MAAM,iBAAiB;;;;;;EAgH7B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@levo-so/core",
3
- "version": "0.1.33",
3
+ "version": "0.1.35",
4
4
  "author": "Levo Engineering <devs@theinternetfolks.com>",
5
5
  "description": "Levo common utilities",
6
6
  "type": "module",
@@ -23,8 +23,8 @@
23
23
  "devDependencies": {
24
24
  "tsup": "8.2.4",
25
25
  "typescript": "5.5.4",
26
- "@levo/ts-config": "0.0.0",
27
- "@levo/eslint-config": "0.0.0"
26
+ "@levo/eslint-config": "0.0.0",
27
+ "@levo/ts-config": "0.0.0"
28
28
  },
29
29
  "main": "./dist/index.js",
30
30
  "module": "./dist/index.js",