@formo/analytics 1.11.5 → 1.11.7

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 (38) hide show
  1. package/README.md +7 -12
  2. package/dist/cjs/src/FormoAnalytics.d.ts +7 -7
  3. package/dist/cjs/src/FormoAnalytics.d.ts.map +1 -1
  4. package/dist/cjs/src/FormoAnalytics.js +23 -36
  5. package/dist/cjs/src/FormoAnalytics.js.map +1 -1
  6. package/dist/cjs/src/FormoAnalyticsProvider.d.ts +1 -1
  7. package/dist/cjs/src/FormoAnalyticsProvider.d.ts.map +1 -1
  8. package/dist/cjs/src/FormoAnalyticsProvider.js +3 -3
  9. package/dist/cjs/src/FormoAnalyticsProvider.js.map +1 -1
  10. package/dist/cjs/src/constants/config.d.ts +1 -1
  11. package/dist/cjs/src/constants/config.d.ts.map +1 -1
  12. package/dist/cjs/src/constants/config.js +2 -2
  13. package/dist/cjs/src/constants/config.js.map +1 -1
  14. package/dist/cjs/src/types/base.d.ts +5 -1
  15. package/dist/cjs/src/types/base.d.ts.map +1 -1
  16. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  17. package/dist/esm/src/FormoAnalytics.d.ts +7 -7
  18. package/dist/esm/src/FormoAnalytics.d.ts.map +1 -1
  19. package/dist/esm/src/FormoAnalytics.js +24 -37
  20. package/dist/esm/src/FormoAnalytics.js.map +1 -1
  21. package/dist/esm/src/FormoAnalyticsProvider.d.ts +1 -1
  22. package/dist/esm/src/FormoAnalyticsProvider.d.ts.map +1 -1
  23. package/dist/esm/src/FormoAnalyticsProvider.js +3 -3
  24. package/dist/esm/src/FormoAnalyticsProvider.js.map +1 -1
  25. package/dist/esm/src/constants/config.d.ts +1 -1
  26. package/dist/esm/src/constants/config.d.ts.map +1 -1
  27. package/dist/esm/src/constants/config.js +1 -1
  28. package/dist/esm/src/constants/config.js.map +1 -1
  29. package/dist/esm/src/types/base.d.ts +5 -1
  30. package/dist/esm/src/types/base.d.ts.map +1 -1
  31. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  32. package/dist/index.umd.min.js +1 -1
  33. package/dist/index.umd.min.js.map +1 -1
  34. package/package.json +1 -1
  35. package/src/FormoAnalytics.ts +28 -43
  36. package/src/FormoAnalyticsProvider.tsx +3 -3
  37. package/src/constants/config.ts +1 -1
  38. package/src/types/base.ts +7 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formo/analytics",
3
- "version": "1.11.5",
3
+ "version": "1.11.7",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/getformo/sdk.git"
@@ -1,19 +1,19 @@
1
1
  import axios from 'axios';
2
2
  import {
3
3
  COUNTRY_LIST,
4
- EVENTS_API,
4
+ EVENTS_API_URL,
5
5
  SESSION_STORAGE_ID_KEY,
6
6
  Event,
7
7
  } from './constants';
8
8
  import { isNotEmpty } from './utils';
9
9
  import { H } from 'highlight.run';
10
- import { ChainID, EIP1193Provider, RequestArguments } from './types';
10
+ import { ChainID, EIP1193Provider, Options } from './types';
11
11
 
12
12
  interface IFormoAnalytics {
13
13
  /**
14
14
  * Initializes the FormoAnalytics instance with the provided API key and project ID.
15
15
  */
16
- init(apiKey: string, projectId: string): Promise<FormoAnalytics>;
16
+ init(apiKey: string, options: Options): Promise<FormoAnalytics>;
17
17
 
18
18
  /**
19
19
  * Identifies the user with the provided user data.
@@ -45,6 +45,9 @@ interface IFormoAnalytics {
45
45
  */
46
46
  chain(attributes: { chainId: ChainID; account?: string }): void;
47
47
  }
48
+ interface Config {
49
+ token: string;
50
+ }
48
51
  export class FormoAnalytics implements IFormoAnalytics {
49
52
  private _provider?: EIP1193Provider;
50
53
  private _registeredProviderListeners: Record<
@@ -52,8 +55,8 @@ export class FormoAnalytics implements IFormoAnalytics {
52
55
  (...args: unknown[]) => void
53
56
  > = {};
54
57
 
55
- private sessionKey = 'walletAddress';
56
- private config: any;
58
+ private walletAddressSessionKey = 'walletAddress';
59
+ private config: Config;
57
60
  private sessionIdKey: string = SESSION_STORAGE_ID_KEY;
58
61
  private timezoneToCountry: Record<string, string> = COUNTRY_LIST;
59
62
 
@@ -62,13 +65,13 @@ export class FormoAnalytics implements IFormoAnalytics {
62
65
 
63
66
  private constructor(
64
67
  public readonly apiKey: string,
65
- public projectId: string
68
+ public options: Options = {}
66
69
  ) {
67
70
  this.config = {
68
71
  token: this.apiKey,
69
72
  };
70
73
 
71
- const provider = window?.ethereum || window.web3?.currentProvider;
74
+ const provider = window?.ethereum || window.web3?.currentProvider || options?.provider;
72
75
  if (provider) {
73
76
  this.trackProvider(provider);
74
77
  }
@@ -76,12 +79,12 @@ export class FormoAnalytics implements IFormoAnalytics {
76
79
 
77
80
  static async init(
78
81
  apiKey: string,
79
- projectId: string
82
+ options: Options
80
83
  ): Promise<FormoAnalytics> {
81
84
  const config = {
82
85
  token: apiKey,
83
86
  };
84
- const instance = new FormoAnalytics(apiKey, projectId);
87
+ const instance = new FormoAnalytics(apiKey, options);
85
88
  instance.config = config;
86
89
 
87
90
  return instance;
@@ -106,13 +109,14 @@ export class FormoAnalytics implements IFormoAnalytics {
106
109
  return newSessionId;
107
110
  }
108
111
 
112
+ private getOrigin(): string {
113
+ return window.location.origin || 'ORIGIN_NOT_FOUND';
114
+ }
115
+
109
116
  // Function to set the session cookie
110
- private setSessionCookie(domain?: string) {
117
+ private setSessionCookie(): void {
111
118
  const sessionId = this.getSessionId();
112
- let cookieValue = `${this.sessionIdKey}=${sessionId}; Max-Age=1800; path=/; secure`;
113
- if (domain) {
114
- cookieValue += `; domain=${domain}`;
115
- }
119
+ let cookieValue = `${this.sessionIdKey}=${sessionId}; Max-Age=1800; path=/; secure; domain=${this.getOrigin()}`;
116
120
  document.cookie = cookieValue;
117
121
  }
118
122
 
@@ -136,12 +140,10 @@ export class FormoAnalytics implements IFormoAnalytics {
136
140
  const maxRetries = 3;
137
141
  let attempt = 0;
138
142
 
139
- this.setSessionCookie(this.config.domain);
140
- const apiUrl = this.buildApiUrl();
143
+ this.setSessionCookie();
141
144
  const address = await this.getCurrentWallet();
142
145
 
143
146
  const requestData = {
144
- project_id: this.projectId,
145
147
  address: address,
146
148
  session_id: this.getSessionId(),
147
149
  timestamp: new Date().toISOString(),
@@ -152,9 +154,11 @@ export class FormoAnalytics implements IFormoAnalytics {
152
154
 
153
155
  const sendRequest = async (): Promise<void> => {
154
156
  try {
155
- const response = await axios.post(apiUrl, JSON.stringify(requestData), {
157
+ const response = await axios.post(EVENTS_API_URL, JSON.stringify(requestData), {
156
158
  headers: {
157
159
  'Content-Type': 'application/json',
160
+ Authorization: `Bearer ${this.apiKey}`,
161
+ 'Origin': this.getOrigin(),
158
162
  },
159
163
  });
160
164
 
@@ -444,7 +448,7 @@ export class FormoAnalytics implements IFormoAnalytics {
444
448
  console.warn('FormoAnalytics::getCurrentWallet: the provider is not set');
445
449
  return;
446
450
  }
447
- const sessionData = sessionStorage.getItem(this.sessionKey);
451
+ const sessionData = sessionStorage.getItem(this.walletAddressSessionKey);
448
452
 
449
453
  if (!sessionData) {
450
454
  return null;
@@ -456,7 +460,7 @@ export class FormoAnalytics implements IFormoAnalytics {
456
460
 
457
461
  if (currentTime - parsedData.timestamp > sessionExpiry) {
458
462
  console.warn('Session expired. Ignoring wallet address.');
459
- sessionStorage.removeItem(this.sessionKey); // Clear expired session data
463
+ sessionStorage.removeItem(this.walletAddressSessionKey); // Clear expired session data
460
464
  return '';
461
465
  }
462
466
 
@@ -479,32 +483,14 @@ export class FormoAnalytics implements IFormoAnalytics {
479
483
  timestamp: Date.now(),
480
484
  };
481
485
 
482
- sessionStorage.setItem(this.sessionKey, JSON.stringify(sessionData));
486
+ sessionStorage.setItem(this.walletAddressSessionKey, JSON.stringify(sessionData));
483
487
  }
484
488
 
485
489
  /**
486
490
  * Clears the wallet address from session storage when disconnected.
487
491
  */
488
492
  private clearWalletAddress(): void {
489
- sessionStorage.removeItem(this.sessionKey);
490
- }
491
-
492
- // Function to build the API URL
493
- private buildApiUrl(): string {
494
- const { host, proxy, token, dataSource = 'analytics_events' } = this.config;
495
- if (token) {
496
- if (proxy) {
497
- return `${proxy}/api/tracking`;
498
- }
499
- if (host) {
500
- return `${host.replace(
501
- /\/+$/,
502
- ''
503
- )}/v0/events?name=${dataSource}&token=${token}`;
504
- }
505
- return `${EVENTS_API}?name=${dataSource}&token=${token}`;
506
- }
507
- return 'Error: No token provided';
493
+ sessionStorage.removeItem(this.walletAddressSessionKey);
508
494
  }
509
495
 
510
496
  connect({ account, chainId }: { account: string; chainId: ChainID }) {
@@ -569,9 +555,8 @@ export class FormoAnalytics implements IFormoAnalytics {
569
555
  });
570
556
  }
571
557
 
572
- init(apiKey: string, projectId: string): Promise<FormoAnalytics> {
573
- const instance = new FormoAnalytics(apiKey, projectId);
574
-
558
+ init(apiKey: string, options: Options): Promise<FormoAnalytics> {
559
+ const instance = new FormoAnalytics(apiKey, options);
575
560
  return Promise.resolve(instance);
576
561
  }
577
562
 
@@ -18,7 +18,7 @@ export const FormoAnalyticsContext = createContext<FormoAnalytics | undefined>(
18
18
 
19
19
  export const FormoAnalyticsProvider = ({
20
20
  apiKey,
21
- projectId,
21
+ options,
22
22
  disabled,
23
23
  children,
24
24
  }: FormoAnalyticsProviderProps) => {
@@ -64,7 +64,7 @@ export const FormoAnalyticsProvider = ({
64
64
 
65
65
  // Initialize FormoAnalytics
66
66
  try {
67
- const sdkInstance = await FormoAnalytics.init(apiKey, projectId);
67
+ const sdkInstance = await FormoAnalytics.init(apiKey, options);
68
68
  setSdk(sdkInstance);
69
69
  console.log('FormoAnalytics SDK initialized successfully');
70
70
  } catch (error) {
@@ -75,7 +75,7 @@ export const FormoAnalyticsProvider = ({
75
75
  };
76
76
 
77
77
  initialize();
78
- }, [apiKey, disabled, projectId]);
78
+ }, [apiKey, disabled, options]);
79
79
 
80
80
  if (!isInitialized) {
81
81
  // Optionally show a loading state until initialization attempt finishes
@@ -1,4 +1,4 @@
1
- export const EVENTS_API = 'https://api.eu-central-1.aws.tinybird.co/v0/events';
1
+ export const EVENTS_API_URL = 'https://events.formo.so/events';
2
2
  export const SESSION_STORAGE_ID_KEY = 'formo-analytics-session-id';
3
3
  export const COUNTRY_LIST = {
4
4
  // Africa
package/src/types/base.ts CHANGED
@@ -1,8 +1,14 @@
1
+ import { EIP1193Provider } from "./wallet";
2
+
1
3
  export type ChainID = string | number
2
4
 
5
+ export interface Options {
6
+ provider?: EIP1193Provider;
7
+ }
8
+
3
9
  export interface FormoAnalyticsProviderProps {
4
10
  apiKey: string;
5
- projectId: string;
11
+ options: Options;
6
12
  disabled?: boolean;
7
13
  children: React.ReactNode;
8
14
  }