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

refactor(ramp): remove anonymous events #12351

Merged
merged 7 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion app/components/UI/Ramp/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/* eslint-disable import/prefer-default-export */
import { AnalyticsEvents } from '../types';

export const AnonymousEvents: (keyof AnalyticsEvents)[] = [];

export const RAMPS_SEND = 'RAMPS_SEND';
37 changes: 20 additions & 17 deletions app/components/UI/Ramp/hooks/useAnalytics.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MetaMetrics, MetaMetricsEvents } from '../../../../core/Analytics';
import { MetricsEventBuilder } from '../../../../core/Analytics/MetricsEventBuilder';
import { renderHookWithProvider } from '../../../../util/test/renderWithProvider';
import useAnalytics from './useAnalytics';

Expand All @@ -24,38 +25,40 @@ describe('useAnalytics', () => {
const { result } = renderHookWithProvider(() => useAnalytics());

const testEvent = 'BUY_BUTTON_CLICKED';

result.current(testEvent, {
const testEventParams = {
location: 'Amount to Buy Screen',
text: 'Buy',
});
} as const;

result.current(testEvent, testEventParams);

expect(MetaMetrics.getInstance().trackEvent).toHaveBeenCalledWith(
MetaMetricsEvents[testEvent],
{
location: 'Amount to Buy Screen',
text: 'Buy',
},
MetricsEventBuilder.createEventBuilder(MetaMetricsEvents[testEvent])
.addProperties(testEventParams)
.build(),
);
});

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

const testEvent = 'RAMP_REGION_SELECTED';
const testPayload = {
const testEventParams = {
country_id: 'test-country-id',
is_unsupported_offramp: false,
is_unsupported_onramp: false,
};
} as const;

jest.mock('../constants', () => ({
AnonymousEvents: [testEvent],
}));

const { result } = renderHookWithProvider(() => useAnalytics());

result.current(testEvent, testPayload);
result.current(testEvent, testEventParams);

expect(MetaMetrics.getInstance().trackEvent).toHaveBeenCalledWith(
MetaMetricsEvents[testEvent],
{
sensitiveProperties: testPayload,
},
MetricsEventBuilder.createEventBuilder(MetaMetricsEvents[testEvent])
.addSensitiveProperties(testEventParams)
.build(),
);
});
});
46 changes: 9 additions & 37 deletions app/components/UI/Ramp/hooks/useAnalytics.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,25 @@
import { useCallback } from 'react';
import { InteractionManager } from 'react-native';
import { AnalyticsEvents } from '../types';
import { AnonymousEvents } from '../constants';
import { MetaMetrics, MetaMetricsEvents } from '../../../../core/Analytics';

const AnonymousEvents: (keyof AnalyticsEvents)[] = [
'RAMP_REGION_SELECTED',
'RAMP_REGION_RESET',
'ONRAMP_GET_STARTED_CLICKED',
'ONRAMP_PAYMENT_METHOD_SELECTED',
'ONRAMP_QUOTES_REQUESTED',
'ONRAMP_QUOTES_RECEIVED',
'ONRAMP_PROVIDER_SELECTED',
'ONRAMP_PURCHASE_COMPLETED',
'ONRAMP_PURCHASE_FAILED',
'ONRAMP_PROVIDER_DETAILS_VIEWED',
'ONRAMP_QUOTE_ERROR',

'OFFRAMP_GET_STARTED_CLICKED',
'OFFRAMP_PAYMENT_METHOD_SELECTED',
'OFFRAMP_QUOTES_REQUESTED',
'OFFRAMP_QUOTES_RECEIVED',
'OFFRAMP_PROVIDER_SELECTED',
'OFFRAMP_PURCHASE_COMPLETED',
'OFFRAMP_PURCHASE_FAILED',
'OFFRAMP_PROVIDER_DETAILS_VIEWED',
'OFFRAMP_QUOTE_ERROR',

'OFFRAMP_SEND_CRYPTO_PROMPT_VIEWED',
'OFFRAMP_SEND_TRANSACTION_INVOKED',
'OFFRAMP_SEND_TRANSACTION_CONFIRMED',
'OFFRAMP_SEND_TRANSACTION_REJECTED',
];
import { MetricsEventBuilder } from '../../../../core/Analytics/MetricsEventBuilder';

export function trackEvent<T extends keyof AnalyticsEvents>(
eventType: T,
params: AnalyticsEvents[T],
) {
const metrics = MetaMetrics.getInstance();
const event = MetaMetricsEvents[eventType];
const anonymous = AnonymousEvents.includes(eventType);
const metrics = MetaMetrics.getInstance();
const event = MetricsEventBuilder.createEventBuilder(
MetaMetricsEvents[eventType],
);

InteractionManager.runAfterInteractions(() => {
if (anonymous) {
metrics.trackEvent(event, {
sensitiveProperties: { ...params },
});
metrics.trackEvent(event.addSensitiveProperties({ ...params }).build());
} else {
metrics.trackEvent(event, {
...params,
});
metrics.trackEvent(event.addProperties({ ...params }).build());
}
});
}
Expand Down
Loading