Skip to content

Commit 672b476

Browse files
committed
chore: add mixpanel tracking for quickbuy
1 parent 7a79e37 commit 672b476

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

src/components/AssetHeader/QuickBuy.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ import { TbPencil } from 'react-icons/tb'
1919
import { useTranslate } from 'react-polyglot'
2020

2121
import { AnimatedCheck } from '../AnimatedCheck'
22+
import { useMixpanel } from '../MultiHopTrade/components/TradeConfirm/hooks/useMixpanel'
2223
import { TooltipWithTouch } from '../TooltipWithTouch'
2324
import { useQuickBuy } from './hooks/useQuickBuy'
2425
import { QuickBuyTradeButton } from './QuickBuyTradeButton'
2526

2627
import { Amount } from '@/components/Amount/Amount'
2728
import { useLocaleFormatter } from '@/hooks/useLocaleFormatter/useLocaleFormatter'
29+
import { MixPanelEvent } from '@/lib/mixpanel/types'
2830
import { TradeExecutionState } from '@/state/slices/tradeQuoteSlice/types'
2931

3032
const editIcon = <Icon as={TbPencil} boxSize={6} color='text.subtle' />
@@ -67,9 +69,12 @@ export const QuickBuy: React.FC<QuickBuyProps> = ({ assetId, onEditAmounts }) =>
6769
cancelPurchase()
6870
}, [cancelPurchase])
6971

72+
const trackMixpanelEvent = useMixpanel(true)
73+
7074
const handleConfirmPurchase = useCallback((): void => {
75+
trackMixpanelEvent(MixPanelEvent.QuickBuyConfirm)
7176
confirmPurchase()
72-
}, [confirmPurchase])
77+
}, [confirmPurchase, trackMixpanelEvent])
7378

7479
if (isNativeAsset) {
7580
// We use native asset as the sell asset right now so can't quick buy it
@@ -110,7 +115,10 @@ export const QuickBuy: React.FC<QuickBuyProps> = ({ assetId, onEditAmounts }) =>
110115
rounded='full'
111116
background={isSuccess ? 'green.500' : undefined}
112117
// eslint-disable-next-line react-memo/require-usememo
113-
onClick={() => startPurchase(amount)}
118+
onClick={() => {
119+
trackMixpanelEvent(MixPanelEvent.QuickBuyPreview)
120+
startPurchase(amount)
121+
}}
114122
flex={1}
115123
isDisabled={isNotEnoughFunds}
116124
fontSize='lg'

src/components/AssetHeader/hooks/useQuickBuy.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import { bn } from '@shapeshiftoss/utils'
55
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
66

77
import { useCurrentHopIndex } from '../../MultiHopTrade/components/TradeConfirm/hooks/useCurrentHopIndex'
8+
import { useMixpanel } from '../../MultiHopTrade/components/TradeConfirm/hooks/useMixpanel'
89
import { useGetTradeRates } from '../../MultiHopTrade/hooks/useGetTradeQuotes/useGetTradeRates'
910

11+
import { MixPanelEvent } from '@/lib/mixpanel/types'
1012
import { swapperApi } from '@/state/apis/swapper/swapperApi'
1113
import { TradeQuoteValidationError } from '@/state/apis/swapper/types'
1214
import { selectMarketDataByAssetIdUserCurrency } from '@/state/slices/marketDataSlice/selectors'
@@ -144,16 +146,20 @@ export const useQuickBuy = ({ assetId }: UseQuickBuyParams): UseQuickBuyReturn =
144146
}
145147
}, [])
146148

149+
const trackMixpanelEvent = useMixpanel(true)
150+
147151
const setErrorState = useCallback(
148152
(messageKey: string, amount: number) => {
153+
trackMixpanelEvent(MixPanelEvent.QuickBuyFailed)
149154
setQuickBuyState({ status: 'error', amount, messageKey })
150155
resetTrade()
151156
},
152-
[resetTrade],
157+
[resetTrade, trackMixpanelEvent],
153158
)
154159

155160
const setSuccessState = useCallback(
156161
(amount: number) => {
162+
trackMixpanelEvent(MixPanelEvent.QuickBuySuccess)
157163
setQuickBuyState({ status: 'success', amount })
158164
resetTrade()
159165

@@ -164,7 +170,7 @@ export const useQuickBuy = ({ assetId }: UseQuickBuyParams): UseQuickBuyReturn =
164170
)
165171
}, SUCCESS_TIMEOUT_MS)
166172
},
167-
[resetTrade, clearSuccessTimer],
173+
[resetTrade, clearSuccessTimer, trackMixpanelEvent],
168174
)
169175

170176
const startPurchase = useCallback(

src/components/MultiHopTrade/components/TradeConfirm/hooks/useMixpanel.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@ import { getMixpanelEventData } from '@/components/MultiHopTrade/helpers'
44
import { getMixPanel } from '@/lib/mixpanel/mixPanelSingleton'
55
import type { MixPanelEvent } from '@/lib/mixpanel/types'
66

7-
export const useMixpanel = () => {
7+
export const useMixpanel = (allowUndefinedEventData = false) => {
88
const mixpanel = useMemo(() => getMixPanel(), [])
9-
const eventData = useMemo(() => getMixpanelEventData(), [])
109
const trackMixpanelEvent = useCallback(
1110
(event: MixPanelEvent) => {
1211
// mixpanel is undefined when the feature is disabled
13-
if (eventData && mixpanel) mixpanel.track(event, eventData)
12+
if (!mixpanel) return
13+
14+
const eventData = getMixpanelEventData()
15+
// Allow tracking without eventData if flag is set
16+
if (allowUndefinedEventData || eventData) {
17+
mixpanel.track(event, eventData)
18+
}
1419
},
15-
[eventData, mixpanel],
20+
[mixpanel, allowUndefinedEventData],
1621
)
1722

1823
return trackMixpanelEvent

src/lib/mixpanel/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ export enum MixPanelEvent {
7474
SendClick = 'Send Click',
7575
SendBroadcast = 'Send Broadcast',
7676
HighlightedTokenClicked = 'Highlighted Token Clicked',
77+
QuickBuyPreview = 'Quick Buy Preview',
78+
QuickBuyConfirm = 'Quick Buy Confirm',
79+
QuickBuySuccess = 'Quick Buy Success',
80+
QuickBuyFailed = 'Quick Buy Failed',
7781
}
7882

7983
export type TrackOpportunityProps = {

0 commit comments

Comments
 (0)