Skip to content
Open
Changes from all 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
20 changes: 15 additions & 5 deletions apps/extension/src/pages/ibc-swap/hooks/use-swap-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export const useSwapAnalytics = ({
const prevFractionRef = useRef(0);
const prevSlippageRef = useRef(uiConfigStore.ibcSwapConfig.slippageNum);
const prevFetchingRef = useRef(false);
const prevQueryRouteRef =
useRef<ReturnType<SwapAmountConfig["getQueryRoute"]>>(undefined);
const prevRouteKeyRef = useRef("");
const loggedQuoteFailKeysRef = useRef<Set<string>>(new Set());

Expand All @@ -85,7 +87,10 @@ export const useSwapAnalytics = ({
if (milestoneEvents.has(eventName) && id) {
const now = performance.now();
if (eventName === "swap_quote_requested" || !durationRef.current[id]) {
durationRef.current[id] = { first: now, prev: now };
durationRef.current[id] = {
first: requestStartedAtRef.current ?? now,
prev: now,
};
}

if (eventName !== "swap_quote_requested") {
Expand Down Expand Up @@ -242,7 +247,12 @@ export const useSwapAnalytics = ({
return;
}

if (!prevFetchingRef.current && queryRouteForLog.isFetching) {
const isNewQuery = queryRouteForLog !== prevQueryRouteRef.current;

if (
queryRouteForLog.isFetching &&
(!prevFetchingRef.current || isNewQuery)
) {
requestStartedAtRef.current = performance.now();
logEvent("swap_quote_requested", {
in_chain_identifier: inChainIdentifier,
Expand All @@ -257,8 +267,9 @@ export const useSwapAnalytics = ({
});
}
prevFetchingRef.current = queryRouteForLog.isFetching;
prevQueryRouteRef.current = queryRouteForLog;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [queryRouteForLog?.isFetching]);
}, [queryRouteForLog, queryRouteForLog?.isFetching]);

// Quote received
useEffect(() => {
Expand Down Expand Up @@ -326,8 +337,6 @@ export const useSwapAnalytics = ({
const durationMs = requestStartedAtRef.current
? performance.now() - requestStartedAtRef.current
: undefined;
requestStartedAtRef.current = undefined;

logEvent("swap_quote_received", {
quote_id: quoteId,
duration_ms: durationMs,
Expand All @@ -346,6 +355,7 @@ export const useSwapAnalytics = ({
route_duration_estimate_sec: estimated_time,
swap_venues: swapVenues,
});
requestStartedAtRef.current = undefined;
quoteIdRef.current = quoteId;
prevRouteKeyRef.current = currentKey;
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down