import { METRIC_NAMES } from "popup/constants/metricsNames" ; import { registerHandler , emitMetric } from "helpers/metrics" ; import { getTransactionInfo } from "helpers/stellar" ; import { parsedSearchParam , getUrlHostname } from "helpers/urls" ; import { navigate } from "popup/ducks/views" ; import { AppState } from "popup/App" ; import { ROUTES } from "popup/constants/routes" ; const routeToEventName = { [ ROUTES . welcome ] : METRIC_NAMES . viewWelcome , [ ROUTES . account ] : METRIC_NAMES . viewAccount , [ ROUTES . addAccount ] : METRIC_NAMES . viewAddAccount , [ ROUTES . importAccount ] : METRIC_NAMES . viewImportAccount , [ ROUTES . signTransaction ] : METRIC_NAMES . viewSignTransaction , [ ROUTES . grantAccess ] : METRIC_NAMES . viewGrantAccess , [ ROUTES . mnemonicPhrase ] : METRIC_NAMES . viewMnemonicPhrase , [ ROUTES . unlockAccount ] : METRIC_NAMES . viewUnlockAccount , [ ROUTES . mnemonicPhraseConfirmed ] : METRIC_NAMES . viewMnemonicPhraseConfirmed , [ ROUTES . accountCreator ] : METRIC_NAMES . viewAccountCreator , [ ROUTES . recoverAccount ] : METRIC_NAMES . viewRecoverAccount , [ ROUTES . recoverAccountSuccess ] : METRIC_NAMES . viewRecoverAccountSuccess , [ ROUTES . unlockBackupPhrase ] : METRIC_NAMES . viewUnlockBackupPhrase , [ ROUTES . displayBackupPhrase ] : METRIC_NAMES . viewDisplayBackupPhrase , [ ROUTES . unlockBackupPhrase ] : METRIC_NAMES . viewUnlockBackupPhrase , [ ROUTES . displayBackupPhrase ] : METRIC_NAMES . viewDisplayBackupPhrase , [ ROUTES . settings ] : METRIC_NAMES . viewSettings , [ ROUTES . viewPublicKey ] : METRIC_NAMES . viewPublicKey , [ ROUTES . debug ] : METRIC_NAMES . viewDebug , } ; registerHandler < AppState > ( navigate , ( _ , a ) => { const action = a as ReturnType < typeof navigate > ; const { pathname , search } = action . payload . location ; const eventName = routeToEventName [ pathname as ROUTES ] ; if ( ! eventName ) { throw new Error ( `Didn't find a metric event name for path '${ pathname }'` ) ; } if ( pathname === ROUTES . grantAccess ) { const { url } = parsedSearchParam ( search ) ; const hostname = getUrlHostname ( url ) ; const METRIC_OPTION_DOMAIN = { domain : hostname , } ; emitMetric ( eventName , METRIC_OPTION_DOMAIN ) ; } else if ( pathname === ROUTES . signTransaction ) { const { domain , operations , operationTypes } = getTransactionInfo ( search ) ; const METRIC_OPTIONS = { domain , number_of_operations : operations . length , operationTypes , } ; emitMetric ( eventName , METRIC_OPTIONS ) ; } else { emitMetric ( eventName ) ; } } ) ;