1
- import { useMemo } from 'react' ;
1
+ import { useEffect , useMemo } from 'react' ;
2
2
import { useLocation } from 'react-router-dom' ;
3
3
4
4
import {
5
5
EventParams ,
6
6
PageParams ,
7
7
} from '@segment/analytics-next/dist/types/core/arguments-resolver' ;
8
8
9
- import { IS_TEST_ENV } from '@shared/environment' ;
9
+ import { IS_TEST_ENV , SEGMENT_WRITE_KEY } from '@shared/environment' ;
10
10
import { logger } from '@shared/logger' ;
11
- import { analytics } from '@shared/utils/analytics' ;
11
+ import { analytics , initAnalytics } from '@shared/utils/analytics' ;
12
12
13
13
import { flow , origin } from '@app/common/initial-search-params' ;
14
14
import { useWalletType } from '@app/common/use-wallet-type' ;
@@ -25,13 +25,20 @@ function isHiroApiUrl(url: string) {
25
25
return / ^ h t t p s : \/ \/ .* \. s t a c k s .c o / . test ( url ) ;
26
26
}
27
27
28
+ export function useInitalizeAnalytics ( ) {
29
+ const hasUserDeclinedAnalytics = useHasUserExplicitlyDeclinedAnalytics ( ) ;
30
+
31
+ useEffect ( ( ) => {
32
+ if ( hasUserDeclinedAnalytics || ! SEGMENT_WRITE_KEY || IS_TEST_ENV ) return ;
33
+ initAnalytics ( ) ;
34
+ } , [ hasUserDeclinedAnalytics ] ) ;
35
+ }
36
+
28
37
export function useAnalytics ( ) {
29
38
const currentNetwork = useCurrentNetworkState ( ) ;
30
39
const location = useLocation ( ) ;
31
40
const { walletType } = useWalletType ( ) ;
32
41
33
- const hasDeclined = useHasUserExplicitlyDeclinedAnalytics ( ) ;
34
-
35
42
return useMemo ( ( ) => {
36
43
const defaultProperties = {
37
44
network : currentNetwork . name . toLowerCase ( ) ,
@@ -48,37 +55,29 @@ export function useAnalytics() {
48
55
} ;
49
56
50
57
return {
58
+ async identify ( properties : object ) {
59
+ return analytics . identify ( properties ) . catch ( logger . error ) ;
60
+ } ,
61
+
51
62
async page ( ...args : PageParams ) {
52
63
const [ category , name , properties , options , ...rest ] = args ;
53
64
const prop = { ...defaultProperties , ...properties } ;
54
65
const opts = { ...defaultOptions , ...options } ;
55
66
logger . info ( `Analytics page view: ${ name } ` , properties ) ;
56
67
57
- if ( ! analytics ) return ;
58
- if ( hasDeclined ) return ;
59
- if ( IS_TEST_ENV ) return ;
60
68
if ( typeof name === 'string' && isIgnoredPath ( name ) ) return ;
61
69
62
70
return analytics . page ( category , name , prop , opts , ...rest ) . catch ( logger . error ) ;
63
71
} ,
72
+
64
73
async track ( ...args : EventParams ) {
65
74
const [ eventName , properties , options , ...rest ] = args ;
66
75
const prop = { ...defaultProperties , ...properties } ;
67
76
const opts = { ...defaultOptions , ...options } ;
68
77
logger . info ( `Analytics event: ${ eventName } ` , properties ) ;
69
78
70
- if ( ! analytics ) return ;
71
- if ( hasDeclined ) return ;
72
- if ( IS_TEST_ENV ) return ;
73
-
74
79
return analytics . track ( eventName , prop , opts , ...rest ) . catch ( logger . error ) ;
75
80
} ,
76
81
} ;
77
- } , [
78
- currentNetwork . chain . stacks . url ,
79
- currentNetwork . name ,
80
- location . pathname ,
81
- walletType ,
82
- hasDeclined ,
83
- ] ) ;
82
+ } , [ currentNetwork . chain . stacks . url , currentNetwork . name , location . pathname , walletType ] ) ;
84
83
}
0 commit comments