Skip to content

Commit

Permalink
chore(swap): track swapId for same-chain swap events (#6398)
Browse files Browse the repository at this point in the history
### Description

It is expected that each submitted same-chain swap should result either
in success or an error.

From an analytics perspective, the number of `swap_review_submit` events
must equal the sum of `swap_execute_success` and `swap_execute_error`
events.

Unfortunately, currently we have more `swap_reveiw_submit` events than
the sum of success and error ones. It suggests that either we send
duplicated submit events, or miss some success or error events.

This PR adds a `swapId` property shared between sequential swap events
to ensure we can trace them in analytics.

Context:
https://valora-app.slack.com/archives/C029Z1QMD7B/p1733859318155609

### Test plan

Updated unit tests

### Related issues

- Related to RET-1276

### Backwards compatibility

Y

### Network scalability

If a new NetworkId and/or Network are added in the future, the changes
in this PR will:

- [x] Continue to work without code changes, OR trigger a compilation
error (guaranteeing we find it when a new network is added)
  • Loading branch information
bakoushin authored Jan 6, 2025
1 parent 9815138 commit 6c5af6f
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/analytics/Properties.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ type SwapQuoteEvent = SwapEvent & {
appFeePercentageIncludedInPrice: string | null | undefined
provider: string
swapType: SwapType
swapId: string
}

export interface SwapTimeMetrics {
Expand Down
12 changes: 12 additions & 0 deletions src/swap/SwapScreen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
mockTokenBalances,
mockUSDCTokenId,
} from 'test/values'
import { v4 as uuidv4 } from 'uuid'

const mockFetch = fetch as FetchMock
const mockGetNumberFormatSettings = jest.fn()
Expand Down Expand Up @@ -79,6 +80,12 @@ jest.mock('src/viem/estimateFeesPerGas', () => ({
})),
}))

jest.mock('uuid', () => ({
v4: jest.fn().mockReturnValue('mocked-uuid'),
}))

const mockedUuidv4 = uuidv4 as jest.Mock

const mockStoreTokenBalances = {
[mockCeurTokenId]: {
...mockTokenBalances[mockCeurTokenId],
Expand Down Expand Up @@ -1221,6 +1228,10 @@ describe('SwapScreen', () => {

it('should have correct analytics on swap submission', async () => {
mockFetch.mockResponse(defaultQuoteResponse)

const mockSwapId = 'test-swap-id'
mockedUuidv4.mockReturnValue(mockSwapId)

const { getByText, swapScreen } = renderScreen({})

selectSwapTokens('CELO', 'cUSD', swapScreen)
Expand Down Expand Up @@ -1261,6 +1272,7 @@ describe('SwapScreen', () => {
feeCurrencySymbol: 'CELO',
txCount: 2,
swapType: 'same-chain',
swapId: mockSwapId,
})
})

Expand Down
4 changes: 3 additions & 1 deletion src/swap/SwapScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,10 @@ export function SwapScreen({ route }: Props) {
// to confirm the swap in this case.
break
case 'possible':
const swapId = uuidv4()

AppAnalytics.track(SwapEvents.swap_review_submit, {
swapId,
toToken: toToken.address,
toTokenId: toToken.tokenId,
toTokenNetworkId: toToken.networkId,
Expand All @@ -431,7 +434,6 @@ export function SwapScreen({ route }: Props) {
),
})

const swapId = uuidv4()
localDispatch(startSwap({ swapId }))
dispatch(
swapStart({
Expand Down
12 changes: 12 additions & 0 deletions src/swap/SwapScreenV2.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
mockTokenBalances,
mockUSDCTokenId,
} from 'test/values'
import { v4 as uuidv4 } from 'uuid'

const mockFetch = fetch as FetchMock
const mockGetNumberFormatSettings = jest.fn()
Expand Down Expand Up @@ -80,6 +81,12 @@ jest.mock('src/viem/estimateFeesPerGas', () => ({
})),
}))

jest.mock('uuid', () => ({
v4: jest.fn().mockReturnValue('mocked-uuid'),
}))

const mockedUuidv4 = uuidv4 as jest.Mock

const mockStoreTokenBalances = {
[mockCeurTokenId]: {
...mockTokenBalances[mockCeurTokenId],
Expand Down Expand Up @@ -1313,6 +1320,10 @@ describe('SwapScreen', () => {

it('should have correct analytics on swap submission', async () => {
mockFetch.mockResponse(defaultQuoteResponse)

const mockSwapId = 'test-swap-id'
mockedUuidv4.mockReturnValue(mockSwapId)

const { getByText, swapScreen } = renderScreen({})

selectSwapTokens('CELO', 'cUSD', swapScreen)
Expand Down Expand Up @@ -1353,6 +1364,7 @@ describe('SwapScreen', () => {
feeCurrencySymbol: 'CELO',
txCount: 2,
swapType: 'same-chain',
swapId: mockSwapId,
})
})

Expand Down
4 changes: 3 additions & 1 deletion src/swap/SwapScreenV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,10 @@ export default function SwapScreenV2({ route }: Props) {
// to confirm the swap in this case.
break
case 'possible':
const swapId = uuidv4()

AppAnalytics.track(SwapEvents.swap_review_submit, {
swapId,
toToken: toToken.address,
toTokenId: toToken.tokenId,
toTokenNetworkId: toToken.networkId,
Expand All @@ -488,7 +491,6 @@ export default function SwapScreenV2({ route }: Props) {
),
})

const swapId = uuidv4()
setStartedSwapId(swapId)
dispatch(
swapStart({
Expand Down
2 changes: 2 additions & 0 deletions src/swap/saga.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ describe(swapSubmitSaga, () => {
swapTxHash: '0x2',
areSwapTokensShuffled: false,
swapType: 'same-chain',
swapId: 'test-swap-id',
})

const analyticsProps = (AppAnalytics.track as jest.Mock).mock.calls[0][1]
Expand Down Expand Up @@ -787,6 +788,7 @@ describe(swapSubmitSaga, () => {
swapTxHash: undefined,
areSwapTokensShuffled: false,
swapType: 'same-chain',
swapId: 'test-swap-id',
})
const analyticsProps = (AppAnalytics.track as jest.Mock).mock.calls[0][1]
expect(analyticsProps.gas).toBeCloseTo(
Expand Down
2 changes: 2 additions & 0 deletions src/swap/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ export function* swapSubmitSaga(action: PayloadAction<SwapInfo>) {
...getTimeMetrics(),
...getSwapTxsReceiptAnalyticsProperties(trackedTxs, networkId, tokensById),
swapType,
swapId,
})
}
} catch (err) {
Expand All @@ -315,6 +316,7 @@ export function* swapSubmitSaga(action: PayloadAction<SwapInfo>) {
...getTimeMetrics(),
...getSwapTxsReceiptAnalyticsProperties(trackedTxs, networkId, tokensById),
error: error.message,
swapId,
})
}
}
Expand Down

0 comments on commit 6c5af6f

Please sign in to comment.