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 marketing traits when tracked settings change [PR 1/2] #13254

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
46 changes: 19 additions & 27 deletions app/components/UI/OptinMetrics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ import Routes from '../../../constants/navigation/Routes';
import generateDeviceAnalyticsMetaData, {
UserSettingsAnalyticsMetaData as generateUserSettingsAnalyticsMetaData,
} from '../../../util/metrics';
import {
UserProfileProperty
} from '../../../util/metrics/UserSettingsAnalyticsMetaData/UserProfileAnalyticsMetaData.types';

const createStyles = ({ colors }) =>
StyleSheet.create({
Expand Down Expand Up @@ -338,44 +341,33 @@ class OptinMetrics extends PureComponent {
setDataCollectionForMarketing,
} = this.props;

// Set marketing consent trait based on user selection
const dataCollectionForMarketingTraits = {
has_marketing_consent: Boolean(
this.props.isDataCollectionForMarketingEnabled,
),
};

await metrics.enable();

// Handle null case for marketing consent
if (
isDataCollectionForMarketingEnabled === null &&
setDataCollectionForMarketing
) {
setDataCollectionForMarketing(false);
}

// Track the analytics preference event first
metrics.trackEvent(
metrics
.createEventBuilder(MetaMetricsEvents.ANALYTICS_PREFERENCE_SELECTED)
.addProperties({
...dataCollectionForMarketingTraits,
is_metrics_opted_in: true,
location: 'onboarding_metametrics',
updated_after_onboarding: false,
[UserProfileProperty.HAS_MARKETING_CONSENT]: Boolean( isDataCollectionForMarketingEnabled ),
is_metrics_opted_in: true,
location: 'onboarding_metametrics',
updated_after_onboarding: false,
})
.build(),
);

// Handle null case for marketing consent
if (
isDataCollectionForMarketingEnabled === null &&
setDataCollectionForMarketing
) {
setDataCollectionForMarketing(false);
}

// consolidate device and user settings traits
const consolidatedTraits = {
...dataCollectionForMarketingTraits,
is_metrics_opted_in: true,
...generateDeviceAnalyticsMetaData(),
...generateUserSettingsAnalyticsMetaData(),
};
await metrics.addTraitsToUser(consolidatedTraits);
await metrics.addTraitsToUser({
...generateDeviceAnalyticsMetaData(),
...generateUserSettingsAnalyticsMetaData(),
});

// track onboarding events that were stored before user opted in
// only if the user eventually opts in.
Expand Down
4 changes: 0 additions & 4 deletions app/components/UI/OptinMetrics/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ describe('OptinMetrics', () => {
expect(mockMetrics.addTraitsToUser).toHaveBeenNthCalledWith(1, {
deviceProp: 'Device value',
userProp: 'User value',
is_metrics_opted_in: true,
has_marketing_consent: false,
});
});
});
Expand Down Expand Up @@ -106,8 +104,6 @@ describe('OptinMetrics', () => {
expect(mockMetrics.addTraitsToUser).toHaveBeenNthCalledWith(1, {
deviceProp: 'Device value',
userProp: 'User value',
is_metrics_opted_in: true,
has_marketing_consent: true,
});
});
});
Expand Down
21 changes: 13 additions & 8 deletions app/components/Views/ExperienceEnhancerModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import {
} from '../../../components/hooks/useMetrics';
import { HOW_TO_MANAGE_METRAMETRICS_SETTINGS } from '../../../constants/urls';
import { ExperienceEnhancerBottomSheetSelectorsIDs } from '../../../../e2e/selectors/Onboarding/ExperienceEnhancerModal.selectors.js';
import {
UserProfileProperty
} from '../../../util/metrics/UserSettingsAnalyticsMetaData/UserProfileAnalyticsMetaData.types';

const ExperienceEnhancerModal = () => {
const dispatch = useDispatch();
Expand All @@ -40,14 +43,14 @@ const ExperienceEnhancerModal = () => {
dispatch(setDataCollectionForMarketing(false));
bottomSheetRef.current?.onCloseBottomSheet();

const traits = {
has_marketing_consent: false,
};
addTraitsToUser(traits);
addTraitsToUser({
[UserProfileProperty.HAS_MARKETING_CONSENT]: UserProfileProperty.OFF,
});
trackEvent(
createEventBuilder(MetaMetricsEvents.ANALYTICS_PREFERENCE_SELECTED)
.addProperties({
...traits,
[UserProfileProperty.HAS_MARKETING_CONSENT]: false,
updated_after_onboarding: true,
location: 'marketing_consent_modal',
})
.build(),
Expand All @@ -64,12 +67,14 @@ const ExperienceEnhancerModal = () => {
dispatch(setDataCollectionForMarketing(true));
bottomSheetRef.current?.onCloseBottomSheet();

const traits = { has_marketing_consent: true };
addTraitsToUser(traits);
addTraitsToUser({
[UserProfileProperty.HAS_MARKETING_CONSENT]: UserProfileProperty.ON,
});
trackEvent(
createEventBuilder(MetaMetricsEvents.ANALYTICS_PREFERENCE_SELECTED)
.addProperties({
...traits,
[UserProfileProperty.HAS_MARKETING_CONSENT]: true,
updated_after_onboarding: true,
location: 'marketing_consent_modal',
})
.build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ describe('MetaMetricsAndDataCollectionSection', () => {
expect(mockMetrics.addTraitsToUser).toHaveBeenCalledWith({
deviceProp: 'Device value',
userProp: 'User value',
is_metrics_opted_in: true,
});
expect(mockMetrics.trackEvent).toHaveBeenCalledWith(
MetricsEventBuilder.createEventBuilder(
Expand All @@ -219,6 +218,7 @@ describe('MetaMetricsAndDataCollectionSection', () => {
.addProperties({
is_metrics_opted_in: true,
updated_after_onboarding: true,
location: 'settings',
})
.build(),
);
Expand Down Expand Up @@ -299,7 +299,6 @@ describe('MetaMetricsAndDataCollectionSection', () => {
expect(mockMetrics.addTraitsToUser).toHaveBeenNthCalledWith(1, {
deviceProp: 'Device value',
userProp: 'User value',
is_metrics_opted_in: true,
});
expect(mockMetrics.trackEvent).toHaveBeenNthCalledWith(
1,
Expand All @@ -308,6 +307,7 @@ describe('MetaMetricsAndDataCollectionSection', () => {
)
.addProperties({
is_metrics_opted_in: true,
location: 'settings',
updated_after_onboarding: true,
})
.build(),
Expand All @@ -318,7 +318,7 @@ describe('MetaMetricsAndDataCollectionSection', () => {
// if MetaMetrics is initially disabled, addTraitsToUser is called twice and this is 2nd call
!metaMetricsInitiallyEnabled ? 2 : 1,
{
has_marketing_consent: true,
has_marketing_consent: 'ON',
},
);
expect(mockMetrics.trackEvent).toHaveBeenNthCalledWith(
Expand All @@ -330,6 +330,7 @@ describe('MetaMetricsAndDataCollectionSection', () => {
.addProperties({
has_marketing_consent: true,
location: 'settings',
updated_after_onboarding: true,
})
.build(),
);
Expand Down Expand Up @@ -376,7 +377,7 @@ describe('MetaMetricsAndDataCollectionSection', () => {
expect(mockAlert).not.toHaveBeenCalled();
expect(mockMetrics.addTraitsToUser).toHaveBeenCalledTimes(1);
expect(mockMetrics.addTraitsToUser).toHaveBeenCalledWith({
has_marketing_consent: false,
has_marketing_consent: 'OFF',
});
expect(mockMetrics.trackEvent).toHaveBeenCalledTimes(1);
expect(mockMetrics.trackEvent).toHaveBeenCalledWith(
Expand All @@ -386,6 +387,7 @@ describe('MetaMetricsAndDataCollectionSection', () => {
.addProperties({
has_marketing_consent: false,
location: 'settings',
updated_after_onboarding: true,
})
.build(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import Routes from '../../../../../../constants/navigation/Routes';
import { useMetrics } from '../../../../../hooks/useMetrics';
import { useDispatch, useSelector } from 'react-redux';
import { useNavigation } from '@react-navigation/native';
import {
UserProfileProperty
} from '../../../../../../util/metrics/UserSettingsAnalyticsMetaData/UserProfileAnalyticsMetaData.types';

const MetaMetricsAndDataCollectionSection: React.FC = () => {
const theme = useTheme();
Expand All @@ -51,7 +54,6 @@ const MetaMetricsAndDataCollectionSection: React.FC = () => {
const consolidatedTraits = {
...generateDeviceAnalyticsMetaData(),
...generateUserSettingsAnalyticsMetaData(),
is_metrics_opted_in: true,
};
await enable();
setAnalyticsEnabled(true);
Expand All @@ -63,6 +65,7 @@ const MetaMetricsAndDataCollectionSection: React.FC = () => {
.addProperties({
is_metrics_opted_in: true,
updated_after_onboarding: true,
location: 'settings',
})
.build(),
);
Expand All @@ -82,14 +85,14 @@ const MetaMetricsAndDataCollectionSection: React.FC = () => {

const addMarketingConsentToTraits = (marketingOptIn: boolean) => {
InteractionManager.runAfterInteractions(async () => {
const traits = {
has_marketing_consent: marketingOptIn,
};
await addTraitsToUser(traits);
await addTraitsToUser({
[UserProfileProperty.HAS_MARKETING_CONSENT]: marketingOptIn ? UserProfileProperty.ON : UserProfileProperty.OFF,
});
trackEvent(
createEventBuilder(MetaMetricsEvents.ANALYTICS_PREFERENCE_SELECTED)
.addProperties({
...traits,
[UserProfileProperty.HAS_MARKETING_CONSENT]: marketingOptIn,
updated_after_onboarding: true,
location: 'settings',
})
.build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export enum UserProfileProperty {
SECURITY_PROVIDERS = 'security_providers',
PRIMARY_CURRENCY = 'primary_currency',
CURRENT_CURRENCY = 'current_currency',
HAS_MARKETING_CONSENT = 'has_marketing_consent',
}

export interface UserProfileMetaData {
Expand All @@ -22,4 +23,5 @@ export interface UserProfileMetaData {
[UserProfileProperty.SECURITY_PROVIDERS]: string;
[UserProfileProperty.PRIMARY_CURRENCY]?: string;
[UserProfileProperty.CURRENT_CURRENCY]?: string;
[UserProfileProperty.HAS_MARKETING_CONSENT]: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import generateUserProfileAnalyticsMetaData from './generateUserProfileAnalyticsMetaData';
import { UserProfileProperty } from './UserProfileAnalyticsMetaData.types';
import {Appearance} from 'react-native';

const mockGetState = jest.fn();
jest.mock('../../../store', () => ({
store: {
getState: jest.fn(() => mockGetState()),
},
}));

const mockIsMetricsEnabled = jest.fn();
jest.mock('../../../core/Analytics', () => ({
MetaMetrics: {
getInstance: jest.fn(() => ({ isEnabled: mockIsMetricsEnabled })),
},
}));

jest.spyOn(Appearance, 'getColorScheme').mockReturnValue('dark');

describe('generateUserProfileAnalyticsMetaData', () => {
const mockState = {
engine: {
backgroundState: {
PreferencesController: {
displayNftMedia: true,
useNftDetection: false,
useTokenDetection: true,
isMultiAccountBalancesEnabled: false,
securityAlertsEnabled: true,
},
},
},
user: { appTheme: 'os' },
security: { dataCollectionForMarketing: true },
};

it('returns metadata', () => {
mockGetState.mockReturnValue(mockState);
mockIsMetricsEnabled.mockReturnValue(true);

const metadata = generateUserProfileAnalyticsMetaData();
expect(metadata).toEqual({
[UserProfileProperty.ENABLE_OPENSEA_API]: UserProfileProperty.ON,
[UserProfileProperty.NFT_AUTODETECTION]: UserProfileProperty.OFF,
[UserProfileProperty.THEME]: 'dark',
[UserProfileProperty.TOKEN_DETECTION]: UserProfileProperty.ON,
[UserProfileProperty.MULTI_ACCOUNT_BALANCE]: UserProfileProperty.OFF,
[UserProfileProperty.SECURITY_PROVIDERS]: 'blockaid',
[UserProfileProperty.HAS_MARKETING_CONSENT]: UserProfileProperty.ON,
});
});

it.each([
[UserProfileProperty.ON, true],
[UserProfileProperty.OFF, false],
])('returns marketing consent "%s"', (expected, stateConsentValue) => {
mockGetState.mockReturnValue({ ...mockState, security: { dataCollectionForMarketing: stateConsentValue } });

const metadata = generateUserProfileAnalyticsMetaData();
expect(metadata[UserProfileProperty.HAS_MARKETING_CONSENT]).toEqual(expected);
});

it('returns default metadata when missing preferences controller', () => {
mockGetState.mockReturnValue({ ...mockState, engine: {} });

const metadata = generateUserProfileAnalyticsMetaData();
expect(metadata).toMatchObject({
[UserProfileProperty.ENABLE_OPENSEA_API]: UserProfileProperty.OFF,
[UserProfileProperty.NFT_AUTODETECTION]: UserProfileProperty.OFF,
[UserProfileProperty.TOKEN_DETECTION]: UserProfileProperty.OFF,
[UserProfileProperty.MULTI_ACCOUNT_BALANCE]: UserProfileProperty.OFF,
[UserProfileProperty.SECURITY_PROVIDERS]: '',
});
});

it('returns user preference for theme', () => {
mockGetState.mockReturnValue({ ...mockState, user: { appTheme: 'light' } });

const metadata = generateUserProfileAnalyticsMetaData();
expect(metadata[UserProfileProperty.THEME]).toBe('light');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const generateUserProfileAnalyticsMetaData = (): UserProfileMetaData => {
// This will return either "light" or "dark"
const appThemeStyle =
appTheme === 'os' ? Appearance.getColorScheme() : appTheme;
const isDataCollectionForMarketingEnabled =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function used in any place outside of components? If not, we should use a hook to gather redux state data instead of accessing the redux store directly.

reduxState?.security?.dataCollectionForMarketing;

return {
[UserProfileProperty.ENABLE_OPENSEA_API]:
Expand All @@ -38,6 +40,9 @@ const generateUserProfileAnalyticsMetaData = (): UserProfileMetaData => {
: UserProfileProperty.OFF,
[UserProfileProperty.SECURITY_PROVIDERS]:
preferencesController?.securityAlertsEnabled ? 'blockaid' : '',
[UserProfileProperty.HAS_MARKETING_CONSENT]: isDataCollectionForMarketingEnabled
? UserProfileProperty.ON
: UserProfileProperty.OFF,
};
};

Expand Down
Loading