-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add telemetry to
Earn
(#1982)
- Loading branch information
1 parent
d889d1b
commit f6b1235
Showing
8 changed files
with
461 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { EarnEvent } from '@/core/analytics/types'; | ||
import { useEarnContext } from '@/earn/components/EarnProvider'; | ||
import { useDepositAnalytics } from '@/earn/hooks/useDepositAnalytics'; | ||
import { act, renderHook } from '@testing-library/react'; | ||
import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
||
const mockSendAnalytics = vi.fn(); | ||
vi.mock('../../core/analytics/hooks/useAnalytics', () => ({ | ||
useAnalytics: vi.fn(() => ({ | ||
sendAnalytics: mockSendAnalytics, | ||
})), | ||
})); | ||
|
||
vi.mock('@/earn/components/EarnProvider', () => ({ | ||
useEarnContext: vi.fn(), | ||
})); | ||
|
||
describe('useDepositAnalytics', () => { | ||
const mockContextData = { | ||
vaultAddress: '0xvault', | ||
vaultToken: { | ||
symbol: 'TEST', | ||
address: '0xtoken', | ||
}, | ||
recipientAddress: '0xrecipient', | ||
depositAmount: '100', | ||
}; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
(useEarnContext as Mock).mockReturnValue(mockContextData); | ||
}); | ||
|
||
it('should send initiated analytics when transaction starts building', () => { | ||
const { result } = renderHook(() => useDepositAnalytics('50')); | ||
|
||
act(() => { | ||
result.current.setTransactionState('buildingTransaction'); | ||
}); | ||
|
||
expect(mockSendAnalytics).toHaveBeenCalledWith( | ||
EarnEvent.EarnDepositInitiated, | ||
{ | ||
amount: 100, | ||
address: '0xrecipient', | ||
tokenAddress: '0xtoken', | ||
vaultAddress: '0xvault', | ||
}, | ||
); | ||
}); | ||
|
||
it('should send success analytics only once', () => { | ||
const { result } = renderHook(() => useDepositAnalytics('50')); | ||
|
||
act(() => { | ||
result.current.setTransactionState('success'); | ||
result.current.setTransactionState('success'); | ||
}); | ||
|
||
expect(mockSendAnalytics).toHaveBeenCalledTimes(1); | ||
expect(mockSendAnalytics).toHaveBeenCalledWith( | ||
EarnEvent.EarnDepositSuccess, | ||
{ | ||
amount: 100, | ||
address: '0xrecipient', | ||
tokenAddress: '0xtoken', | ||
vaultAddress: '0xvault', | ||
}, | ||
); | ||
}); | ||
|
||
it('should send error analytics only once', () => { | ||
const { result } = renderHook(() => useDepositAnalytics('50')); | ||
|
||
act(() => { | ||
result.current.setTransactionState('error'); | ||
result.current.setTransactionState('error'); | ||
}); | ||
|
||
expect(mockSendAnalytics).toHaveBeenCalledTimes(1); | ||
expect(mockSendAnalytics).toHaveBeenCalledWith( | ||
EarnEvent.EarnDepositFailure, | ||
{ | ||
amount: 100, | ||
address: '0xrecipient', | ||
tokenAddress: '0xtoken', | ||
vaultAddress: '0xvault', | ||
}, | ||
); | ||
}); | ||
|
||
it('should use depositedAmount when depositAmount is 0', () => { | ||
(useEarnContext as Mock).mockReturnValue({ | ||
...mockContextData, | ||
depositAmount: '0', | ||
}); | ||
|
||
const { result } = renderHook(() => useDepositAnalytics('50')); | ||
|
||
act(() => { | ||
result.current.setTransactionState('buildingTransaction'); | ||
}); | ||
|
||
expect(mockSendAnalytics).toHaveBeenCalledWith( | ||
expect.any(String), | ||
expect.objectContaining({ | ||
amount: 50, | ||
}), | ||
); | ||
}); | ||
|
||
it('should handle missing context values', () => { | ||
(useEarnContext as Mock).mockReturnValue({ | ||
vaultAddress: '0xvault', | ||
vaultToken: null, | ||
recipientAddress: null, | ||
depositAmount: '100', | ||
}); | ||
|
||
const { result } = renderHook(() => useDepositAnalytics('50')); | ||
|
||
act(() => { | ||
result.current.setTransactionState('buildingTransaction'); | ||
}); | ||
|
||
expect(mockSendAnalytics).toHaveBeenCalledWith( | ||
EarnEvent.EarnDepositInitiated, | ||
{ | ||
amount: 100, | ||
address: '', | ||
tokenAddress: '', | ||
vaultAddress: '0xvault', | ||
}, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useAnalytics } from '@/core/analytics/hooks/useAnalytics'; | ||
import { EarnEvent } from '@/core/analytics/types'; | ||
import { useEarnContext } from '@/earn/components/EarnProvider'; | ||
import type { LifecycleStatus } from '@/transaction/types'; | ||
import { useEffect, useMemo, useRef, useState } from 'react'; | ||
|
||
export const useDepositAnalytics = (depositedAmount: string) => { | ||
const [transactionState, setTransactionState] = useState< | ||
LifecycleStatus['statusName'] | null | ||
>(null); | ||
// Undesirable, but required because Transaction emits multiple success and error events | ||
const successSent = useRef(false); | ||
const errorSent = useRef(false); | ||
const { sendAnalytics } = useAnalytics(); | ||
const { vaultAddress, vaultToken, recipientAddress, depositAmount } = | ||
useEarnContext(); | ||
|
||
const analyticsData = useMemo( | ||
() => ({ | ||
amount: Number(depositAmount) || Number(depositedAmount), // fall back to depositedAmount to avoid sending 0 | ||
address: recipientAddress ?? '', | ||
tokenAddress: vaultToken?.address ?? '', | ||
vaultAddress, | ||
}), | ||
[ | ||
depositedAmount, | ||
depositAmount, | ||
recipientAddress, | ||
vaultToken?.address, | ||
vaultAddress, | ||
], | ||
); | ||
|
||
useEffect(() => { | ||
if (transactionState === 'buildingTransaction') { | ||
successSent.current = false; // in case user does a second deposit | ||
sendAnalytics(EarnEvent.EarnDepositInitiated, analyticsData); | ||
} | ||
|
||
if (transactionState === 'success' && !successSent.current) { | ||
successSent.current = true; | ||
sendAnalytics(EarnEvent.EarnDepositSuccess, analyticsData); | ||
} | ||
|
||
if (transactionState === 'error' && !errorSent.current) { | ||
errorSent.current = true; | ||
sendAnalytics(EarnEvent.EarnDepositFailure, analyticsData); | ||
} | ||
}, [transactionState, analyticsData, sendAnalytics]); | ||
|
||
return { | ||
setTransactionState, | ||
}; | ||
}; |
Oops, something went wrong.