From 160b17cba168608fe3a6e8304a1b44296ecdfb83 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Mon, 16 Dec 2024 15:32:10 -0800 Subject: [PATCH] Fix telemetry --- components/Discovery.tsx | 13 ++++--- components/views/ConnectExtension.tsx | 39 ++++++++++--------- .../views/ScanConnect/ScanConnectDesktop.tsx | 7 +++- .../views/ScanConnect/ScanConnectMobile.tsx | 7 +++- helpers/telemetry/telemetry.client.ts | 30 ++++++++++---- helpers/telemetry/telemetry.server.ts | 16 ++++---- helpers/telemetry/telemetry.ts | 24 ------------ 7 files changed, 68 insertions(+), 68 deletions(-) delete mode 100644 helpers/telemetry/telemetry.ts diff --git a/components/Discovery.tsx b/components/Discovery.tsx index 591f348..bd2fda3 100644 --- a/components/Discovery.tsx +++ b/components/Discovery.tsx @@ -72,17 +72,20 @@ export default function Discovery() { }) }, [isCollapsed]) - const connectWalletService = (wallet: Wallet, service: Service) => { + const connectWalletService = async (wallet: Wallet, service: Service) => { if (service.method === FCL_SERVICE_METHODS.WC && rpcEnabled) { setCurrentView(VIEWS.SCAN_CONNECT) } else if (service.method === FCL_SERVICE_METHODS.EXT && rpcEnabled) { setCurrentView(VIEWS.CONNECT_EXTENSION) } else { + await Promise.race([ + telemetry.trackWalletConnected({ + walletUid: wallet.uid, + serviceMethod: service.method as FCL_SERVICE_METHODS, + }), + new Promise(resolve => setTimeout(resolve, 1000)), + ]) fcl.WalletUtils.redirect(service) - telemetry.trackWalletConnected( - wallet.uid, - service.method as FCL_SERVICE_METHODS, - ) } setSelectedWallet(wallet) diff --git a/components/views/ConnectExtension.tsx b/components/views/ConnectExtension.tsx index 7bed932..7515da7 100644 --- a/components/views/ConnectExtension.tsx +++ b/components/views/ConnectExtension.tsx @@ -22,25 +22,28 @@ export default function ConnectExtension({ wallet }: ConnectExtensionProps) { const { setLastUsed } = useWalletHistory() const telemetry = useTelemetry() - const connect = () => { + const connect = async () => { 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() - }) - .catch(e => { - console.error('Failed to connect to extension', e) - }) - .finally(() => { - setIsConnecting(false) - }) - } - }) + const service = wallet?.services?.find( + service => service.method === FCL_SERVICE_METHODS.WC, + ) + if (!service) { + return + } + + try { + await rpc.request(FclRequest.EXEC_SERVICE, { service }) + setLastUsed(wallet) + await telemetry.trackWalletConnected({ + walletUid: wallet.uid, + serviceMethod: FCL_SERVICE_METHODS.EXT, + }) + handleCancel() + } catch (e) { + console.error('Failed to connect to extension', e) + } finally { + setIsConnecting(false) + } } useEffect(() => { diff --git a/components/views/ScanConnect/ScanConnectDesktop.tsx b/components/views/ScanConnect/ScanConnectDesktop.tsx index 2f7c40c..c49403d 100644 --- a/components/views/ScanConnect/ScanConnectDesktop.tsx +++ b/components/views/ScanConnect/ScanConnectDesktop.tsx @@ -21,8 +21,11 @@ export default function ScanConnectDesktop({ }: ScanConnectDesktopProps) { const { setLastUsed } = useWalletHistory() const telemetry = useTelemetry() - const { uri, connecting, error, isLoading } = useWcUri(() => { - telemetry.trackWalletConnected(wallet.uid, FCL_SERVICE_METHODS.WC) + const { uri, connecting, error, isLoading } = useWcUri(async () => { + telemetry.trackWalletConnected({ + walletUid: wallet.uid, + serviceMethod: FCL_SERVICE_METHODS.WC, + }) setLastUsed(wallet) handleCancel() }) diff --git a/components/views/ScanConnect/ScanConnectMobile.tsx b/components/views/ScanConnect/ScanConnectMobile.tsx index 33b2372..b09d92b 100644 --- a/components/views/ScanConnect/ScanConnectMobile.tsx +++ b/components/views/ScanConnect/ScanConnectMobile.tsx @@ -23,9 +23,12 @@ export default function ScanConnectMobile({ }: ScanConnectMobileProps) { const { setLastUsed } = useWalletHistory() const telemetry = useTelemetry() - const { uri, connecting, error, isLoading } = useWcUri(() => { - telemetry.trackWalletConnected(wallet.uid, FCL_SERVICE_METHODS.WC) + const { uri, connecting, error, isLoading } = useWcUri(async () => { setLastUsed(wallet) + await telemetry.trackWalletConnected({ + walletUid: wallet.uid, + serviceMethod: FCL_SERVICE_METHODS.WC, + }) handleCancel() }) const shouldOpenLink = useRef(true) diff --git a/helpers/telemetry/telemetry.client.ts b/helpers/telemetry/telemetry.client.ts index 9cc9d0e..5828666 100644 --- a/helpers/telemetry/telemetry.client.ts +++ b/helpers/telemetry/telemetry.client.ts @@ -1,8 +1,8 @@ -import Mixpanel from 'mixpanel-browser' -import { trackWalletConnected, trackWalletDiscoveryRequest } from './telemetry' +import * as Mixpanel from 'mixpanel-browser' import { TelemetryDataClient } from './types' +import { FCL_SERVICE_METHODS } from '../constants' -let mixpanel: any = null +let mixpanel: Mixpanel.Mixpanel | null = null export function getTelemetryClient(baseData: TelemetryDataClient) { if (process.env.NEXT_PUBLIC_MIXPANEL_ID && !mixpanel) { @@ -10,10 +10,24 @@ export function getTelemetryClient(baseData: TelemetryDataClient) { } return { - trackWalletDiscoveryRequest: trackWalletDiscoveryRequest( - mixpanel, - baseData, - ), - trackWalletConnected: trackWalletConnected(mixpanel, baseData), + trackWalletConnected: async ({ + walletUid, + serviceMethod, + }: { + walletUid: string + serviceMethod: FCL_SERVICE_METHODS + }) => { + return new Promise(resolve => { + mixpanel?.track( + 'Wallet Connected', + { + walletUid: walletUid, + method: serviceMethod, + ...baseData, + }, + () => resolve(), + ) + }) + }, } } diff --git a/helpers/telemetry/telemetry.server.ts b/helpers/telemetry/telemetry.server.ts index 12c55e6..3330e1e 100644 --- a/helpers/telemetry/telemetry.server.ts +++ b/helpers/telemetry/telemetry.server.ts @@ -1,19 +1,17 @@ import Mixpanel from 'mixpanel' import { TelemetryDataServer } from './types' -import { trackWalletConnected, trackWalletDiscoveryRequest } from './telemetry' -let mixpanel: Mixpanel.Mixpanel | null = null +let hasInitialized = false export function getTelemetryServer(baseData: TelemetryDataServer) { - if (process.env.NEXT_PUBLIC_MIXPANEL_ID && !mixpanel) { - mixpanel = Mixpanel.init(process.env.NEXT_PUBLIC_MIXPANEL_ID) + if (process.env.NEXT_PUBLIC_MIXPANEL_ID && !hasInitialized) { + Mixpanel.init(process.env.NEXT_PUBLIC_MIXPANEL_ID) } return { - trackWalletDiscoveryRequest: trackWalletDiscoveryRequest( - mixpanel, - baseData, - ), - trackWalletConnected: trackWalletConnected(mixpanel, baseData), + trackWalletDiscoveryRequest: async () => + new Promise(resolve => { + Mixpanel.track('Wallet Discovery Request', baseData, () => resolve()) + }), } } diff --git a/helpers/telemetry/telemetry.ts b/helpers/telemetry/telemetry.ts deleted file mode 100644 index 6bab651..0000000 --- a/helpers/telemetry/telemetry.ts +++ /dev/null @@ -1,24 +0,0 @@ -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, - }) - } -}