@formo/analytics 1.11.9 → 1.11.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formo/analytics",
3
- "version": "1.11.9",
3
+ "version": "1.11.10",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/getformo/sdk.git"
@@ -12,11 +12,7 @@ interface IFormoAnalytics {
12
12
  /**
13
13
  * Initializes the FormoAnalytics instance with the provided API key and project ID.
14
14
  */
15
- init(
16
- apiKey: string,
17
- projectId: string,
18
- options?: Options
19
- ): Promise<FormoAnalytics>;
15
+ init(apiKey: string, options?: Options): Promise<FormoAnalytics>;
20
16
 
21
17
  /**
22
18
  * Tracks page visit events.
@@ -63,7 +59,6 @@ export class FormoAnalytics implements IFormoAnalytics {
63
59
 
64
60
  private constructor(
65
61
  public readonly apiKey: string,
66
- public readonly projectId: string,
67
62
  public options: Options = {}
68
63
  ) {
69
64
  this.config = {
@@ -79,13 +74,12 @@ export class FormoAnalytics implements IFormoAnalytics {
79
74
 
80
75
  static async init(
81
76
  apiKey: string,
82
- projectId: string,
83
77
  options?: Options
84
78
  ): Promise<FormoAnalytics> {
85
79
  const config = {
86
80
  token: apiKey,
87
81
  };
88
- const instance = new FormoAnalytics(apiKey, projectId, options);
82
+ const instance = new FormoAnalytics(apiKey, options);
89
83
  instance.config = config;
90
84
 
91
85
  return instance;
@@ -260,6 +254,23 @@ export class FormoAnalytics implements IFormoAnalytics {
260
254
  this.registerChainChangedListener();
261
255
  }
262
256
 
257
+ private async getAndStoreConnectedAddress(): Promise<string | null> {
258
+ console.warn(
259
+ 'Session data missing. Attempting to fetch address from provider.'
260
+ );
261
+ try {
262
+ const accounts = await this.fetchAccounts();
263
+ if (accounts && accounts.length > 0) {
264
+ const address = accounts[0];
265
+ this.storeWalletAddress(address);
266
+ return address;
267
+ }
268
+ } catch (err) {
269
+ console.error('Failed to fetch accounts from provider:', err);
270
+ }
271
+ return null;
272
+ }
273
+
263
274
  private async getCurrentWallet() {
264
275
  if (!this.provider) {
265
276
  console.warn('FormoAnalytics::getCurrentWallet: the provider is not set');
@@ -269,22 +280,7 @@ export class FormoAnalytics implements IFormoAnalytics {
269
280
  const sessionData = sessionStorage.getItem(this.walletAddressSessionKey);
270
281
 
271
282
  if (!sessionData) {
272
- console.warn(
273
- 'Session data missing. Attempting to fetch address from provider.'
274
- );
275
- try {
276
- const accounts = await this.provider.request<string[]>({
277
- method: 'eth_accounts',
278
- });
279
- if (accounts && accounts.length > 0) {
280
- const address = accounts[0];
281
- this.storeWalletAddress(address);
282
- return address;
283
- }
284
- } catch (err) {
285
- console.error('Failed to fetch accounts from provider:', err);
286
- }
287
- return null;
283
+ return await this.getAndStoreConnectedAddress();
288
284
  }
289
285
 
290
286
  const parsedData = JSON.parse(sessionData);
@@ -301,13 +297,61 @@ export class FormoAnalytics implements IFormoAnalytics {
301
297
  return parsedData.address || '';
302
298
  }
303
299
 
300
+ // Utility to fetch accounts
301
+ private async fetchAccounts(): Promise<string[] | null> {
302
+ try {
303
+ const res: string[] | null | undefined = await this.provider?.request({
304
+ method: 'eth_accounts',
305
+ });
306
+ if (!res || res.length === 0) {
307
+ console.error(
308
+ 'error',
309
+ 'FormoAnalytics::fetchAccounts: unable to get account. eth_accounts returned empty'
310
+ );
311
+ return null;
312
+ }
313
+ return res;
314
+ } catch (err) {
315
+ if ((err as any).code !== 4001) {
316
+ console.error(
317
+ 'error',
318
+ 'FormoAnalytics::fetchAccounts: eth_accounts threw an error',
319
+ err
320
+ );
321
+ }
322
+ return null;
323
+ }
324
+ }
325
+
326
+ // Utility to fetch chain ID
327
+ private async fetchChainId(): Promise<string | null> {
328
+ try {
329
+ const chainIdHex = await this.provider?.request<string>({
330
+ method: 'eth_chainId',
331
+ });
332
+ if (!chainIdHex) {
333
+ console.error(
334
+ 'FormoAnalytics::fetchChainId: chainIdHex is null or undefined'
335
+ );
336
+ return null;
337
+ }
338
+ return chainIdHex;
339
+ } catch (err) {
340
+ console.error(
341
+ 'error',
342
+ 'FormoAnalytics::fetchChainId: eth_chainId threw an error',
343
+ err
344
+ );
345
+ return null;
346
+ }
347
+ }
348
+
304
349
  private async getCurrentChainId(): Promise<string> {
305
350
  if (!this.provider) {
306
351
  console.error('FormoAnalytics::getCurrentChainId: provider not set');
307
352
  }
308
- const chainIdHex = await this.provider?.request<string>({
309
- method: 'eth_chainId',
310
- });
353
+
354
+ const chainIdHex = await this.fetchChainId();
311
355
  // Because we're connected, the chainId cannot be null
312
356
  if (!chainIdHex) {
313
357
  console.error(
@@ -390,9 +434,8 @@ export class FormoAnalytics implements IFormoAnalytics {
390
434
  }
391
435
 
392
436
  try {
393
- const res: string[] | null | undefined = await this.provider.request({
394
- method: 'eth_accounts',
395
- });
437
+ const res: string[] | null | undefined = await this.fetchAccounts();
438
+
396
439
  if (!res || res.length === 0) {
397
440
  console.error(
398
441
  'error',
@@ -450,8 +493,8 @@ export class FormoAnalytics implements IFormoAnalytics {
450
493
  sessionStorage.removeItem(this.walletAddressSessionKey);
451
494
  }
452
495
 
453
- init(apiKey: string, projectId: string, options: Options): Promise<FormoAnalytics> {
454
- const instance = new FormoAnalytics(apiKey, projectId, options);
496
+ init(apiKey: string, options: Options): Promise<FormoAnalytics> {
497
+ const instance = new FormoAnalytics(apiKey, options);
455
498
  return Promise.resolve(instance);
456
499
  }
457
500
 
@@ -19,7 +19,6 @@ export const FormoAnalyticsContext = createContext<FormoAnalytics | undefined>(
19
19
  export const FormoAnalyticsProvider = ({
20
20
  apiKey,
21
21
  options,
22
- projectId,
23
22
  disabled,
24
23
  children,
25
24
  }: FormoAnalyticsProviderProps) => {
@@ -62,7 +61,7 @@ export const FormoAnalyticsProvider = ({
62
61
 
63
62
  // Initialize FormoAnalytics
64
63
  try {
65
- const sdkInstance = await FormoAnalytics.init(apiKey, projectId, options);
64
+ const sdkInstance = await FormoAnalytics.init(apiKey, options);
66
65
  setSdk(sdkInstance);
67
66
  console.log('FormoAnalytics SDK initialized successfully');
68
67
  } catch (error) {
package/src/types/base.ts CHANGED
@@ -8,7 +8,6 @@ export interface Options {
8
8
 
9
9
  export interface FormoAnalyticsProviderProps {
10
10
  apiKey: string;
11
- projectId: string;
12
11
  options?: Options;
13
12
  disabled?: boolean;
14
13
  children: React.ReactNode;