Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update metrics logic #10545

Merged
merged 21 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ describe('PermissionApproval', () => {
jest.resetAllMocks();
(useMetrics as jest.MockedFn<typeof useMetrics>).mockReturnValue({
trackEvent: mockTrackEvent,
trackAnonymousEvent: jest.fn(),
enable: jest.fn(),
addTraitsToUser: jest.fn(),
createDataDeletionTask: jest.fn(),
Expand Down
11 changes: 3 additions & 8 deletions app/components/Nav/Main/RootRPCMethodsUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export const useSwapConfirmedEvent = ({ trackSwaps }) => {
};

const RootRPCMethodsUI = (props) => {
const { trackEvent, trackAnonymousEvent } = useMetrics();
const { trackEvent } = useMetrics();
const [transactionModalType, setTransactionModalType] = useState(undefined);
const tokenList = useSelector(selectTokenList);
const setTransactionObject = props.setTransactionObject;
Expand Down Expand Up @@ -237,20 +237,15 @@ const RootRPCMethodsUI = (props) => {
...smartTransactionMetricsProperties,
};

trackAnonymousEvent(event, parameters);
trackEvent(event, { sensitiveProperties: { ...parameters } });
} catch (e) {
Logger.error(e, MetaMetricsEvents.SWAP_TRACKING_FAILED);
trackEvent(MetaMetricsEvents.SWAP_TRACKING_FAILED, {
error: e,
});
}
},
[
props.selectedAddress,
props.shouldUseSmartTransaction,
trackAnonymousEvent,
trackEvent,
],
[props.selectedAddress, props.shouldUseSmartTransaction, trackEvent],
);

const { addTransactionMetaIdForListening } = useSwapConfirmedEvent({
Expand Down
1 change: 0 additions & 1 deletion app/components/UI/OptinMetrics/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ jest.mock('../../../core/Analytics/MetaMetrics');

const mockMetrics = {
trackEvent: jest.fn().mockImplementation(() => Promise.resolve()),
trackAnonymousEvent: jest.fn(),
enable: jest.fn(() => Promise.resolve()),
addTraitsToUser: jest.fn(() => Promise.resolve()),
isEnabled: jest.fn(() => true),
Expand Down
11 changes: 6 additions & 5 deletions app/components/UI/Ramp/hooks/useAnalytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ jest.mock('../../../../core/Analytics', () => ({
MetaMetrics: {
getInstance: jest.fn().mockReturnValue({
trackEvent: jest.fn(),
trackAnonymousEvent: jest.fn(),
}),
},
}));
Expand All @@ -21,7 +20,7 @@ describe('useAnalytics', () => {
jest.clearAllMocks();
});

it('calls trackEvent with the correct params', () => {
it('calls trackEvent for non-anonymous params', () => {
const { result } = renderHookWithProvider(() => useAnalytics());

const testEvent = 'BUY_BUTTON_CLICKED';
Expand All @@ -40,7 +39,7 @@ describe('useAnalytics', () => {
);
});

it('calls trackAnonymousEvent with the correct params', () => {
it('calls trackEvent for anonymous params', () => {
const { result } = renderHookWithProvider(() => useAnalytics());

const testEvent = 'RAMP_REGION_SELECTED';
Expand All @@ -52,9 +51,11 @@ describe('useAnalytics', () => {

result.current(testEvent, testPayload);

expect(MetaMetrics.getInstance().trackAnonymousEvent).toHaveBeenCalledWith(
expect(MetaMetrics.getInstance().trackEvent).toHaveBeenCalledWith(
MetaMetricsEvents[testEvent],
testPayload,
{
sensitiveProperties: testPayload,
},
);
});
});
4 changes: 2 additions & 2 deletions app/components/UI/Ramp/hooks/useAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export function trackEvent<T extends keyof AnalyticsEvents>(
const anonymous = AnonymousEvents.includes(eventType);
InteractionManager.runAfterInteractions(() => {
if (anonymous) {
metrics.trackAnonymousEvent(event, {
...params,
metrics.trackEvent(event, {
sensitiveProperties: { ...params },
});
} else {
metrics.trackEvent(event, {
Expand Down
49 changes: 31 additions & 18 deletions app/components/UI/Swaps/QuotesView.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ function SwapsQuotesView({
const navigation = useNavigation();
/* Get params from navigation */
const route = useRoute();
const { trackAnonymousEvent, trackEvent } = useMetrics();
const { trackEvent } = useMetrics();

const { colors } = useTheme();
const styles = createStyles(colors);
Expand Down Expand Up @@ -744,15 +744,17 @@ function SwapsQuotesView({
chain_id: getDecimalChainId(chainId),
};

trackAnonymousEvent(MetaMetricsEvents.GAS_FEES_CHANGED, parameters);
trackEvent(MetaMetricsEvents.GAS_FEES_CHANGED, {
sensitiveProperties: { ...parameters },
});
},
[
chainId,
conversionRate,
currentCurrency,
gasEstimateType,
gasLimit,
trackAnonymousEvent,
trackEvent,
],
);

Expand Down Expand Up @@ -904,7 +906,9 @@ function SwapsQuotesView({
chain_id: getDecimalChainId(chainId),
is_smart_transaction: shouldUseSmartTransaction,
};
trackAnonymousEvent(MetaMetricsEvents.SWAP_STARTED, parameters);
trackEvent(MetaMetricsEvents.SWAP_STARTED, {
sensitiveProperties: { ...parameters },
});
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[
Expand Down Expand Up @@ -1150,7 +1154,9 @@ function SwapsQuotesView({
custom_spend_limit_amount: currentAmount,
chain_id: getDecimalChainId(chainId),
};
trackAnonymousEvent(MetaMetricsEvents.EDIT_SPEND_LIMIT_OPENED, parameters);
trackEvent(MetaMetricsEvents.EDIT_SPEND_LIMIT_OPENED, {
sensitiveProperties: { ...parameters },
});
}, [
chainId,
allQuotes,
Expand All @@ -1166,7 +1172,7 @@ function SwapsQuotesView({
slippage,
sourceAmount,
sourceToken,
trackAnonymousEvent,
trackEvent,
]);

const handleQuotesReceivedMetric = useCallback(() => {
Expand Down Expand Up @@ -1196,7 +1202,9 @@ function SwapsQuotesView({
available_quotes: allQuotes.length,
chain_id: getDecimalChainId(chainId),
};
trackAnonymousEvent(MetaMetricsEvents.QUOTES_RECEIVED, parameters);
trackEvent(MetaMetricsEvents.QUOTES_RECEIVED, {
sensitiveProperties: { ...parameters },
});
}, [
chainId,
sourceToken,
Expand All @@ -1209,7 +1217,7 @@ function SwapsQuotesView({
selectedQuoteValue,
allQuotes,
conversionRate,
trackAnonymousEvent,
trackEvent,
]);

const handleOpenQuotesModal = useCallback(() => {
Expand Down Expand Up @@ -1241,10 +1249,9 @@ function SwapsQuotesView({
chain_id: getDecimalChainId(chainId),
};

trackAnonymousEvent(
MetaMetricsEvents.ALL_AVAILABLE_QUOTES_OPENED,
parameters,
);
trackEvent(MetaMetricsEvents.ALL_AVAILABLE_QUOTES_OPENED, {
sensitiveProperties: { ...parameters },
});
}, [
chainId,
selectedQuote,
Expand All @@ -1258,7 +1265,7 @@ function SwapsQuotesView({
allQuotesFetchTime,
conversionRate,
allQuotes.length,
trackAnonymousEvent,
trackEvent,
]);

const handleQuotesErrorMetric = useCallback(
Expand All @@ -1281,12 +1288,16 @@ function SwapsQuotesView({
gas_fees: '',
};

trackAnonymousEvent(MetaMetricsEvents.QUOTES_TIMED_OUT, parameters);
trackEvent(MetaMetricsEvents.QUOTES_TIMED_OUT, {
sensitiveProperties: { ...parameters },
});
} else if (
error?.key === swapsUtils.SwapsError.QUOTES_NOT_AVAILABLE_ERROR
) {
const parameters = { ...data };
trackAnonymousEvent(MetaMetricsEvents.NO_QUOTES_AVAILABLE, parameters);
trackEvent(MetaMetricsEvents.NO_QUOTES_AVAILABLE, {
sensitiveProperties: { ...parameters },
});
} else {
trackErrorAsAnalytics(`Swaps: ${error?.key}`, error?.description);
}
Expand All @@ -1298,7 +1309,7 @@ function SwapsQuotesView({
destinationToken,
hasEnoughTokenBalance,
slippage,
trackAnonymousEvent,
trackEvent,
],
);

Expand Down Expand Up @@ -1563,7 +1574,9 @@ function SwapsQuotesView({
navigation.setParams({ selectedQuote: undefined });
navigation.setParams({ quoteBegin: Date.now() });

trackAnonymousEvent(MetaMetricsEvents.QUOTES_REQUESTED, data);
trackEvent(MetaMetricsEvents.QUOTES_REQUESTED, {
sensitiveProperties: { ...data },
});
}, [
chainId,
destinationToken,
Expand All @@ -1574,7 +1587,7 @@ function SwapsQuotesView({
sourceAmount,
sourceToken,
trackedRequestedQuotes,
trackAnonymousEvent,
trackEvent,
]);

/* Metrics: Quotes received */
Expand Down
14 changes: 8 additions & 6 deletions app/components/UI/Swaps/components/TokenSelectModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function TokenSelectModal({
balances,
}) {
const navigation = useNavigation();
const { trackAnonymousEvent } = useMetrics();
const { trackEvent } = useMetrics();

const searchInput = useRef(null);
const list = useRef();
Expand Down Expand Up @@ -304,15 +304,17 @@ function TokenSelectModal({
const handlePressImportToken = useCallback(
(item) => {
const { address, symbol } = item;
trackAnonymousEvent(MetaMetricsEvents.CUSTOM_TOKEN_IMPORTED, {
address,
symbol,
chain_id: getDecimalChainId(chainId),
trackEvent(MetaMetricsEvents.CUSTOM_TOKEN_IMPORTED, {
sensitiveProperties: {
address,
symbol,
chain_id: getDecimalChainId(chainId),
},
});
hideTokenImportModal();
onItemPress(item);
},
[chainId, hideTokenImportModal, onItemPress, trackAnonymousEvent],
[chainId, hideTokenImportModal, onItemPress, trackEvent],
);

const handleBlockExplorerPress = useCallback(() => {
Expand Down
6 changes: 4 additions & 2 deletions app/components/UI/Swaps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ function SwapsAmountView({
const navigation = useNavigation();
const route = useRoute();
const { colors } = useTheme();
const { trackAnonymousEvent } = useMetrics();
const { trackEvent } = useMetrics();
const styles = createStyles(colors);

const previousSelectedAddress = useRef();
Expand Down Expand Up @@ -277,7 +277,9 @@ function SwapsAmountView({
chain_id: getDecimalChainId(chainId),
};

trackAnonymousEvent(MetaMetricsEvents.SWAPS_OPENED, parameters);
trackEvent(MetaMetricsEvents.SWAPS_OPENED, {
sensitiveProperties: { ...parameters },
});
});
} else {
navigation.pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ jest.mock('../../../../../../core/Analytics/MetaMetrics');

const mockMetrics = {
trackEvent: jest.fn(),
trackAnonymousEvent: jest.fn(),
enable: jest.fn(() => Promise.resolve()),
addTraitsToUser: jest.fn(() => Promise.resolve()),
isEnabled: jest.fn(() => false),
Expand Down
9 changes: 0 additions & 9 deletions app/components/hooks/useMetrics/useMetrics.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const expectedDataDeleteRegulationId = 'TWV0YU1hc2t1c2Vzbm9wb2ludCE';

const mockMetrics = {
trackEvent: jest.fn(),
trackAnonymousEvent: jest.fn(),
enable: jest.fn(() => Promise.resolve()),
addTraitsToUser: jest.fn(() => Promise.resolve()),
createDataDeletionTask: jest.fn(() =>
Expand Down Expand Up @@ -65,7 +64,6 @@ describe('useMetrics', () => {
"getMetaMetricsId": undefined,
"isDataRecorded": [MockFunction],
"isEnabled": [MockFunction],
"trackAnonymousEvent": [Function],
"trackEvent": [Function],
}
`);
Expand All @@ -80,7 +78,6 @@ describe('useMetrics', () => {

const {
trackEvent,
trackAnonymousEvent,
enable,
addTraitsToUser,
createDataDeletionTask,
Expand All @@ -100,7 +97,6 @@ describe('useMetrics', () => {

await act(async () => {
trackEvent(event);
trackAnonymousEvent(event);
await enable(true);
await addTraitsToUser({});
deletionTaskIdValue = await createDataDeletionTask();
Expand All @@ -112,11 +108,6 @@ describe('useMetrics', () => {
});

expect(mockMetrics.trackEvent).toHaveBeenCalledWith(event, {}, true);
expect(mockMetrics.trackAnonymousEvent).toHaveBeenCalledWith(
event,
{},
true,
);
expect(mockMetrics.enable).toHaveBeenCalledWith(true);
expect(mockMetrics.addTraitsToUser).toHaveBeenCalledWith({});

Expand Down
Loading
Loading