Skip to content

Commit

Permalink
refactor(earn): consolidate analytics events (#5450)
Browse files Browse the repository at this point in the history
### Description

Use consistent naming, include some common properties in events

### Test plan

Unit tests

### Related issues

N/A

### Backwards compatibility

Yes

### 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
satish-ravi authored May 22, 2024
1 parent 211c71d commit 8b27b0a
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 56 deletions.
43 changes: 19 additions & 24 deletions src/analytics/Properties.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1581,18 +1581,18 @@ interface PointsEventsProperties {
[PointsEvents.points_screen_activity_learn_more_press]: undefined
}

interface EarnDepositProperties {
tokenId: string
interface EarnCommonProperties {
providerId: 'aave-v3'
networkId: NetworkId
depositTokenId: string
}

interface EarnDepositProperties extends EarnCommonProperties {
tokenAmount: string
providerId: string
}

interface EarnWithdrawProperties {
tokenId: string
interface EarnWithdrawProperties extends EarnCommonProperties {
tokenAmount: string
networkId: NetworkId
providerId: string
rewards: SerializableRewardsInfo[]
}

Expand All @@ -1612,38 +1612,33 @@ interface EarnEventsProperties {
[EarnEvents.earn_add_crypto_action_press]: {
action: TokenActionName
} & TokenProperties
[EarnEvents.earn_deposit_provider_info_press]: undefined
[EarnEvents.earn_deposit_terms_and_conditions_press]: undefined
[EarnEvents.earn_deposit_complete]: undefined
[EarnEvents.earn_deposit_cancel]: undefined
[EarnEvents.earn_deposit_provider_info_press]: EarnDepositProperties
[EarnEvents.earn_deposit_terms_and_conditions_press]: EarnDepositProperties
[EarnEvents.earn_deposit_complete]: EarnDepositProperties
[EarnEvents.earn_deposit_cancel]: EarnDepositProperties
[EarnEvents.earn_deposit_submit_start]: EarnDepositProperties
[EarnEvents.earn_deposit_submit_success]: EarnDepositProperties & EarnDepositTxsReceiptProperties
[EarnEvents.earn_deposit_submit_error]: EarnDepositProperties &
EarnDepositTxsReceiptProperties & {
error: string
}
[EarnEvents.earn_deposit_submit_cancel]: EarnDepositProperties
[EarnEvents.earn_view_pools_press]: undefined
[EarnEvents.earn_view_pools_press]: {
poolTokenId: string
networkId: string
providerId: 'aave-v3'
}
[EarnEvents.earn_enter_amount_info_press]: undefined
[EarnEvents.earn_enter_amount_continue_press]: {
userHasFunds: boolean
tokenAmount: string
amountInUsd: string
amountEnteredIn: AmountEnteredIn
tokenId: string
networkId: string
}
} & EarnDepositProperties
[EarnEvents.earn_enter_amount_info_more_pools]: undefined
[EarnEvents.earn_exit_pool_press]: {
poolTokenId: string
networkId: NetworkId
tokenAmount: string
providerId: string
}
[EarnEvents.earn_deposit_more_press]: {
depositTokenId: string
providerId: string
}
} & EarnCommonProperties
[EarnEvents.earn_deposit_more_press]: EarnCommonProperties
[EarnEvents.earn_deposit_add_gas_press]: { gasTokenId: string }
[EarnEvents.earn_feed_item_select]: {
origin: 'EarnDeposit' | 'EarnWithdraw' | 'EarnClaimReward'
Expand Down
9 changes: 7 additions & 2 deletions src/earn/EarnActivePool.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ describe('EarnActivePool', () => {
)

fireEvent.press(getByText('earnFlow.activePools.viewPools'))
expect(ValoraAnalytics.track).toHaveBeenCalledWith(EarnEvents.earn_view_pools_press)
expect(ValoraAnalytics.track).toHaveBeenCalledWith(EarnEvents.earn_view_pools_press, {
poolTokenId: networkConfig.aaveArbUsdcTokenId,
networkId: NetworkId['arbitrum-sepolia'],
providerId: 'aave-v3',
})
expect(navigate).toBeCalledWith(Screens.TabDiscover)
})

Expand All @@ -90,7 +94,7 @@ describe('EarnActivePool', () => {

fireEvent.press(getByText('earnFlow.activePools.exitPool'))
expect(ValoraAnalytics.track).toHaveBeenCalledWith(EarnEvents.earn_exit_pool_press, {
poolTokenId: networkConfig.aaveArbUsdcTokenId,
depositTokenId: networkConfig.arbUsdcTokenId,
networkId: NetworkId['arbitrum-sepolia'],
tokenAmount: '10.75',
providerId: 'aave-v3',
Expand All @@ -116,6 +120,7 @@ describe('EarnActivePool', () => {
expect(ValoraAnalytics.track).toHaveBeenCalledWith(EarnEvents.earn_deposit_more_press, {
depositTokenId: networkConfig.arbUsdcTokenId,
providerId: 'aave-v3',
networkId: NetworkId['arbitrum-sepolia'],
})
expect(navigate).toBeCalledWith(Screens.EarnEnterAmount, {
tokenId: networkConfig.arbUsdcTokenId,
Expand Down
28 changes: 18 additions & 10 deletions src/earn/EarnActivePool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ValoraAnalytics from 'src/analytics/ValoraAnalytics'
import Button, { BtnSizes, BtnTypes } from 'src/components/Button'
import SkeletonPlaceholder from 'src/components/SkeletonPlaceholder'
import TokenDisplay from 'src/components/TokenDisplay'
import { PROVIDER_ID } from 'src/earn/constants'
import { useAavePoolInfo } from 'src/earn/hooks'
import UpwardGraph from 'src/icons/UpwardGraph'
import { navigate } from 'src/navigator/NavigationService'
Expand Down Expand Up @@ -45,14 +46,20 @@ export default function EarnActivePool({ depositTokenId, poolTokenId, cta }: Pro
const poolToken = useTokenInfo(poolTokenId)
const asyncPoolInfo = useAavePoolInfo({ depositTokenId })

if (!poolToken) {
// should never happen
Logger.error(TAG, `No pool token found ${poolTokenId}`)
return null
}

return (
<View style={styles.card} testID="EarnActivePool">
<View style={styles.container}>
<Text style={styles.title}>{t('earnFlow.activePools.title')}</Text>
<View>
<View style={styles.row}>
<Text style={styles.totalValueText}>{t('earnFlow.activePools.totalValue')}</Text>
{poolToken && poolToken.balance && (
{poolToken.balance && (
<TokenDisplay
amount={poolToken.balance}
showLocalAmount={false}
Expand All @@ -74,7 +81,7 @@ export default function EarnActivePool({ depositTokenId, poolTokenId, cta }: Pro
<UpwardGraph />
</View>
)}
{poolToken && poolToken.balance && (
{poolToken.balance && (
<TokenDisplay
amount={poolToken.balance}
showLocalAmount={true}
Expand All @@ -88,15 +95,11 @@ export default function EarnActivePool({ depositTokenId, poolTokenId, cta }: Pro
<View style={styles.buttonContainer}>
<Button
onPress={() => {
if (!poolToken) {
Logger.warn(TAG, 'No pool token found')
return
}
ValoraAnalytics.track(EarnEvents.earn_exit_pool_press, {
poolTokenId,
depositTokenId,
networkId: poolToken.networkId,
tokenAmount: poolToken.balance.toString(),
providerId: 'aave-v3',
providerId: PROVIDER_ID,
})
navigate(Screens.EarnCollectScreen, { depositTokenId, poolTokenId })
}}
Expand All @@ -109,7 +112,8 @@ export default function EarnActivePool({ depositTokenId, poolTokenId, cta }: Pro
onPress={() => {
ValoraAnalytics.track(EarnEvents.earn_deposit_more_press, {
depositTokenId,
providerId: 'aave-v3',
providerId: PROVIDER_ID,
networkId: poolToken.networkId,
})
navigate(Screens.EarnEnterAmount, { tokenId: depositTokenId })
}}
Expand All @@ -124,7 +128,11 @@ export default function EarnActivePool({ depositTokenId, poolTokenId, cta }: Pro
<View style={styles.buttonContainer}>
<Button
onPress={() => {
ValoraAnalytics.track(EarnEvents.earn_view_pools_press)
ValoraAnalytics.track(EarnEvents.earn_view_pools_press, {
poolTokenId,
networkId: poolToken.networkId,
providerId: PROVIDER_ID,
})
navigate(Screens.TabDiscover)
}}
text={t('earnFlow.activePools.viewPools')}
Expand Down
2 changes: 1 addition & 1 deletion src/earn/EarnCollectScreen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ describe('EarnCollectScreen', () => {
])

expect(ValoraAnalytics.track).toHaveBeenCalledWith(EarnEvents.earn_collect_earnings_press, {
tokenId: mockArbUsdcTokenId,
depositTokenId: mockArbUsdcTokenId,
tokenAmount: '10.75',
networkId: NetworkId['arbitrum-sepolia'],
providerId: 'aave-v3',
Expand Down
5 changes: 3 additions & 2 deletions src/earn/EarnCollectScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Button, { BtnSizes } from 'src/components/Button'
import InLineNotification, { NotificationVariant } from 'src/components/InLineNotification'
import TokenDisplay from 'src/components/TokenDisplay'
import TokenIcon, { IconSize } from 'src/components/TokenIcon'
import { PROVIDER_ID } from 'src/earn/constants'
import { useAavePoolInfo, useAaveRewardsInfoAndPrepareTransactions } from 'src/earn/hooks'
import { withdrawStatusSelector } from 'src/earn/selectors'
import { withdrawStart } from 'src/earn/slice'
Expand Down Expand Up @@ -72,10 +73,10 @@ export default function EarnCollectScreen({ route }: Props) {
)

ValoraAnalytics.track(EarnEvents.earn_collect_earnings_press, {
tokenId: depositTokenId,
depositTokenId,
tokenAmount: poolToken.balance.toString(),
networkId: depositToken.networkId,
providerId: 'aave-v3',
providerId: PROVIDER_ID,
rewards: serializedRewards,
})
}
Expand Down
33 changes: 29 additions & 4 deletions src/earn/EarnDepositBottomSheet.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { Provider } from 'react-redux'
import { EarnEvents } from 'src/analytics/Events'
import ValoraAnalytics from 'src/analytics/ValoraAnalytics'
import EarnDepositBottomSheet from 'src/earn/EarnDepositBottomSheet'
import { PROVIDER_ID } from 'src/earn/constants'
import { depositStart } from 'src/earn/slice'
import { navigate } from 'src/navigator/NavigationService'
import { getDynamicConfigParams } from 'src/statsig'
import { StatsigDynamicConfigs } from 'src/statsig/types'
import { NetworkId } from 'src/transactions/types'
import { PreparedTransactionsPossible } from 'src/viem/prepareTransactions'
import { getSerializablePreparedTransactions } from 'src/viem/preparedTransactionSerialization'
import { createMockStore } from 'test/utils'
Expand Down Expand Up @@ -48,6 +50,13 @@ const mockPreparedTransaction: PreparedTransactionsPossible = {
}

describe('EarnDepositBottomSheet', () => {
const expectedAnalyticsProperties = {
depositTokenId: mockArbEthTokenId,
tokenAmount: '100',
networkId: NetworkId['arbitrum-sepolia'],
providerId: PROVIDER_ID,
}

beforeEach(() => {
jest.clearAllMocks()
jest.mocked(getDynamicConfigParams).mockImplementation(({ configName, defaultValues }) => {
Expand All @@ -72,6 +81,7 @@ describe('EarnDepositBottomSheet', () => {
amount={'100'}
tokenId={mockArbEthTokenId}
preparedTransaction={mockPreparedTransaction}
networkId={NetworkId['arbitrum-sepolia']}
/>
</Provider>
)
Expand Down Expand Up @@ -106,12 +116,16 @@ describe('EarnDepositBottomSheet', () => {
amount={'100'}
tokenId={mockArbEthTokenId}
preparedTransaction={mockPreparedTransaction}
networkId={NetworkId['arbitrum-sepolia']}
/>
</Provider>
)

fireEvent.press(getByTestId('EarnDeposit/PrimaryCta'))
expect(ValoraAnalytics.track).toHaveBeenCalledWith(EarnEvents.earn_deposit_complete)
expect(ValoraAnalytics.track).toHaveBeenCalledWith(
EarnEvents.earn_deposit_complete,
expectedAnalyticsProperties
)
expect(store.getActions()).toEqual([
{
type: depositStart.type,
Expand All @@ -134,12 +148,16 @@ describe('EarnDepositBottomSheet', () => {
amount={'100'}
tokenId={mockArbEthTokenId}
preparedTransaction={mockPreparedTransaction}
networkId={NetworkId['arbitrum-sepolia']}
/>
</Provider>
)

fireEvent.press(getByTestId('EarnDeposit/SecondaryCta'))
expect(ValoraAnalytics.track).toHaveBeenCalledWith(EarnEvents.earn_deposit_cancel)
expect(ValoraAnalytics.track).toHaveBeenCalledWith(
EarnEvents.earn_deposit_cancel,
expectedAnalyticsProperties
)
})

it('pressing provider info opens the terms and conditions', () => {
Expand All @@ -150,12 +168,16 @@ describe('EarnDepositBottomSheet', () => {
amount={'100'}
tokenId={mockArbEthTokenId}
preparedTransaction={mockPreparedTransaction}
networkId={NetworkId['arbitrum-sepolia']}
/>
</Provider>
)

fireEvent.press(getByTestId('EarnDeposit/ProviderInfo'))
expect(ValoraAnalytics.track).toHaveBeenCalledWith(EarnEvents.earn_deposit_provider_info_press)
expect(ValoraAnalytics.track).toHaveBeenCalledWith(
EarnEvents.earn_deposit_provider_info_press,
expectedAnalyticsProperties
)
expect(navigate).toHaveBeenCalledWith('WebViewScreen', { uri: 'termsUrl' })
})

Expand All @@ -167,13 +189,15 @@ describe('EarnDepositBottomSheet', () => {
amount={'100'}
tokenId={mockArbEthTokenId}
preparedTransaction={mockPreparedTransaction}
networkId={NetworkId['arbitrum-sepolia']}
/>
</Provider>
)

fireEvent.press(getByTestId('EarnDeposit/TermsAndConditions'))
expect(ValoraAnalytics.track).toHaveBeenCalledWith(
EarnEvents.earn_deposit_terms_and_conditions_press
EarnEvents.earn_deposit_terms_and_conditions_press,
expectedAnalyticsProperties
)
expect(navigate).toHaveBeenCalledWith('WebViewScreen', { uri: 'termsUrl' })
})
Expand All @@ -190,6 +214,7 @@ describe('EarnDepositBottomSheet', () => {
amount={'100'}
tokenId={mockArbEthTokenId}
preparedTransaction={mockPreparedTransaction}
networkId={NetworkId['arbitrum-sepolia']}
/>
</Provider>
)
Expand Down
Loading

0 comments on commit 8b27b0a

Please sign in to comment.