import { useCurrentNetworkState } from '@app/store/network/networks.hooks' ; import { useLocation } from 'react-router-dom' ; import { IS_TEST_ENV } from '@shared/constants' ; import { EventParams , PageParams } from '@segment/analytics-next/dist/pkg/core/arguments-resolver' ; import { analytics } from '@app/common/segment-init' ; import { logger } from '@shared/logger' ; const IGNORED_PATH_REGEXPS = [ /^\/$/ ] ; function isIgnoredPath ( path ) { return IGNORED_PATH_REGEXPS . find ( regexp => regexp . test ( path ) ) ; } function isHiroApiUrl ( url ) { return /^https:\/\/.*\.stacks.co/ . test ( url ) ; } export function useAnalytics ( ) { const currentNetwork = useCurrentNetworkState ( ) ; const location = useLocation ( ) ; const defaultProperties = { network : currentNetwork . name . toLowerCase ( ) , usingDefaultHiroApi : isHiroApiUrl ( currentNetwork . url ) , route : location . pathname , version : VERSION , } ; const defaultOptions = { context : { ip : '0.0.0.0' } , } ; return { page : async ( ... args ) => { if ( ! analytics || IS_TEST_ENV ) return ; const [ category , name , properties , options , ... rest ] = args ; if ( typeof name === 'string' && isIgnoredPath ( name ) ) return ; const prop = { ... defaultProperties , ... properties } ; const opts = { ... defaultOptions , ... options } ; return analytics . page ( category , name , prop , opts , ... rest ) . catch ( logger . error ) ; } , track : async ( ... args ) => { if ( ! analytics || IS_TEST_ENV ) return ; const [ eventName , properties , options , ... rest ] = args ; const prop = { ... defaultProperties , ... properties } ; const opts = { ... defaultOptions , ... options } ; return analytics . track ( eventName , prop , opts , ... rest ) . catch ( logger . error ) ; } , } ; }