Skip to content

Commit c3157b0

Browse files
committed
Analytics: update error message and code in events
1 parent dd4f423 commit c3157b0

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

packages/modal/src/modalManager.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
CONNECTOR_STATUS,
1616
fetchProjectConfig,
1717
fetchWalletRegistry,
18+
getErrorAnalyticsProperties,
1819
type IConnector,
1920
type IProvider,
2021
type IWeb3AuthCoreOptions,
@@ -28,7 +29,6 @@ import {
2829
WALLET_CONNECTORS,
2930
WalletInitializationError,
3031
type WalletRegistry,
31-
Web3AuthError,
3232
Web3AuthNoModal,
3333
withAbort,
3434
} from "@web3auth/no-modal";
@@ -154,14 +154,13 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
154154
if (error instanceof DOMException && error.name === "AbortError") return;
155155

156156
// track failure event
157-
const serializedError = await serializeError(error);
158157
this.analytics.track(ANALYTICS_EVENTS.SDK_INITIALIZATION_FAILED, {
159158
...trackData,
159+
...getErrorAnalyticsProperties(error),
160160
duration: Date.now() - startTime,
161-
error_code: error instanceof Web3AuthError ? error.code : undefined,
162-
error_message: serializedError.message,
163161
});
164162
log.error("Failed to initialize modal", error);
163+
throw error;
165164
}
166165
}
167166

packages/no-modal/src/base/analytics.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AnalyticsBrowser, type EventProperties, type UserTraits } from "@segmen
22

33
import { log } from "./loglevel";
44

5-
const SEGMENT_WRITE_KEY = "gGjtk5XxaH2OAIlErcBgydrHpoRZ2hkZ"; // TODO: use the production key
5+
const SEGMENT_WRITE_KEY = "rpE5pCcpA6ME2oFu2TbuVydhOXapjHs3"; // TODO: this is dev key, use the production key
66

77
export class Analytics {
88
private segment: AnalyticsBrowser;
@@ -93,7 +93,7 @@ export const ANALYTICS_EVENTS = {
9393
MFA_ENABLEMENT_STARTED: "MFA Enablement Started",
9494
MFA_ENABLEMENT_COMPLETED: "MFA Enablement Completed",
9595
MFA_ENABLEMENT_FAILED: "MFA Enablement Failed",
96-
MFA_MANAGEMENT_STARTED: "MFA Management Started",
96+
MFA_MANAGEMENT_SELECTED: "MFA Management Selected",
9797
MFA_MANAGEMENT_FAILED: "MFA Management Failed",
9898
LOGIN_MODAL_OPENED: "Login Modal Opened",
9999
LOGIN_MODAL_CLOSED: "Login Modal Closed",

packages/no-modal/src/base/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { type Chain } from "viem";
88
import { type CustomChainConfig } from "./chain/IChainInterface";
99
import { WEB3AUTH_NETWORK, type WEB3AUTH_NETWORK_TYPE } from "./connector";
1010
import { type UIConfig, type WalletServicesConfig } from "./core/IWeb3Auth";
11+
import { Web3AuthError } from "./errors";
1112
import type { ProjectConfig, WalletRegistry } from "./interfaces";
1213

1314
export const isHexStrict = (hex: string): boolean => {
@@ -161,3 +162,13 @@ export const getWalletServicesAnalyticsProperties = (walletServicesConfig?: Wall
161162
};
162163

163164
export const sdkVersion = process.env.WEB3AUTH_VERSION;
165+
166+
export const getErrorAnalyticsProperties = (error: unknown): { error_message?: string; error_code?: number } => {
167+
try {
168+
const code = error instanceof Web3AuthError ? error.code : (error as { code?: number })?.code;
169+
const message = error instanceof Error ? error.message : (error as { message?: string })?.message || error?.toString();
170+
return { error_message: message, error_code: code };
171+
} catch {
172+
return { error_message: "Unknown error", error_code: undefined };
173+
}
174+
};

packages/no-modal/src/noModal.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
type CustomChainConfig,
2020
fetchProjectConfig,
2121
getAaAnalyticsProperties,
22+
getErrorAnalyticsProperties,
2223
getWalletServicesAnalyticsProperties,
2324
getWhitelabelAnalyticsProperties,
2425
type IBaseProvider,
@@ -220,14 +221,13 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
220221
if (error instanceof DOMException && error.name === "AbortError") return;
221222

222223
// track failure event
223-
const serializedError = await serializeError(error);
224224
this.analytics.track(ANALYTICS_EVENTS.SDK_INITIALIZATION_FAILED, {
225225
...trackData,
226+
...getErrorAnalyticsProperties(error),
226227
duration: Date.now() - startTime,
227-
error_code: error instanceof Web3AuthError ? error.code : undefined,
228-
error_message: serializedError.message,
229228
});
230229
log.error("Failed to initialize modal", error);
230+
throw error;
231231
}
232232
}
233233

@@ -337,11 +337,9 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
337337
});
338338
this.once(CONNECTOR_EVENTS.ERRORED, async (err) => {
339339
// track connection failed event
340-
const serializedError = await serializeError(err);
341340
this.analytics.track(ANALYTICS_EVENTS.CONNECTION_FAILED, {
342341
...eventData,
343-
error_code: err instanceof Web3AuthError ? err.code : undefined,
344-
error_message: serializedError.message,
342+
...getErrorAnalyticsProperties(err),
345343
duration: Date.now() - startTime,
346344
});
347345
reject(err);
@@ -373,11 +371,9 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
373371
this.analytics.track(ANALYTICS_EVENTS.MFA_ENABLEMENT_STARTED, trackData);
374372
await this.connectedConnector.enableMFA(loginParams);
375373
} catch (error) {
376-
const serializedError = await serializeError(error);
377374
this.analytics.track(ANALYTICS_EVENTS.MFA_ENABLEMENT_FAILED, {
378375
...trackData,
379-
error_code: error instanceof Web3AuthError ? error.code : undefined,
380-
error_message: serializedError.message,
376+
...getErrorAnalyticsProperties(error),
381377
});
382378
throw error;
383379
}
@@ -391,14 +387,12 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
391387
const authConnector = this.connectedConnector as AuthConnectorType;
392388
const trackData = { connector: this.connectedConnector.name, auth_ux_mode: authConnector.authInstance?.options?.uxMode };
393389
try {
394-
this.analytics.track(ANALYTICS_EVENTS.MFA_MANAGEMENT_STARTED, trackData);
390+
this.analytics.track(ANALYTICS_EVENTS.MFA_MANAGEMENT_SELECTED, trackData);
395391
await this.connectedConnector.manageMFA(loginParams);
396392
} catch (error) {
397-
const serializedError = await serializeError(error);
398393
this.analytics.track(ANALYTICS_EVENTS.MFA_MANAGEMENT_FAILED, {
399394
...trackData,
400-
error_code: error instanceof Web3AuthError ? error.code : undefined,
401-
error_message: serializedError.message,
395+
...getErrorAnalyticsProperties(error),
402396
});
403397
throw error;
404398
}
@@ -414,11 +408,9 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
414408
this.analytics.track(ANALYTICS_EVENTS.IDENTITY_TOKEN_COMPLETED, trackData);
415409
return identityToken;
416410
} catch (error) {
417-
const serializedError = await serializeError(error);
418411
this.analytics.track(ANALYTICS_EVENTS.IDENTITY_TOKEN_FAILED, {
419412
...trackData,
420-
error_code: error instanceof Web3AuthError ? error.code : undefined,
421-
error_message: serializedError.message,
413+
...getErrorAnalyticsProperties(error),
422414
});
423415
throw error;
424416
}

0 commit comments

Comments
 (0)