Skip to content

Commit

Permalink
fix: Simplify event logging for SWAP_QUOTE_RECEIVED (#6628)
Browse files Browse the repository at this point in the history
* simplify event logging

* remove unused function parameter
  • Loading branch information
tinaszheng committed May 23, 2023
1 parent 95814e3 commit 6424fdf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 42 deletions.
6 changes: 1 addition & 5 deletions src/lib/utils/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ export const formatSwapSignedAnalyticsEventProperties = ({

export const formatSwapQuoteReceivedEventProperties = (
trade: Trade<Currency, Currency, TradeType>,
gasUseEstimateUSD?: string,
fetchingSwapQuoteStartTime?: Date
gasUseEstimateUSD?: string
) => {
return {
token_in_symbol: trade.inputAmount.currency.symbol,
Expand All @@ -77,8 +76,5 @@ export const formatSwapQuoteReceivedEventProperties = (
: undefined,
token_in_amount: formatToDecimal(trade.inputAmount, trade.inputAmount.currency.decimals),
token_out_amount: formatToDecimal(trade.outputAmount, trade.outputAmount.currency.decimals),
quote_latency_milliseconds: fetchingSwapQuoteStartTime
? getDurationFromDateMilliseconds(fetchingSwapQuoteStartTime)
: undefined,
}
}
46 changes: 9 additions & 37 deletions src/pages/Swap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ export function Swap({
disableTokenInputs?: boolean
}) {
const { account, chainId: connectedChainId, connector } = useWeb3React()
const [newSwapQuoteNeedsLogging, setNewSwapQuoteNeedsLogging] = useState(true)
const [fetchingSwapQuoteStartTime, setFetchingSwapQuoteStartTime] = useState<Date | undefined>()
const trace = useTrace()

// token warning stuff
Expand Down Expand Up @@ -526,42 +524,16 @@ export function Swap({
const priceImpactTooHigh = priceImpactSeverity > 3 && !isExpertMode
const showPriceImpactWarning = largerPriceImpact && priceImpactSeverity > 3

// Handle time based logging events and event properties.
const prevTrade = usePrevious(trade)
useEffect(() => {
const now = new Date()
// If a trade exists, and we need to log the receipt of this new swap quote:
if (newSwapQuoteNeedsLogging && !!trade) {
// Set the current datetime as the time of receipt of latest swap quote.
setSwapQuoteReceivedDate(now)
// Log swap quote.
sendAnalyticsEvent(SwapEventName.SWAP_QUOTE_RECEIVED, {
...formatSwapQuoteReceivedEventProperties(
trade,
trade.gasUseEstimateUSD ?? undefined,
fetchingSwapQuoteStartTime
),
...trace,
})
// 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.
setNewSwapQuoteNeedsLogging(false)
// New quote is not being fetched, so set start time of quote fetch to undefined.
setFetchingSwapQuoteStartTime(undefined)
}
// If another swap quote is being loaded based on changed user inputs:
if (routeIsLoading) {
setNewSwapQuoteNeedsLogging(true)
if (!fetchingSwapQuoteStartTime) setFetchingSwapQuoteStartTime(now)
}
}, [
newSwapQuoteNeedsLogging,
routeIsSyncing,
routeIsLoading,
fetchingSwapQuoteStartTime,
trade,
setSwapQuoteReceivedDate,
trace,
])
if (!trade || prevTrade === trade) return // no new swap quote to log

setSwapQuoteReceivedDate(new Date())
sendAnalyticsEvent(SwapEventName.SWAP_QUOTE_RECEIVED, {
...formatSwapQuoteReceivedEventProperties(trade, trade.gasUseEstimateUSD ?? undefined),
...trace,
})
}, [prevTrade, trade, trace])

const showDetailsDropdown = Boolean(
!showWrap && userHasSpecifiedInputOutput && (trade || routeIsLoading || routeIsSyncing)
Expand Down

1 comment on commit 6424fdf

@vercel
Copy link

@vercel vercel bot commented on 6424fdf May 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

interface – ./

interface-uniswap.vercel.app
interface-git-main-uniswap.vercel.app

Please sign in to comment.