@formo/analytics 1.11.10 → 1.11.11

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.10",
3
+ "version": "1.11.11",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/getformo/sdk.git"
@@ -191,39 +191,76 @@ export class FormoAnalytics implements IFormoAnalytics {
191
191
 
192
192
  // Function to track page hits
193
193
  private trackPageHit() {
194
- if (window.__nightmare || window.navigator.webdriver || window.Cypress)
195
- return;
194
+ if (this.isAutomationEnvironment()) return;
195
+
196
+ const location = this.getUserLocation();
197
+ const language = this.getUserLanguage();
198
+
199
+ setTimeout(async () => {
200
+ const eventData = await this.buildPageEventData(location, language);
201
+ this.trackEvent(Event.PAGE, eventData);
202
+ }, 300);
203
+ }
196
204
 
197
- let location: string | undefined;
198
- let language: string;
205
+ private isAutomationEnvironment(): boolean {
206
+ return (
207
+ window.__nightmare ||
208
+ window.navigator.webdriver ||
209
+ window.Cypress ||
210
+ false
211
+ );
212
+ }
213
+
214
+ private getUserLocation(): string | undefined {
199
215
  try {
200
216
  const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
201
- location = this.timezoneToCountry[timezone];
202
- language =
203
- navigator.languages && navigator.languages.length
217
+ return this.timezoneToCountry[timezone];
218
+ } catch (error) {
219
+ console.error('Error resolving timezone:', error);
220
+ return undefined;
221
+ }
222
+ }
223
+
224
+ private getUserLanguage(): string {
225
+ try {
226
+ return (
227
+ (navigator.languages && navigator.languages.length
204
228
  ? navigator.languages[0]
205
- : navigator.language || 'en';
229
+ : navigator.language) || 'en'
230
+ );
206
231
  } catch (error) {
207
- console.error('Error resolving timezone or language:', error);
232
+ console.error('Error resolving language:', error);
233
+ return 'en';
208
234
  }
235
+ }
209
236
 
210
- setTimeout(() => {
211
- const url = new URL(window.location.href);
212
- const params = new URLSearchParams(url.search);
213
- this.trackEvent(Event.PAGE, {
214
- 'user-agent': window.navigator.userAgent,
215
- address: this.currentConnectedAddress,
216
- locale: language,
217
- location: location,
218
- referrer: document.referrer,
219
- pathname: window.location.pathname,
220
- href: window.location.href,
221
- utm_source: params.get('utm_source'),
222
- utm_medium: params.get('utm_medium'),
223
- utm_campaign: params.get('utm_campaign'),
224
- ref: params.get('ref'),
225
- });
226
- }, 300);
237
+ async buildPageEventData(location: string | undefined, language: string): Promise<Record<string, unknown>> {
238
+ const url = new URL(window.location.href);
239
+ const params = new URLSearchParams(url.search);
240
+
241
+ const address = await this.getAndStoreConnectedAddress();
242
+ if (address === null) {
243
+ console.warn('Wallet address could not be retrieved.');
244
+ }
245
+
246
+ const eventData: Record<string, unknown> = {
247
+ 'user-agent': window.navigator.userAgent,
248
+ locale: language,
249
+ location,
250
+ referrer: document.referrer,
251
+ pathname: window.location.pathname,
252
+ href: window.location.href,
253
+ utm_source: params.get('utm_source'),
254
+ utm_medium: params.get('utm_medium'),
255
+ utm_campaign: params.get('utm_campaign'),
256
+ ref: params.get('ref'),
257
+ };
258
+
259
+ if (address !== null) {
260
+ eventData['address'] = address;
261
+ }
262
+
263
+ return eventData;
227
264
  }
228
265
 
229
266
  private trackProvider(provider: EIP1193Provider) {
@@ -413,7 +450,7 @@ export class FormoAnalytics implements IFormoAnalytics {
413
450
 
414
451
  const payload = {
415
452
  chain_id: this.currentChainId,
416
- address: this.currentConnectedAddress,
453
+ address: this.getAndStoreConnectedAddress(),
417
454
  };
418
455
  this.currentChainId = undefined;
419
456
  this.currentConnectedAddress = undefined;
@@ -433,36 +470,31 @@ export class FormoAnalytics implements IFormoAnalytics {
433
470
  return;
434
471
  }
435
472
 
436
- try {
437
- const res: string[] | null | undefined = await this.fetchAccounts();
438
-
439
- if (!res || res.length === 0) {
440
- console.error(
441
- 'error',
442
- 'FormoAnalytics::onChainChanged: unable to get account. eth_accounts returned empty'
443
- );
444
- return;
445
- }
446
-
447
- this.currentConnectedAddress = res[0];
448
- } catch (err) {
449
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
450
- if ((err as any).code !== 4001) {
451
- // 4001: The request is rejected by the user , see https://docs.metamask.io/wallet/reference/provider-api/#errors
452
- console.error(
453
- 'error',
454
- `FormoAnalytics::onChainChanged: unable to get account. eth_accounts threw an error`,
455
- err
456
- );
457
- return;
458
- }
473
+ // Attempt to fetch and store the connected address
474
+ const address = await this.getAndStoreConnectedAddress();
475
+ if (!address) {
476
+ console.error(
477
+ 'error',
478
+ 'FormoAnalytics::onChainChanged: Unable to fetch or store connected address'
479
+ );
480
+ return;
459
481
  }
482
+
483
+ this.currentConnectedAddress = address[0];
460
484
  }
461
485
 
462
- return this.chain({
463
- chainId: this.currentChainId,
464
- address: this.currentConnectedAddress,
465
- });
486
+ // Proceed only if the address exists
487
+ if (this.currentConnectedAddress) {
488
+ return this.chain({
489
+ chainId: this.currentChainId,
490
+ address: this.currentConnectedAddress,
491
+ });
492
+ } else {
493
+ console.error(
494
+ 'error',
495
+ 'FormoAnalytics::onChainChanged: currentConnectedAddress is null despite fetch attempt'
496
+ );
497
+ }
466
498
  }
467
499
 
468
500
  /**