@formo/analytics 1.12.0-alpha.1 → 1.12.0-alpha.3
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/cjs/src/FormoAnalytics.d.ts +17 -8
- package/dist/cjs/src/FormoAnalytics.d.ts.map +1 -1
- package/dist/cjs/src/FormoAnalytics.js +51 -42
- package/dist/cjs/src/FormoAnalytics.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/src/FormoAnalytics.d.ts +17 -8
- package/dist/esm/src/FormoAnalytics.d.ts.map +1 -1
- package/dist/esm/src/FormoAnalytics.js +51 -42
- package/dist/esm/src/FormoAnalytics.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/package.json +1 -1
- package/src/FormoAnalytics.ts +58 -37
package/package.json
CHANGED
package/src/FormoAnalytics.ts
CHANGED
|
@@ -25,21 +25,21 @@ interface IFormoAnalytics {
|
|
|
25
25
|
*/
|
|
26
26
|
page(): void;
|
|
27
27
|
|
|
28
|
-
/**
|
|
29
|
-
* Tracks a specific event with a name and associated data.
|
|
30
|
-
*/
|
|
31
|
-
track(eventName: string, eventData: Record<string, any>): void;
|
|
32
|
-
|
|
33
28
|
/**
|
|
34
29
|
* Connects to a wallet with the specified chain ID and address.
|
|
35
30
|
*/
|
|
36
|
-
connect(params: {
|
|
31
|
+
connect(params: { account: string; chainId: ChainID }): Promise<void>;
|
|
37
32
|
|
|
38
33
|
/**
|
|
39
34
|
* Disconnects the current wallet and clears the session information.
|
|
40
35
|
*/
|
|
41
36
|
disconnect(attributes?: { account?: string; chainId?: ChainID }): void;
|
|
42
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Tracks a specific event with a name and associated data.
|
|
40
|
+
*/
|
|
41
|
+
track(eventName: string, eventData: Record<string, any>): void;
|
|
42
|
+
|
|
43
43
|
/**
|
|
44
44
|
* Switches the blockchain chain context and optionally logs additional attributes.
|
|
45
45
|
*/
|
|
@@ -47,12 +47,12 @@ interface IFormoAnalytics {
|
|
|
47
47
|
}
|
|
48
48
|
export class FormoAnalytics implements IFormoAnalytics {
|
|
49
49
|
private _provider?: EIP1193Provider;
|
|
50
|
-
private _originalRequest?: EIP1193Provider['request'];
|
|
51
50
|
private _registeredProviderListeners: Record<
|
|
52
51
|
string,
|
|
53
52
|
(...args: unknown[]) => void
|
|
54
53
|
> = {};
|
|
55
54
|
|
|
55
|
+
private sessionKey = 'walletAddress';
|
|
56
56
|
private config: any;
|
|
57
57
|
private sessionIdKey: string = SESSION_STORAGE_ID_KEY;
|
|
58
58
|
private timezoneToCountry: Record<string, string> = COUNTRY_LIST;
|
|
@@ -308,19 +308,9 @@ export class FormoAnalytics implements IFormoAnalytics {
|
|
|
308
308
|
);
|
|
309
309
|
delete this._registeredProviderListeners[eventName];
|
|
310
310
|
}
|
|
311
|
-
|
|
312
|
-
// Restore original request
|
|
313
|
-
if (
|
|
314
|
-
this._originalRequest &&
|
|
315
|
-
Object.getOwnPropertyDescriptor(this._provider, 'request')?.writable !==
|
|
316
|
-
false
|
|
317
|
-
) {
|
|
318
|
-
this._provider.request = this._originalRequest;
|
|
319
|
-
}
|
|
320
311
|
}
|
|
321
312
|
|
|
322
313
|
this._provider = provider;
|
|
323
|
-
this._originalRequest = provider?.request;
|
|
324
314
|
|
|
325
315
|
this.getCurrentWallet();
|
|
326
316
|
this.registerAccountsChangedListener();
|
|
@@ -340,11 +330,12 @@ export class FormoAnalytics implements IFormoAnalytics {
|
|
|
340
330
|
}
|
|
341
331
|
|
|
342
332
|
const disconnectAttributes = {
|
|
343
|
-
|
|
333
|
+
address: this.currentConnectedAccount,
|
|
344
334
|
chainId: this.currentChainId,
|
|
345
335
|
};
|
|
346
336
|
this.currentChainId = undefined;
|
|
347
337
|
this.currentConnectedAccount = undefined;
|
|
338
|
+
this.clearWalletAddress();
|
|
348
339
|
|
|
349
340
|
return this.trackEvent(Event.DISCONNECT, disconnectAttributes);
|
|
350
341
|
}
|
|
@@ -362,12 +353,12 @@ export class FormoAnalytics implements IFormoAnalytics {
|
|
|
362
353
|
|
|
363
354
|
try {
|
|
364
355
|
const res: string[] | null | undefined = await this.provider.request({
|
|
365
|
-
method: '
|
|
356
|
+
method: 'eth_accounts',
|
|
366
357
|
});
|
|
367
358
|
if (!res || res.length === 0) {
|
|
368
359
|
console.error(
|
|
369
360
|
'error',
|
|
370
|
-
'FormoAnalytics::onChainChanged: unable to get account.
|
|
361
|
+
'FormoAnalytics::onChainChanged: unable to get account. eth_accounts returned empty'
|
|
371
362
|
);
|
|
372
363
|
return;
|
|
373
364
|
}
|
|
@@ -379,7 +370,7 @@ export class FormoAnalytics implements IFormoAnalytics {
|
|
|
379
370
|
// 4001: The request is rejected by the user , see https://docs.metamask.io/wallet/reference/provider-api/#errors
|
|
380
371
|
console.error(
|
|
381
372
|
'error',
|
|
382
|
-
`FormoAnalytics::onChainChanged: unable to get account.
|
|
373
|
+
`FormoAnalytics::onChainChanged: unable to get account. eth_accounts threw an error`,
|
|
383
374
|
err
|
|
384
375
|
);
|
|
385
376
|
return;
|
|
@@ -444,7 +435,8 @@ export class FormoAnalytics implements IFormoAnalytics {
|
|
|
444
435
|
|
|
445
436
|
this.currentChainId = await this.getCurrentChainId();
|
|
446
437
|
|
|
447
|
-
this.connect({ chainId: this.currentChainId
|
|
438
|
+
this.connect({ account, chainId: this.currentChainId });
|
|
439
|
+
this.storeWalletAddress(account);
|
|
448
440
|
}
|
|
449
441
|
|
|
450
442
|
private async getCurrentWallet() {
|
|
@@ -452,20 +444,49 @@ export class FormoAnalytics implements IFormoAnalytics {
|
|
|
452
444
|
console.warn('FormoAnalytics::getCurrentWallet: the provider is not set');
|
|
453
445
|
return;
|
|
454
446
|
}
|
|
455
|
-
|
|
456
|
-
const accounts = await this.provider.request<string[]>({
|
|
457
|
-
method: 'eth_accounts',
|
|
458
|
-
});
|
|
459
|
-
if (accounts && accounts.length > 0 && accounts[0]) {
|
|
460
|
-
this.handleAccountConnected(accounts[0]);
|
|
461
|
-
return accounts && accounts.length > 0 && accounts[0];
|
|
462
|
-
}
|
|
447
|
+
const sessionData = sessionStorage.getItem(this.sessionKey);
|
|
463
448
|
|
|
449
|
+
if (!sessionData) {
|
|
450
|
+
return null;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const parsedData = JSON.parse(sessionData);
|
|
454
|
+
const sessionExpiry = 30 * 60 * 1000; // 30 minutes
|
|
455
|
+
const currentTime = Date.now();
|
|
456
|
+
|
|
457
|
+
if (currentTime - parsedData.timestamp > sessionExpiry) {
|
|
458
|
+
console.warn('Session expired. Ignoring wallet address.');
|
|
459
|
+
sessionStorage.removeItem(this.sessionKey); // Clear expired session data
|
|
464
460
|
return '';
|
|
465
|
-
} catch (error) {
|
|
466
|
-
console.error('Failed to fetch connected address:', error);
|
|
467
|
-
return '';
|
|
468
461
|
}
|
|
462
|
+
|
|
463
|
+
this.handleAccountConnected(parsedData.address);
|
|
464
|
+
return parsedData.address || '';
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Stores the wallet address in session storage when connected.
|
|
469
|
+
* @param address - The wallet address to store.
|
|
470
|
+
*/
|
|
471
|
+
private storeWalletAddress(address: string): void {
|
|
472
|
+
if (!address) {
|
|
473
|
+
console.error('No wallet address provided to store.');
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
const sessionData = {
|
|
478
|
+
address,
|
|
479
|
+
timestamp: Date.now(),
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
sessionStorage.setItem(this.sessionKey, JSON.stringify(sessionData));
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* Clears the wallet address from session storage when disconnected.
|
|
487
|
+
*/
|
|
488
|
+
private clearWalletAddress(): void {
|
|
489
|
+
sessionStorage.removeItem(this.sessionKey);
|
|
469
490
|
}
|
|
470
491
|
|
|
471
492
|
// Function to build the API URL
|
|
@@ -486,20 +507,20 @@ export class FormoAnalytics implements IFormoAnalytics {
|
|
|
486
507
|
return 'Error: No token provided';
|
|
487
508
|
}
|
|
488
509
|
|
|
489
|
-
connect({
|
|
510
|
+
connect({ account, chainId }: { account: string; chainId: ChainID }) {
|
|
490
511
|
if (!chainId) {
|
|
491
512
|
throw new Error('FormoAnalytics::connect: chainId cannot be empty');
|
|
492
513
|
}
|
|
493
|
-
if (!
|
|
514
|
+
if (!account) {
|
|
494
515
|
throw new Error('FormoAnalytics::connect: account cannot be empty');
|
|
495
516
|
}
|
|
496
517
|
|
|
497
518
|
this.currentChainId = chainId.toString();
|
|
498
|
-
this.currentConnectedAccount =
|
|
519
|
+
this.currentConnectedAccount = account;
|
|
499
520
|
|
|
500
521
|
return this.trackEvent(Event.CONNECT, {
|
|
501
522
|
chainId,
|
|
502
|
-
address,
|
|
523
|
+
address: account,
|
|
503
524
|
});
|
|
504
525
|
}
|
|
505
526
|
|