11import { AuthConnectionConfigItem , serializeError } from "@web3auth/auth" ;
22import {
3- Analytics ,
3+ ANALYTICS_EVENTS ,
4+ ANALYTICS_SDK_TYPE ,
45 type AUTH_CONNECTION_TYPE ,
56 type AuthLoginParams ,
67 type BaseConnectorConfig ,
@@ -23,6 +24,7 @@ import {
2324 WALLET_CONNECTORS ,
2425 WalletInitializationError ,
2526 type WalletRegistry ,
27+ Web3AuthError ,
2628 Web3AuthNoModal ,
2729 withAbort ,
2830} from "@web3auth/no-modal" ;
@@ -62,64 +64,105 @@ export class Web3Auth extends Web3AuthNoModal implements IWeb3AuthModal {
6264 }
6365
6466 public async initModal ( options ?: { signal ?: AbortSignal } ) : Promise < void > {
65- const { signal } = options || { } ;
66-
67- super . checkInitRequirements ( ) ;
68- // get project config and wallet registry
69- const { projectConfig, walletRegistry } = await this . getProjectAndWalletConfig ( ) ;
70-
71- // init config
72- this . initUIConfig ( projectConfig ) ;
73- super . initAccountAbstractionConfig ( projectConfig ) ;
74- super . initChainsConfig ( projectConfig ) ;
75- super . initCachedConnectorAndChainId ( ) ;
76-
7767 // init analytics
78- Analytics . init ( ) ;
79- Analytics . identify ( window . location . hostname , {
68+ const startTime = Date . now ( ) ;
69+ this . analytics . init ( ) ;
70+ this . analytics . identify ( this . options . clientId , {
8071 web3auth_client_id : this . options . clientId ,
8172 web3auth_network : this . options . web3AuthNetwork ,
8273 } ) ;
74+ this . analytics . setGlobalProperties ( {
75+ dapp_url : window . location . origin ,
76+ sdk_type : ANALYTICS_SDK_TYPE . WEB_MODAL ,
77+ } ) ;
78+ let trackData : Record < string , unknown > = { } ;
8379
84- // init login modal
85- const { filteredWalletRegistry, disabledExternalWallets } = this . filterWalletRegistry ( walletRegistry , projectConfig ) ;
86- this . loginModal = new LoginModal (
87- {
88- ...this . options . uiConfig ,
89- connectorListener : this ,
90- web3authClientId : this . options . clientId ,
91- web3authNetwork : this . options . web3AuthNetwork ,
92- authBuildEnv : this . options . authBuildEnv ,
93- chainNamespaces : this . getChainNamespaces ( ) ,
94- walletRegistry : filteredWalletRegistry ,
95- } ,
96- {
97- onInitExternalWallets : this . onInitExternalWallets ,
98- onSocialLogin : this . onSocialLogin ,
99- onExternalWalletLogin : this . onExternalWalletLogin ,
100- onModalVisibility : this . onModalVisibility ,
101- }
102- ) ;
103- await withAbort ( ( ) => this . loginModal . initModal ( ) , signal ) ;
104-
105- // setup common JRPC provider
106- await withAbort ( ( ) => this . setupCommonJRPCProvider ( ) , signal ) ;
80+ try {
81+ const { signal } = options || { } ;
82+
83+ super . checkInitRequirements ( ) ;
84+ // get project config and wallet registry
85+ const { projectConfig, walletRegistry } = await this . getProjectAndWalletConfig ( ) ;
86+
87+ // init config
88+ this . initUIConfig ( projectConfig ) ;
89+ super . initAccountAbstractionConfig ( projectConfig ) ;
90+ super . initChainsConfig ( projectConfig ) ;
91+ super . initCachedConnectorAndChainId ( ) ;
92+ trackData = {
93+ chains : this . coreOptions . chains . map ( ( chain ) => chain . chainId ) ,
94+ rpc_urls : this . coreOptions . chains . map ( ( chain ) => chain . rpcTarget ) ,
95+ storage_type : this . coreOptions . storageType ,
96+ session_time : this . coreOptions . sessionTime ,
97+ is_core_kit_key_used : this . coreOptions . useCoreKitKey ,
98+ whitelabel : this . coreOptions . uiConfig , // TODO: flatten this
99+ account_abstraction : this . coreOptions . accountAbstractionConfig , // TODO: flatten this
100+ is_aa_enabled_for_external_wallets : this . coreOptions . useAAWithExternalWallet ,
101+ is_mipd_enabled : this . coreOptions . multiInjectedProviderDiscovery ,
102+ wallet_services : this . coreOptions . walletServicesConfig , // TODO: flatten this
103+ } ;
107104
108- // initialize connectors
109- this . on ( CONNECTOR_EVENTS . CONNECTORS_UPDATED , ( { connectors : newConnectors } ) => {
110- const onAbortHandler = ( ) => {
111- log . debug ( "init aborted" ) ;
112- if ( this . connectors ?. length > 0 ) {
113- super . cleanup ( ) ;
105+ // init login modal
106+ const { filteredWalletRegistry, disabledExternalWallets } = this . filterWalletRegistry ( walletRegistry , projectConfig ) ;
107+ this . loginModal = new LoginModal (
108+ {
109+ ...this . options . uiConfig ,
110+ connectorListener : this ,
111+ web3authClientId : this . options . clientId ,
112+ web3authNetwork : this . options . web3AuthNetwork ,
113+ authBuildEnv : this . options . authBuildEnv ,
114+ chainNamespaces : this . getChainNamespaces ( ) ,
115+ walletRegistry : filteredWalletRegistry ,
116+ } ,
117+ {
118+ onInitExternalWallets : this . onInitExternalWallets ,
119+ onSocialLogin : this . onSocialLogin ,
120+ onExternalWalletLogin : this . onExternalWalletLogin ,
121+ onModalVisibility : this . onModalVisibility ,
114122 }
115- } ;
116- withAbort ( ( ) => this . initConnectors ( { connectors : newConnectors , projectConfig, disabledExternalWallets } ) , signal , onAbortHandler ) ;
117- } ) ;
123+ ) ;
124+ await withAbort ( ( ) => this . loginModal . initModal ( ) , signal ) ;
125+
126+ // setup common JRPC provider
127+ await withAbort ( ( ) => this . setupCommonJRPCProvider ( ) , signal ) ;
118128
119- await withAbort ( ( ) => super . loadConnectors ( { projectConfig, modalMode : true } ) , signal ) ;
129+ // initialize connectors
130+ this . on ( CONNECTOR_EVENTS . CONNECTORS_UPDATED , ( { connectors : newConnectors } ) => {
131+ const onAbortHandler = ( ) => {
132+ log . debug ( "init aborted" ) ;
133+ if ( this . connectors ?. length > 0 ) {
134+ super . cleanup ( ) ;
135+ }
136+ } ;
137+ withAbort ( ( ) => this . initConnectors ( { connectors : newConnectors , projectConfig, disabledExternalWallets } ) , signal , onAbortHandler ) ;
138+ } ) ;
120139
121- // initialize plugins
122- await withAbort ( ( ) => super . initPlugins ( ) , signal ) ;
140+ await withAbort ( ( ) => super . loadConnectors ( { projectConfig, modalMode : true } ) , signal ) ;
141+
142+ // initialize plugins
143+ await withAbort ( ( ) => super . initPlugins ( ) , signal ) ;
144+
145+ // track completion event
146+ trackData = {
147+ ...trackData ,
148+ connectors : this . connectors . map ( ( connector ) => connector . name ) ,
149+ } ;
150+ this . analytics . track ( ANALYTICS_EVENTS . SDK_INITIALIZATION_COMPLETED , {
151+ ...trackData ,
152+ duration : Date . now ( ) - startTime ,
153+ } ) ;
154+ } catch ( error ) {
155+ if ( error instanceof DOMException && error . name === "AbortError" ) return ;
156+
157+ // track failure event
158+ this . analytics . track ( ANALYTICS_EVENTS . SDK_INITIALIZATION_FAILED , {
159+ ...trackData ,
160+ duration : Date . now ( ) - startTime ,
161+ error_code : error instanceof Web3AuthError ? error . code : undefined ,
162+ error_message : serializeError ( error ) ,
163+ } ) ;
164+ log . error ( "Failed to initialize modal" , error ) ;
165+ }
123166 }
124167
125168 public async connect ( ) : Promise < IProvider | null > {
0 commit comments