Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add telemetry for wallet connection events #274

Merged
merged 10 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 6 additions & 3 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 = useCallback(() => {
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.WC)
setLastUsed(wallet)
handleCancel()
})
Expand All @@ -38,7 +41,7 @@ export default function ConnectExtension({ wallet }: ConnectExtensionProps) {
})
}
})
}
}, [rpc, wallet, telemetry, setLastUsed])

useEffect(() => {
if (!hasAttemptedConnection.current) {
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.

21 changes: 21 additions & 0 deletions helpers/telemetry/telemetry.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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.MIXPANEL_ID)
}

mixpanel.register(baseData)

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.MIXPANEL_ID && !mixpanel) {
mixpanel = Mixpanel.init(process.env.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
}
12 changes: 12 additions & 0 deletions hooks/useTelemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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?.parent?.location?.href,
})
}
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
Loading