@levo-so/core 0.1.29 → 0.1.32
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.
- package/dist/plugins/LevoAudience.d.ts +8 -1
- package/dist/plugins/LevoAudience.d.ts.map +1 -1
- package/dist/plugins/LevoAudience.js +83 -19
- package/dist/types/audience.d.ts +6 -0
- package/dist/types/audience.d.ts.map +1 -1
- package/dist/utils/lock.d.ts +21 -0
- package/dist/utils/lock.d.ts.map +1 -0
- package/package.json +1 -1
|
@@ -4,8 +4,10 @@ import { LevoPlugin } from "./LevoPlugin";
|
|
|
4
4
|
export declare class LevoAudience extends LevoPlugin {
|
|
5
5
|
#private;
|
|
6
6
|
instance: AnalyticsInstance | null;
|
|
7
|
+
idle_listener: any;
|
|
7
8
|
request<ResponseBody = unknown, RequestBody = unknown>(url: string, data: RequestBody): Promise<Awaited<ResponseBody>>;
|
|
8
9
|
initiate(properties: ILevoAudience.Properties): Promise<any>;
|
|
10
|
+
identify(): Promise<void>;
|
|
9
11
|
getReferrer(): {
|
|
10
12
|
type?: undefined;
|
|
11
13
|
referrer?: undefined;
|
|
@@ -17,7 +19,12 @@ export declare class LevoAudience extends LevoPlugin {
|
|
|
17
19
|
data: Record<string, string>;
|
|
18
20
|
raw: string;
|
|
19
21
|
};
|
|
20
|
-
startActivityListener():
|
|
22
|
+
startActivityListener(): null;
|
|
23
|
+
getActivityStatus(): {
|
|
24
|
+
status: "idle" | "active";
|
|
25
|
+
seconds: number;
|
|
26
|
+
} | null;
|
|
27
|
+
bounce(properties: ILevoAudience.Properties): Promise<void>;
|
|
21
28
|
track(event: string, properties: ILevoAudience.Properties): Promise<any>;
|
|
22
29
|
getCurrentTabId(): string;
|
|
23
30
|
getTabCount(): number;
|
|
@@ -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;
|
|
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;IAsI7C,QAAQ;IAad,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"}
|
|
@@ -206,9 +206,39 @@ var getUserProperties = async () => {
|
|
|
206
206
|
};
|
|
207
207
|
};
|
|
208
208
|
|
|
209
|
+
// src/utils/lock.ts
|
|
210
|
+
var LocalStorageLock = class {
|
|
211
|
+
key;
|
|
212
|
+
constructor(lockKey) {
|
|
213
|
+
this.key = `lock_${lockKey}`;
|
|
214
|
+
}
|
|
215
|
+
acquireLock(expiryMs = 5e3) {
|
|
216
|
+
const now = Date.now();
|
|
217
|
+
const lockValue = localStorage.getItem(this.key);
|
|
218
|
+
if (lockValue) {
|
|
219
|
+
const timestamp = Number(lockValue);
|
|
220
|
+
if (now - timestamp < expiryMs) {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
localStorage.setItem(this.key, now.toString());
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
releaseLock() {
|
|
228
|
+
localStorage.removeItem(this.key);
|
|
229
|
+
}
|
|
230
|
+
isLocked(expiryMs = 5e3) {
|
|
231
|
+
const lockValue = localStorage.getItem(this.key);
|
|
232
|
+
if (!lockValue) return false;
|
|
233
|
+
const timestamp = Number(lockValue);
|
|
234
|
+
return Date.now() - timestamp < expiryMs;
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
|
|
209
238
|
// src/plugins/LevoAudience.ts
|
|
210
239
|
var LevoAudience = class extends LevoPlugin {
|
|
211
240
|
instance = null;
|
|
241
|
+
idle_listener = null;
|
|
212
242
|
#IDLE_THROTTLE = 1 * 1e3;
|
|
213
243
|
// every 1 second we check for idleness
|
|
214
244
|
#IDLE_TIMEOUT = 60 * 1e3;
|
|
@@ -317,35 +347,48 @@ var LevoAudience = class extends LevoPlugin {
|
|
|
317
347
|
tab_id: this.getCurrentTabId(),
|
|
318
348
|
tab_count: this.getTabCount()
|
|
319
349
|
};
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
350
|
+
const identifyLock = new LocalStorageLock("lv_aud_identify");
|
|
351
|
+
if (identifyLock.acquireLock()) {
|
|
352
|
+
this.request(
|
|
353
|
+
`${this?.client?.insightsUrl}/v1/insights/event/welcome`,
|
|
354
|
+
welcomeInput
|
|
355
|
+
).then((data) => {
|
|
356
|
+
this.#is_identified = true;
|
|
357
|
+
this.#session = data.content.data.session;
|
|
358
|
+
this.#device = data.content.data.device;
|
|
359
|
+
navigator.sendBeacon(
|
|
360
|
+
`${this?.client?.insightsUrl}/v1/insights/event/bulk?workspace=${this?.client?.workspace}`,
|
|
361
|
+
JSON.stringify(this.#batch)
|
|
362
|
+
);
|
|
363
|
+
identifyLock.releaseLock();
|
|
364
|
+
}).catch((e) => {
|
|
365
|
+
identifyLock.releaseLock();
|
|
366
|
+
return getLevoError(e);
|
|
367
|
+
});
|
|
368
|
+
}
|
|
332
369
|
}
|
|
333
370
|
};
|
|
334
371
|
this.instance = Analytics({
|
|
335
372
|
app: this.client.workspace || "",
|
|
336
373
|
plugins: [plugin]
|
|
337
374
|
});
|
|
338
|
-
|
|
339
|
-
await this.instance.identify("", {
|
|
340
|
-
...await getUserProperties(),
|
|
341
|
-
referrer
|
|
342
|
-
});
|
|
375
|
+
await this.identify();
|
|
343
376
|
this.startActivityListener();
|
|
344
377
|
return this.instance.page({
|
|
345
378
|
resource: "page",
|
|
346
379
|
identifier: properties.id
|
|
347
380
|
});
|
|
348
381
|
}
|
|
382
|
+
async identify() {
|
|
383
|
+
if (!this.instance) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
const referrer = this.getReferrer();
|
|
387
|
+
await this.instance.identify("", {
|
|
388
|
+
...await getUserProperties(),
|
|
389
|
+
referrer
|
|
390
|
+
});
|
|
391
|
+
}
|
|
349
392
|
getReferrer() {
|
|
350
393
|
if (typeof document === "undefined") {
|
|
351
394
|
return {};
|
|
@@ -368,8 +411,8 @@ var LevoAudience = class extends LevoPlugin {
|
|
|
368
411
|
return { type, referrer, data, raw: document.referrer };
|
|
369
412
|
}
|
|
370
413
|
startActivityListener() {
|
|
371
|
-
if (typeof document !== "undefined") {
|
|
372
|
-
|
|
414
|
+
if (typeof document !== "undefined" && !this.idle_listener) {
|
|
415
|
+
this.idle_listener = onUserActivity({
|
|
373
416
|
timeout: this.#IDLE_TIMEOUT,
|
|
374
417
|
throttle: this.#IDLE_THROTTLE,
|
|
375
418
|
onIdle: (activeForSeconds) => {
|
|
@@ -386,6 +429,27 @@ var LevoAudience = class extends LevoPlugin {
|
|
|
386
429
|
}
|
|
387
430
|
return null;
|
|
388
431
|
}
|
|
432
|
+
getActivityStatus() {
|
|
433
|
+
const status = this?.idle_listener?.getStatus();
|
|
434
|
+
if (!status) {
|
|
435
|
+
return null;
|
|
436
|
+
}
|
|
437
|
+
return {
|
|
438
|
+
status: status.isIdle ? "idle" : "active",
|
|
439
|
+
seconds: status.isIdle ? status.idle : status.active
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
async bounce(properties) {
|
|
443
|
+
const activity_status = this.getActivityStatus();
|
|
444
|
+
if (activity_status) {
|
|
445
|
+
if (activity_status?.status === "idle") {
|
|
446
|
+
properties.seconds_idle = activity_status.seconds;
|
|
447
|
+
} else {
|
|
448
|
+
properties.seconds_active = activity_status.seconds;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
this.track("page.bounce", properties);
|
|
452
|
+
}
|
|
389
453
|
async track(event, properties) {
|
|
390
454
|
if (!this.client.workspace) {
|
|
391
455
|
return;
|
package/dist/types/audience.d.ts
CHANGED
|
@@ -55,5 +55,11 @@ export declare namespace ILevoAudience {
|
|
|
55
55
|
resource: string;
|
|
56
56
|
identifier: string | undefined;
|
|
57
57
|
};
|
|
58
|
+
type ActivityStatus = {
|
|
59
|
+
isIdle: boolean;
|
|
60
|
+
isDisabled: boolean;
|
|
61
|
+
active: number;
|
|
62
|
+
idle: number;
|
|
63
|
+
};
|
|
58
64
|
}
|
|
59
65
|
//# sourceMappingURL=audience.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"audience.d.ts","sourceRoot":"","sources":["../../src/types/audience.ts"],"names":[],"mappings":"AACA,yBAAiB,aAAa,CAAC;IAC7B,UAAiB,MAAM;QACrB,YAAY,EAAE,OAAO,CAAC;QACtB,SAAS,EAAE,OAAO,CAAC;QACnB,MAAM,EAAE,MAAM,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AAC;CACH"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @example
|
|
3
|
+
*
|
|
4
|
+
* const lock = new LocalStorageLock("myResource");
|
|
5
|
+
* if (lock.acquireLock()) {
|
|
6
|
+
* console.log("Lock acquired");
|
|
7
|
+
* // Do some work...
|
|
8
|
+
* lock.releaseLock();
|
|
9
|
+
* } else {
|
|
10
|
+
* console.log("Lock is already held by another instance");
|
|
11
|
+
* }
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
export declare class LocalStorageLock {
|
|
15
|
+
private key;
|
|
16
|
+
constructor(lockKey: string);
|
|
17
|
+
acquireLock(expiryMs?: number): boolean;
|
|
18
|
+
releaseLock(): void;
|
|
19
|
+
isLocked(expiryMs?: number): boolean;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=lock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lock.d.ts","sourceRoot":"","sources":["../../src/utils/lock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,GAAG,CAAS;gBAER,OAAO,EAAE,MAAM;IAI3B,WAAW,CAAC,QAAQ,GAAE,MAAa,GAAG,OAAO;IAe7C,WAAW,IAAI,IAAI;IAInB,QAAQ,CAAC,QAAQ,GAAE,MAAa,GAAG,OAAO;CAO3C"}
|