11import { Trans } from '@lingui/macro' ;
22import { Button } from '@mui/material' ;
33import { ConnectKitButton } from 'connectkit' ;
4+ import { useEffect , useRef , useState } from 'react' ;
45import { useRootStore } from 'src/store/root' ;
56import { AUTH } from 'src/utils/events' ;
7+ import { useShallow } from 'zustand/shallow' ;
68
79import { AvatarSize } from '../Avatar' ;
810import { UserDisplay } from '../UserDisplay' ;
@@ -14,19 +16,75 @@ export interface ConnectWalletProps {
1416}
1517
1618export const ConnectWalletButton : React . FC < ConnectWalletProps > = ( { funnel, onClick } ) => {
17- const trackEvent = useRootStore ( ( store ) => store . trackEvent ) ;
19+ const [ trackEvent , walletType ] = useRootStore (
20+ useShallow ( ( store ) => [ store . trackEvent , store . walletType ] )
21+ ) ;
22+
23+ // Track connection attempt duration
24+ const [ connectionStartTime , setConnectionStartTime ] = useState < number | null > ( null ) ;
25+ const previousConnectionState = useRef < boolean > ( false ) ;
26+ const isConnectingRef = useRef < boolean > ( false ) ;
27+ const isConnectedRef = useRef < boolean > ( false ) ;
28+
29+ const connectionAttempts = useRef < number > ( 0 ) ;
30+
31+ // Track connection state changes
32+ useEffect ( ( ) => {
33+ if ( isConnectingRef . current && ! previousConnectionState . current ) {
34+ // Connection attempt started
35+ setConnectionStartTime ( Date . now ( ) ) ;
36+ connectionAttempts . current += 1 ;
37+ trackEvent ( AUTH . WALLET_CONNECT_START , {
38+ funnel,
39+ attempt_number : connectionAttempts . current ,
40+ previous_wallet_type : walletType ,
41+ } ) ;
42+ }
43+
44+ if ( ! isConnectingRef . current && previousConnectionState . current ) {
45+ // Connection attempt ended
46+ const duration = connectionStartTime ? Date . now ( ) - connectionStartTime : 0 ;
47+ if ( isConnectedRef . current ) {
48+ trackEvent ( AUTH . WALLET_CONNECT_SUCCESS , {
49+ funnel,
50+ wallet_type : walletType ,
51+ connection_duration_ms : duration ,
52+ attempts_until_success : connectionAttempts . current ,
53+ } ) ;
54+ } else {
55+ trackEvent ( AUTH . WALLET_CONNECT_ABORT , {
56+ funnel,
57+ connection_duration_ms : duration ,
58+ attempt_number : connectionAttempts . current ,
59+ } ) ;
60+ }
61+ setConnectionStartTime ( null ) ;
62+ }
63+
64+ previousConnectionState . current = isConnectingRef . current ;
65+ } , [ connectionStartTime , funnel , trackEvent , walletType ] ) ;
1866
1967 return (
2068 < >
2169 < ConnectKitButton . Custom >
22- { ( { isConnected, show } ) => {
70+ { ( { isConnected, show, isConnecting } ) => {
71+ // Update refs for tracking
72+ isConnectingRef . current = isConnecting ;
73+ isConnectedRef . current = isConnected ;
74+
2375 return (
2476 < Button
2577 variant = { isConnected ? 'surface' : 'gradient' }
2678 onClick = { ( ) => {
79+ // Track initial button click
80+ trackEvent ( AUTH . CONNECT_WALLET , {
81+ funnel,
82+ current_url : window . location . pathname ,
83+ is_reconnect_attempt : connectionAttempts . current > 0 ,
84+ } ) ;
85+
2786 onClick && onClick ( ) ;
2887 show && show ( ) ;
29- trackEvent ( AUTH . CONNECT_WALLET , { funnel : funnel } ) ;
3088 } }
3189 >
3290 { isConnected ? (
0 commit comments