Commit c8a81491 authored by eddie's avatar eddie Committed by GitHub

fix: add amplitude page context to swap flow events (#6489)

parent 38cde648
import { TransactionResponse } from '@ethersproject/abstract-provider' import { TransactionResponse } from '@ethersproject/abstract-provider'
import { BigNumber } from '@ethersproject/bignumber' import { BigNumber } from '@ethersproject/bignumber'
import { t } from '@lingui/macro' import { t } from '@lingui/macro'
import { sendAnalyticsEvent } from '@uniswap/analytics' import { sendAnalyticsEvent, useTrace } from '@uniswap/analytics'
import { SwapEventName } from '@uniswap/analytics-events' import { SwapEventName } from '@uniswap/analytics-events'
import { Trade } from '@uniswap/router-sdk' import { Trade } from '@uniswap/router-sdk'
import { Currency, Percent, TradeType } from '@uniswap/sdk-core' import { Currency, Percent, TradeType } from '@uniswap/sdk-core'
...@@ -49,6 +49,7 @@ export function useUniversalRouterSwapCallback( ...@@ -49,6 +49,7 @@ export function useUniversalRouterSwapCallback(
options: SwapOptions options: SwapOptions
) { ) {
const { account, chainId, provider } = useWeb3React() const { account, chainId, provider } = useWeb3React()
const analyticsContext = useTrace()
return useCallback(async (): Promise<TransactionResponse> => { return useCallback(async (): Promise<TransactionResponse> => {
return trace( return trace(
...@@ -90,12 +91,19 @@ export function useUniversalRouterSwapCallback( ...@@ -90,12 +91,19 @@ export function useUniversalRouterSwapCallback(
.getSigner() .getSigner()
.sendTransaction({ ...tx, gasLimit }) .sendTransaction({ ...tx, gasLimit })
.then((response) => { .then((response) => {
sendAnalyticsEvent( sendAnalyticsEvent(SwapEventName.SWAP_SIGNED, {
SwapEventName.SWAP_SIGNED, ...formatSwapSignedAnalyticsEventProperties({
formatSwapSignedAnalyticsEventProperties({ trade, fiatValues, txHash: response.hash }) trade,
) fiatValues,
txHash: response.hash,
}),
...analyticsContext,
})
if (tx.data !== response.data) { if (tx.data !== response.data) {
sendAnalyticsEvent(SwapEventName.SWAP_MODIFIED_IN_WALLET, { txHash: response.hash }) sendAnalyticsEvent(SwapEventName.SWAP_MODIFIED_IN_WALLET, {
txHash: response.hash,
...analyticsContext,
})
throw new ModifiedSwapError() throw new ModifiedSwapError()
} }
return response return response
...@@ -117,6 +125,7 @@ export function useUniversalRouterSwapCallback( ...@@ -117,6 +125,7 @@ export function useUniversalRouterSwapCallback(
) )
}, [ }, [
account, account,
analyticsContext,
chainId, chainId,
fiatValues, fiatValues,
options.deadline, options.deadline,
......
import { Trans } from '@lingui/macro' import { Trans } from '@lingui/macro'
import { sendAnalyticsEvent, Trace, TraceEvent } from '@uniswap/analytics' import { sendAnalyticsEvent, Trace, TraceEvent, useTrace } from '@uniswap/analytics'
import { import {
BrowserEvent, BrowserEvent,
InterfaceElementName, InterfaceElementName,
...@@ -189,6 +189,7 @@ export function Swap({ ...@@ -189,6 +189,7 @@ export function Swap({
const { account, chainId: connectedChainId, connector } = useWeb3React() const { account, chainId: connectedChainId, connector } = useWeb3React()
const [newSwapQuoteNeedsLogging, setNewSwapQuoteNeedsLogging] = useState(true) const [newSwapQuoteNeedsLogging, setNewSwapQuoteNeedsLogging] = useState(true)
const [fetchingSwapQuoteStartTime, setFetchingSwapQuoteStartTime] = useState<Date | undefined>() const [fetchingSwapQuoteStartTime, setFetchingSwapQuoteStartTime] = useState<Date | undefined>()
const trace = useTrace()
// token warning stuff // token warning stuff
const prefilledInputCurrency = useCurrency(prefilledState?.[Field.INPUT]?.currencyId) const prefilledInputCurrency = useCurrency(prefilledState?.[Field.INPUT]?.currencyId)
...@@ -392,13 +393,14 @@ export function Swap({ ...@@ -392,13 +393,14 @@ export function Swap({
chain_id: chainId, chain_id: chainId,
token_symbol: maximumAmountIn?.currency.symbol, token_symbol: maximumAmountIn?.currency.symbol,
token_address: maximumAmountIn?.currency.address, token_address: maximumAmountIn?.currency.address,
...trace,
}) })
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} finally { } finally {
setIsAllowancePending(false) setIsAllowancePending(false)
} }
}, [allowance, chainId, maximumAmountIn?.currency.address, maximumAmountIn?.currency.symbol]) }, [allowance, chainId, maximumAmountIn?.currency.address, maximumAmountIn?.currency.symbol, trace])
const maxInputAmount: CurrencyAmount<Currency> | undefined = useMemo( const maxInputAmount: CurrencyAmount<Currency> | undefined = useMemo(
() => maxAmountSpend(currencyBalances[Field.INPUT]), () => maxAmountSpend(currencyBalances[Field.INPUT]),
...@@ -534,10 +536,14 @@ export function Swap({ ...@@ -534,10 +536,14 @@ export function Swap({
// Set the current datetime as the time of receipt of latest swap quote. // Set the current datetime as the time of receipt of latest swap quote.
setSwapQuoteReceivedDate(now) setSwapQuoteReceivedDate(now)
// Log swap quote. // Log swap quote.
sendAnalyticsEvent( sendAnalyticsEvent(SwapEventName.SWAP_QUOTE_RECEIVED, {
SwapEventName.SWAP_QUOTE_RECEIVED, ...formatSwapQuoteReceivedEventProperties(
formatSwapQuoteReceivedEventProperties(trade, trade.gasUseEstimateUSD ?? undefined, fetchingSwapQuoteStartTime) trade,
) trade.gasUseEstimateUSD ?? undefined,
fetchingSwapQuoteStartTime
),
...trace,
})
// Latest swap quote has just been logged, so we don't need to log the current trade anymore // Latest swap quote has just been logged, so we don't need to log the current trade anymore
// unless user inputs change again and a new trade is in the process of being generated. // unless user inputs change again and a new trade is in the process of being generated.
setNewSwapQuoteNeedsLogging(false) setNewSwapQuoteNeedsLogging(false)
...@@ -556,6 +562,7 @@ export function Swap({ ...@@ -556,6 +562,7 @@ export function Swap({
fetchingSwapQuoteStartTime, fetchingSwapQuoteStartTime,
trade, trade,
setSwapQuoteReceivedDate, setSwapQuoteReceivedDate,
trace,
]) ])
const showDetailsDropdown = Boolean( const showDetailsDropdown = Boolean(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment