import { Amplitude } from "@amplitude/react-native" ; import { makeObservable , observable } from "mobx" ; import { AccountStore , AccountSetBase , KeyRingStore , } from "@keplr-wallet/stores" ; import { FeeType } from "@keplr-wallet/hooks" ; import { sha256 } from "sha.js" ; export interface EventProperties extends Record < string , unknown > { chainId ? : string ; chainName ? : string ; toChainId ? : string ; toChainName ? : string ; validatorName ? : string ; toValidatorName ? : string ; amount ? : string ; denom ? : string ; proposalId ? : string ; proposalTitle ? : string ; linkTitle ? : string ; linkUrl ? : string ; registerType ? : "seed" | "google" | "apple" | "ledger" | "qr" ; accountType ? : "mnemonic" | "privateKey" | "ledger" ; feeType ? : FeeType | undefined ; authType ? : "biometrics" | "password" ; fromScreen ? : string ; } export interface UserProperties { registerType ? : "seed" | "google" | "apple" | "ledger" | "qr" ; accountType ? : "mnemonic" | "privateKey" | "ledger" ; currency ? : string ; hasMobileAccount ? : boolean ; } export class AnalyticsStore { @ observable protected _isInitialized : boolean = false ; protected _mainChainId : string = "cosmoshub-4" ; protected amplitudeAnalytics ? : Amplitude ; constructor ( protected readonly appName , protected readonly accountStore , protected readonly keyRingStore ) { makeObservable ( this ) ; this . initializeAnalytics ( ) ; } get isInitialized ( ) : boolean { return this . _isInitialized ; } get mainChainId ( ) : string { return this . _mainChainId ; } initializeAnalytics ( ) { this . amplitudeAnalytics = Amplitude . getInstance ( ) ; this . amplitudeAnalytics . init ( "03e0234602d4044c9123bdad308fa1bc" ) ; this . _isInitialized = true ; } setUserId ( bech32Address ? ) { const accountInfo = this . accountStore . getAccount ( this . mainChainId ) ; if ( ! this . amplitudeAnalytics || ( ! bech32Address && ! accountInfo ) ) { return ; } const hashed = new sha256 ( ) . update ( bech32Address ? bech32Address : accountInfo . bech32Address ) . digest ( "hex" ) ; this . amplitudeAnalytics . setUserId ( hashed ) ; this . setUserProperties ( { hasMobileAccount : true } ) ; } setUserProperties ( userProperties ? ) { if ( ! this . amplitudeAnalytics ) { return ; } this . amplitudeAnalytics . setUserProperties ( { accountType : this . keyRingStore . keyRingType , ... userProperties , } ) ; } logEvent ( eventName , eventProperties ? ) { if ( ! eventName || ! this . amplitudeAnalytics ) { return ; } this . amplitudeAnalytics . logEvent ( eventName , eventProperties ) ; } logScreenView ( screenName , eventProperties ? ) { this . logEvent ( `${ screenName } viewed` , eventProperties ) ; } }