@formo/analytics 1.13.4-alpha.8 → 1.13.4-alpha.9

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.
Files changed (55) hide show
  1. package/dist/cjs/src/FormoAnalytics.d.ts +1 -2
  2. package/dist/cjs/src/FormoAnalytics.d.ts.map +1 -1
  3. package/dist/cjs/src/FormoAnalytics.js +15 -18
  4. package/dist/cjs/src/FormoAnalytics.js.map +1 -1
  5. package/dist/cjs/src/constants/base.d.ts +3 -0
  6. package/dist/cjs/src/constants/base.d.ts.map +1 -0
  7. package/dist/cjs/src/constants/base.js +6 -0
  8. package/dist/cjs/src/constants/base.js.map +1 -0
  9. package/dist/cjs/src/constants/index.d.ts +3 -2
  10. package/dist/cjs/src/constants/index.d.ts.map +1 -1
  11. package/dist/cjs/src/constants/index.js +1 -0
  12. package/dist/cjs/src/constants/index.js.map +1 -1
  13. package/dist/cjs/src/lib/index.d.ts +3 -0
  14. package/dist/cjs/src/lib/index.d.ts.map +1 -0
  15. package/dist/cjs/src/lib/index.js +24 -0
  16. package/dist/cjs/src/lib/index.js.map +1 -0
  17. package/dist/cjs/src/lib/session-storage.d.ts +11 -0
  18. package/dist/cjs/src/lib/session-storage.d.ts.map +1 -0
  19. package/dist/cjs/src/lib/session-storage.js +52 -0
  20. package/dist/cjs/src/lib/session-storage.js.map +1 -0
  21. package/dist/cjs/test/lib.spec.js +3 -3
  22. package/dist/cjs/test/lib.spec.js.map +1 -1
  23. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  24. package/dist/esm/src/FormoAnalytics.d.ts +1 -2
  25. package/dist/esm/src/FormoAnalytics.d.ts.map +1 -1
  26. package/dist/esm/src/FormoAnalytics.js +13 -16
  27. package/dist/esm/src/FormoAnalytics.js.map +1 -1
  28. package/dist/esm/src/constants/base.d.ts +3 -0
  29. package/dist/esm/src/constants/base.d.ts.map +1 -0
  30. package/dist/esm/src/constants/base.js +3 -0
  31. package/dist/esm/src/constants/base.js.map +1 -0
  32. package/dist/esm/src/constants/index.d.ts +3 -2
  33. package/dist/esm/src/constants/index.d.ts.map +1 -1
  34. package/dist/esm/src/constants/index.js +3 -2
  35. package/dist/esm/src/constants/index.js.map +1 -1
  36. package/dist/esm/src/lib/index.d.ts +3 -0
  37. package/dist/esm/src/lib/index.d.ts.map +1 -0
  38. package/dist/esm/src/lib/index.js +3 -0
  39. package/dist/esm/src/lib/index.js.map +1 -0
  40. package/dist/esm/src/lib/session-storage.d.ts +11 -0
  41. package/dist/esm/src/lib/session-storage.d.ts.map +1 -0
  42. package/dist/esm/src/lib/session-storage.js +49 -0
  43. package/dist/esm/src/lib/session-storage.js.map +1 -0
  44. package/dist/esm/test/lib.spec.js +1 -1
  45. package/dist/esm/test/lib.spec.js.map +1 -1
  46. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  47. package/dist/index.umd.min.js +1 -1
  48. package/dist/index.umd.min.js.map +1 -1
  49. package/package.json +1 -1
  50. package/src/FormoAnalytics.ts +12 -18
  51. package/src/constants/base.ts +2 -0
  52. package/src/constants/index.ts +3 -2
  53. package/src/lib/index.ts +2 -0
  54. package/src/lib/session-storage.ts +53 -0
  55. package/test/lib.spec.ts +1 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formo/analytics",
3
- "version": "1.13.4-alpha.8",
3
+ "version": "1.13.4-alpha.9",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/getformo/sdk.git"
@@ -17,7 +17,8 @@ import {
17
17
  SignatureStatus,
18
18
  TransactionStatus,
19
19
  } from "./types";
20
- import { isLocalhost, toSnakeCase } from "./lib/utils";
20
+ import { formoSessionStorage, isLocalhost, toSnakeCase } from "./lib";
21
+ import { SESSION_IDENTIFIED_KEY } from "./constants";
21
22
 
22
23
  interface IFormoAnalytics {
23
24
  page(): void;
@@ -61,8 +62,7 @@ interface IFormoAnalytics {
61
62
  export class FormoAnalytics implements IFormoAnalytics {
62
63
  private _provider?: EIP1193Provider;
63
64
  private _providerListeners: Record<string, (...args: unknown[]) => void> = {};
64
- private identifySent?: boolean;
65
- readonly identifySentKey = "identifySent";
65
+ private sessionIdentified?: boolean = false;
66
66
 
67
67
  config: Config;
68
68
  currentChainId?: ChainID;
@@ -77,14 +77,8 @@ export class FormoAnalytics implements IFormoAnalytics {
77
77
  trackLocalhost: options.trackLocalhost,
78
78
  };
79
79
 
80
- try {
81
- const isWalletIdentified = JSON.parse(
82
- sessionStorage.getItem(this.identifySentKey) || "false"
83
- );
84
- this.identifySent = isWalletIdentified;
85
- } catch {
86
- this.identifySent = false;
87
- }
80
+ this.sessionIdentified =
81
+ (formoSessionStorage.getItem(SESSION_IDENTIFIED_KEY) as boolean) ?? false;
88
82
 
89
83
  // TODO: replace with eip6963
90
84
  const provider = options.provider || window?.ethereum;
@@ -288,9 +282,9 @@ export class FormoAnalytics implements IFormoAnalytics {
288
282
  providerName?: string;
289
283
  rdns?: string;
290
284
  }): Promise<void> {
291
- if (this.identifySent === false) {
292
- this.identifySent = true;
293
- sessionStorage.setItem(this.identifySentKey, "true");
285
+ if (this.sessionIdentified === false) {
286
+ this.sessionIdentified = true;
287
+ formoSessionStorage.setItem(SESSION_IDENTIFIED_KEY, true);
294
288
  await this.trackEvent(Event.IDENTIFY, {
295
289
  address,
296
290
  providerName,
@@ -548,8 +542,8 @@ export class FormoAnalytics implements IFormoAnalytics {
548
542
  }
549
543
 
550
544
  private async trackFirstPageHit(): Promise<void> {
551
- if (sessionStorage.getItem(CURRENT_URL_KEY) === null) {
552
- sessionStorage.setItem(CURRENT_URL_KEY, window.location.href);
545
+ if (formoSessionStorage.getItem(CURRENT_URL_KEY) === null) {
546
+ formoSessionStorage.setItem(CURRENT_URL_KEY, window.location.href);
553
547
  }
554
548
 
555
549
  return this.trackPageHit();
@@ -575,10 +569,10 @@ export class FormoAnalytics implements IFormoAnalytics {
575
569
  }
576
570
 
577
571
  private async onLocationChange(): Promise<void> {
578
- const currentUrl = sessionStorage.getItem(CURRENT_URL_KEY);
572
+ const currentUrl = formoSessionStorage.getItem(CURRENT_URL_KEY);
579
573
 
580
574
  if (currentUrl !== window.location.href) {
581
- sessionStorage.setItem(CURRENT_URL_KEY, window.location.href);
575
+ formoSessionStorage.setItem(CURRENT_URL_KEY, window.location.href);
582
576
  this.trackPageHit();
583
577
  }
584
578
  }
@@ -0,0 +1,2 @@
1
+ export const SESSION_PREFIX = "f0-";
2
+ export const SESSION_IDENTIFIED_KEY = SESSION_PREFIX + "session_identified";
@@ -1,2 +1,3 @@
1
- export * from './config';
2
- export * from './events';
1
+ export * from "./base";
2
+ export * from "./config";
3
+ export * from "./events";
@@ -0,0 +1,2 @@
1
+ export { default as formoSessionStorage } from "./session-storage";
2
+ export * from "./utils";
@@ -0,0 +1,53 @@
1
+ export class SessionStorage {
2
+ private readonly json_prefix = "__json=";
3
+
4
+ public setItem(key: string, value: any): void {
5
+ if (typeof value === "boolean") value = value === true ? "true" : "false";
6
+ if (typeof value === "object")
7
+ value = this.json_prefix + JSON.stringify(value);
8
+ sessionStorage.setItem(key, value);
9
+ }
10
+
11
+ public getItem(key: string): string | boolean | Record<any, any> | null {
12
+ const value = sessionStorage.getItem(key);
13
+
14
+ if (!value || typeof value !== "string") return null;
15
+ if (["null", "undefined"].some((item) => item == value)) return null;
16
+
17
+ if (value.startsWith(this.json_prefix)) {
18
+ try {
19
+ return JSON.parse(value.slice(7));
20
+ } catch (error) {
21
+ console.error(
22
+ "[FORMO_ERROR] SessionStorage failed to parse JSON",
23
+ error
24
+ );
25
+ return null;
26
+ }
27
+ }
28
+
29
+ if (["true", "false"].some((item) => item == value)) {
30
+ return JSON.parse(value);
31
+ }
32
+
33
+ return value;
34
+ }
35
+
36
+ public removeMatch(pattern: RegExp): void {
37
+ for (const key in sessionStorage) {
38
+ if (pattern.test(key)) {
39
+ this.removeItem(key);
40
+ }
41
+ }
42
+ }
43
+
44
+ public removeItem(key: string): void {
45
+ sessionStorage.removeItem(key);
46
+ }
47
+
48
+ public clear(): void {
49
+ sessionStorage.clear();
50
+ }
51
+ }
52
+
53
+ export default new SessionStorage();
package/test/lib.spec.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { describe, it } from "mocha";
2
- import { toSnakeCase } from "../src/lib/utils";
2
+ import { toSnakeCase } from "../src/lib";
3
3
  import { expect } from "chai";
4
4
 
5
5
  describe("toSnakeCase", () => {