Skip to content

Commit

Permalink
Add telemetry for wallet connection events (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink authored Dec 14, 2024
1 parent a43795c commit 0c3f1c1
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 28 deletions.
7 changes: 6 additions & 1 deletion components/Discovery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import { Service } from '../types'
import { useConfig, useRpc } from '../contexts/FclContext'
import { handleCancel } from '../helpers/window'
import { useDevice } from '../contexts/DeviceContext'
import { DeviceType } from '../helpers/device'
import { getCompatibleInstallLinks } from '../hooks/useInstallLinks'
import { useTelemetry } from '../hooks/useTelemetry'

export enum VIEWS {
WALLET_SELECTION,
Expand All @@ -47,6 +47,7 @@ export default function Discovery() {
const { deviceInfo } = useDevice()
const { rpcEnabled } = useRpc()
const { supportedStrategies } = useConfig()
const telemetry = useTelemetry()

// Skip the connect page if there is only one service available and no install links
const shouldSkipConnectPage = (wallet: Wallet) =>
Expand Down Expand Up @@ -78,6 +79,10 @@ export default function Discovery() {
setCurrentView(VIEWS.CONNECT_EXTENSION)
} else {
fcl.WalletUtils.redirect(service)
telemetry.trackWalletConnected(
wallet.uid,
service.method as FCL_SERVICE_METHODS,
)
}

setSelectedWallet(wallet)
Expand Down
7 changes: 5 additions & 2 deletions components/views/ConnectExtension.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Button, Heading, Spinner, Stack, Text } from '@chakra-ui/react'
import { Wallet } from '../../data/wallets'
import { useEffect, useRef, useState } from 'react'
import { useCallback, useEffect, useRef, useState } from 'react'
import { FCL_SERVICE_METHODS } from '../../helpers/constants'
import { useRpc } from '../../contexts/FclContext'
import { FclRequest } from '../../helpers/rpc'
import WalletIcon from '../icons/WalletIcon'
import { useWalletHistory } from '../../hooks/useWalletHistory'
import { handleCancel } from '../../helpers/window'
import { ViewContainer } from '../layout/ViewContainer'
import { useTelemetry } from '../../hooks/useTelemetry'

type ConnectExtensionProps = {
wallet: Wallet
Expand All @@ -19,14 +20,16 @@ export default function ConnectExtension({ wallet }: ConnectExtensionProps) {
const hasAttemptedConnection = useRef(true)
const showSpinner = !rpc || isConnecting
const { setLastUsed } = useWalletHistory()
const telemetry = useTelemetry()

function connect() {
const connect = () => {
setIsConnecting(true)
wallet.services.forEach(service => {
if (service.method === FCL_SERVICE_METHODS.EXT) {
rpc
.request(FclRequest.EXEC_SERVICE, { service })
.then(() => {
telemetry.trackWalletConnected(wallet.uid, FCL_SERVICE_METHODS.EXT)
setLastUsed(wallet)
handleCancel()
})
Expand Down
4 changes: 4 additions & 0 deletions components/views/ScanConnect/ScanConnectDesktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { useWcUri } from '../../../hooks/useWcUri'
import { useWalletHistory } from '../../../hooks/useWalletHistory'
import { handleCancel } from '../../../helpers/window'
import { ViewContainer } from '../../layout/ViewContainer'
import { FCL_SERVICE_METHODS } from '../../../helpers/constants'
import { useTelemetry } from '../../../hooks/useTelemetry'

interface ScanConnectDesktopProps {
wallet: Wallet
Expand All @@ -18,7 +20,9 @@ export default function ScanConnectDesktop({
onGetWallet,
}: ScanConnectDesktopProps) {
const { setLastUsed } = useWalletHistory()
const telemetry = useTelemetry()
const { uri, connecting, error, isLoading } = useWcUri(() => {
telemetry.trackWalletConnected(wallet.uid, FCL_SERVICE_METHODS.WC)
setLastUsed(wallet)
handleCancel()
})
Expand Down
3 changes: 3 additions & 0 deletions components/views/ScanConnect/ScanConnectMobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useEffect, useRef } from 'react'
import { FCL_SERVICE_METHODS } from '../../../helpers/constants'
import WalletIcon from '../../icons/WalletIcon'
import { ViewContainer } from '../../layout/ViewContainer'
import { useTelemetry } from '../../../hooks/useTelemetry'

interface ScanConnectMobileProps {
wallet: Wallet
Expand All @@ -21,7 +22,9 @@ export default function ScanConnectMobile({
noDeepLink,
}: ScanConnectMobileProps) {
const { setLastUsed } = useWalletHistory()
const telemetry = useTelemetry()
const { uri, connecting, error, isLoading } = useWcUri(() => {
telemetry.trackWalletConnected(wallet.uid, FCL_SERVICE_METHODS.WC)
setLastUsed(wallet)
handleCancel()
})
Expand Down
14 changes: 0 additions & 14 deletions config/mixpanel.server.js

This file was deleted.

19 changes: 19 additions & 0 deletions helpers/telemetry/telemetry.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Mixpanel from 'mixpanel-browser'
import { trackWalletConnected, trackWalletDiscoveryRequest } from './telemetry'
import { TelemetryDataClient } from './types'

let mixpanel: any = null

export function getTelemetryClient(baseData: TelemetryDataClient) {
if (process.env.NEXT_PUBLIC_MIXPANEL_ID && !mixpanel) {
mixpanel = Mixpanel.init(process.env.NEXT_PUBLIC_MIXPANEL_ID)
}

return {
trackWalletDiscoveryRequest: trackWalletDiscoveryRequest(
mixpanel,
baseData,
),
trackWalletConnected: trackWalletConnected(mixpanel, baseData),
}
}
19 changes: 19 additions & 0 deletions helpers/telemetry/telemetry.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Mixpanel from 'mixpanel'
import { TelemetryDataServer } from './types'
import { trackWalletConnected, trackWalletDiscoveryRequest } from './telemetry'

let mixpanel: Mixpanel.Mixpanel | null = null

export function getTelemetryServer(baseData: TelemetryDataServer) {
if (process.env.NEXT_PUBLIC_MIXPANEL_ID && !mixpanel) {
mixpanel = Mixpanel.init(process.env.NEXT_PUBLIC_MIXPANEL_ID)
}

return {
trackWalletDiscoveryRequest: trackWalletDiscoveryRequest(
mixpanel,
baseData,
),
trackWalletConnected: trackWalletConnected(mixpanel, baseData),
}
}
24 changes: 24 additions & 0 deletions helpers/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Mixpanel from 'mixpanel'
import { FCL_SERVICE_METHODS } from '../constants'

export function trackWalletDiscoveryRequest(
mixpanel: Mixpanel.Mixpanel,
baseData: any,
) {
return () => {
mixpanel?.track('Wallet Discovery Request', baseData)
}
}

export function trackWalletConnected(
mixpanel: Mixpanel.Mixpanel,
baseData: any,
) {
return (walletUid: string, serviceMethod: FCL_SERVICE_METHODS) => {
mixpanel?.track('Wallet Connected', {
walletUid: walletUid,
method: serviceMethod,
...baseData,
})
}
}
13 changes: 13 additions & 0 deletions helpers/telemetry/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type TelemetryData = {
fclVersion: string
type: 'UI' | 'API'
network: string
}

export type TelemetryDataServer = TelemetryData & {
origin?: string
}

export type TelemetryDataClient = TelemetryData & {
parent: string
}
13 changes: 13 additions & 0 deletions hooks/useTelemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useConfig } from '../contexts/FclContext'
import { getTelemetryClient } from '../helpers/telemetry/telemetry.client'

export function useTelemetry() {
const cfg = useConfig()
return getTelemetryClient({
network: cfg.network,
type: 'UI',
fclVersion: cfg.appVersion,
parent:
window.location != window.parent.location ? document.referrer : undefined,
})
}
7 changes: 6 additions & 1 deletion hooks/useWallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ export function useWallets() {
port,
} = useConfig()

const requestUrl = `/api/${network.toLowerCase()}/wallets?discoveryType=UI&enableExperimentalWalletsEndpoint=true`
const params = new URLSearchParams()
params.append('discoveryType', 'UI')
params.append('enableExperimentalWalletsEndpoint', 'true')
params.append('origin', window.location.origin)

const requestUrl = `/api/${network.toLowerCase()}/wallets?${params.toString()}`
const body = {
type: ['authn'],
fclVersion: appVersion,
Expand Down
Loading

0 comments on commit 0c3f1c1

Please sign in to comment.