1- import { signChallenge , verifySignedChallenge } from "@toruslabs/base-controllers" ;
1+ import { getErrorAnalyticsProperties , signChallenge , verifySignedChallenge } from "@toruslabs/base-controllers" ;
22import Client from "@walletconnect/sign-client" ;
33import { SessionTypes } from "@walletconnect/types" ;
44import { getSdkError , isValidArray } from "@walletconnect/utils" ;
55import { EVM_METHOD_TYPES , SOLANA_METHOD_TYPES } from "@web3auth/ws-embed" ;
66import deepmerge from "deepmerge" ;
77
88import {
9+ type Analytics ,
10+ ANALYTICS_EVENTS ,
911 BaseConnector ,
1012 BaseConnectorLoginParams ,
1113 CHAIN_NAMESPACES ,
@@ -23,6 +25,7 @@ import {
2325 ConnectorNamespaceType ,
2426 ConnectorParams ,
2527 CustomChainConfig ,
28+ getCaipChainId ,
2629 getSavedToken ,
2730 IdentityTokenInfo ,
2831 IProvider ,
@@ -61,6 +64,8 @@ class WalletConnectV2Connector extends BaseConnector<void> {
6164
6265 private wcProvider : WalletConnectV2Provider | null = null ;
6366
67+ private analytics ?: Analytics ;
68+
6469 constructor ( options : WalletConnectV2ConnectorOptions ) {
6570 super ( options ) ;
6671 this . connectorOptions = { ...options } ;
@@ -72,6 +77,8 @@ class WalletConnectV2Connector extends BaseConnector<void> {
7277 loginSettings : this . connectorOptions ?. loginSettings ?? { } ,
7378 } ;
7479
80+ this . analytics = options . analytics ;
81+
7582 if ( qrcodeModal ) this . connectorOptions . connectorSettings . qrcodeModal = qrcodeModal ;
7683 if ( walletConnectInitOptions )
7784 this . connectorOptions . connectorSettings . walletConnectInitOptions = {
@@ -151,16 +158,41 @@ class WalletConnectV2Connector extends BaseConnector<void> {
151158 if ( ! chainConfig ) throw WalletLoginError . connectionError ( "Chain config is not available" ) ;
152159 if ( ! this . connector ) throw WalletInitializationError . notReady ( "Wallet connector is not ready yet" ) ;
153160
161+ // Skip tracking for rehydration since only new connections are tracked
162+ // Track when connection completes since it auto-initializes to generate QR code
163+ const shouldTrack = ! this . connected && ! this . rehydrated ;
164+ const startTime = Date . now ( ) ;
165+ const eventData = {
166+ connector : this . name ,
167+ connector_type : this . type ,
168+ is_injected : this . isInjected ,
169+ chain_id : getCaipChainId ( chainConfig ) ,
170+ chain_name : chainConfig ?. displayName ,
171+ chain_namespace : chainConfig ?. chainNamespace ,
172+ } ;
173+
154174 try {
175+ const trackCompletionEvents = ( ) => {
176+ // track connection events
177+ if ( shouldTrack ) {
178+ this . analytics ?. track ( ANALYTICS_EVENTS . CONNECTION_STARTED , eventData ) ;
179+ this . analytics ?. track ( ANALYTICS_EVENTS . CONNECTION_COMPLETED , {
180+ ...eventData ,
181+ duration : Date . now ( ) - startTime ,
182+ } ) ;
183+ }
184+ } ;
185+
155186 // if already connected
156187 if ( this . connected ) {
157188 await this . onConnectHandler ( { chain : chainConfig } ) ;
158189 return this . provider ;
159190 }
160191
161192 if ( this . status !== CONNECTOR_STATUS . CONNECTING ) {
162- await this . createNewSession ( { chainConfig } ) ;
193+ await this . createNewSession ( { chainConfig, trackCompletionEvents } ) ;
163194 }
195+
164196 return this . provider ;
165197 } catch ( error ) {
166198 log . error ( "Wallet connect v2 connector error while connecting" , error ) ;
@@ -169,6 +201,16 @@ class WalletConnectV2Connector extends BaseConnector<void> {
169201 this . rehydrated = true ;
170202 this . emit ( CONNECTOR_EVENTS . ERRORED , error as Web3AuthError ) ;
171203
204+ // track connection events
205+ if ( shouldTrack ) {
206+ this . analytics ?. track ( ANALYTICS_EVENTS . CONNECTION_STARTED , eventData ) ;
207+ this . analytics ?. track ( ANALYTICS_EVENTS . CONNECTION_COMPLETED , {
208+ ...eventData ,
209+ ...getErrorAnalyticsProperties ( error ) ,
210+ duration : Date . now ( ) - startTime ,
211+ } ) ;
212+ }
213+
172214 const finalError =
173215 error instanceof Web3AuthError
174216 ? error
@@ -293,9 +335,11 @@ class WalletConnectV2Connector extends BaseConnector<void> {
293335 private async createNewSession ( {
294336 forceNewSession = false ,
295337 chainConfig,
338+ trackCompletionEvents,
296339 } : {
297340 forceNewSession ?: boolean ;
298341 chainConfig : CustomChainConfig ;
342+ trackCompletionEvents ?: ( ) => void ;
299343 } ) : Promise < void > {
300344 try {
301345 if ( ! this . connector ) throw WalletInitializationError . notReady ( "Wallet connector is not ready yet" ) ;
@@ -334,7 +378,7 @@ class WalletConnectV2Connector extends BaseConnector<void> {
334378 const session = await approval ( ) ;
335379 this . activeSession = session ;
336380 // Handle the returned session (e.g. update UI to "connected" state).
337- await this . onConnectHandler ( { chain : chainConfig } ) ;
381+ await this . onConnectHandler ( { chain : chainConfig , trackCompletionEvents } ) ;
338382 if ( qrcodeModal ) {
339383 qrcodeModal . closeModal ( ) ;
340384 }
@@ -357,7 +401,7 @@ class WalletConnectV2Connector extends BaseConnector<void> {
357401 }
358402 }
359403
360- private async onConnectHandler ( { chain } : { chain : CustomChainConfig } ) {
404+ private async onConnectHandler ( { chain, trackCompletionEvents } : { chain : CustomChainConfig ; trackCompletionEvents ?: ( ) => void } ) {
361405 if ( ! this . connector || ! this . wcProvider ) throw WalletInitializationError . notReady ( "Wallet connect connector is not ready yet" ) ;
362406 this . subscribeEvents ( ) ;
363407 if ( this . connectorOptions . connectorSettings ?. qrcodeModal ) {
@@ -369,6 +413,10 @@ class WalletConnectV2Connector extends BaseConnector<void> {
369413 await this . wcProvider . setupProvider ( this . connector ) ;
370414 this . cleanupPendingPairings ( ) ;
371415 this . status = CONNECTOR_STATUS . CONNECTED ;
416+
417+ // track connection events
418+ if ( trackCompletionEvents ) trackCompletionEvents ( ) ;
419+
372420 this . emit ( CONNECTOR_EVENTS . CONNECTED , {
373421 connector : WALLET_CONNECTORS . WALLET_CONNECT_V2 ,
374422 reconnected : this . rehydrated ,
@@ -405,7 +453,7 @@ class WalletConnectV2Connector extends BaseConnector<void> {
405453}
406454
407455export const walletConnectV2Connector = ( params ?: IConnectorSettings ) : ConnectorFn => {
408- return ( { coreOptions, projectConfig } : ConnectorParams ) => {
456+ return ( { coreOptions, projectConfig, analytics } : ConnectorParams ) => {
409457 const projectId = params ?. walletConnectInitOptions ?. projectId || projectConfig ?. walletConnectProjectId ;
410458
411459 const connectorSettings = {
@@ -416,6 +464,7 @@ export const walletConnectV2Connector = (params?: IConnectorSettings): Connector
416464 return new WalletConnectV2Connector ( {
417465 connectorSettings,
418466 coreOptions,
467+ analytics,
419468 } ) ;
420469 } ;
421470} ;
0 commit comments