Skip to content

Commit b9d8a78

Browse files
refactor(analytics): PR E1 rename addTraitsToUser to identify in mobile-core-ux files (MetaMask#30473)
## **Description** Part of the analytics cleanup workstream (MetaMask#26686). Renames `.addTraitsToUser()` → `.identify()` across all mobile-core-ux-owned Settings components and network hooks, and migrates `AccountActions.tsx` from the deprecated `useMetrics` to `useAnalytics`. Also removes stale `MetaMetrics`/`MetaMetrics.events` mocks from `AccountsMenu.test.tsx`. Files touched: - `BlockaidSettings.tsx`, `DisplayNFTMediaSettings`, `BatchAccountBalanceSettings`, `AutoDetectTokensSettings`, `AutoDetectNFTSettings` — rename `.addTraitsToUser` → `.identify` - `useNetworkOperations.ts`, `useAddPopularNetwork.ts`, `NetworkManager/index.tsx` — rename `.addTraitsToUser` → `.identify` - `AccountActions.tsx` — migrate `useMetrics` → `useAnalytics` - Corresponding test files updated to use `identify` override in `createMockUseAnalyticsHook` - `AccountsMenu.test.tsx` — remove stale `jest.mock('../../../core/Analytics', ...)` and `jest.mock('../../../core/Analytics/MetaMetrics.events', ...)` ## **Changelog** CHANGELOG entry: null ## **Related issues** Closes: MetaMask#26821 Refs: MetaMask#26686 ## **Manual testing steps** N/A ## **Screenshots/Recordings** ### **Before** N/A — no UI changes. ### **After** N/A — no UI changes. ## **Pre-merge author checklist** - [x] I've followed the [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md) - [x] I've completed the PR template to the best of my ability - [x] I've included tests if applicable - [x] I've documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I've applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)) ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, test affected areas) - [ ] I confirm that this PR addresses what is claimed in the PR title - [ ] I confirm that I've manually reviewed the changes if not manually tested Made with [Cursor](https://cursor.com) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Low risk refactor limited to analytics hook usage and test mocks, with minimal runtime behavior change beyond calling the new `identify` alias. > > **Overview** > Updates mobile-core-ux components and network-related hooks to use `useAnalytics().identify` instead of the deprecated `addTraitsToUser` for updating analytics user traits (settings toggles and network add/remove flows). > > Migrates `AccountActions` off `useMetrics` to `useAnalytics`, and cleans up related unit tests by updating mocked hook return values and removing stale `MetaMetrics`/`MetaMetrics.events` mocks in `AccountsMenu.test.tsx`. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 367f37e. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 1635a36 commit b9d8a78

16 files changed

Lines changed: 32 additions & 55 deletions

File tree

app/components/UI/NetworkManager/index.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ jest.mock('../../hooks/useAnalytics/useAnalytics', () => ({
122122
useAnalytics: () => ({
123123
trackEvent: mockTrackEvent,
124124
createEventBuilder: mockCreateEventBuilder,
125-
addTraitsToUser: mockAddTraitsToUser,
125+
identify: mockAddTraitsToUser,
126126
}),
127127
}));
128128

app/components/UI/NetworkManager/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const NetworkManager = () => {
8383
const navigation = useNavigation();
8484
const { colors } = useTheme();
8585
const { styles } = useStyles(createStyles, { colors });
86-
const { trackEvent, createEventBuilder, addTraitsToUser } = useAnalytics();
86+
const { trackEvent, createEventBuilder, identify } = useAnalytics();
8787
const { disableNetwork, enabledNetworksByNamespace } = useNetworkEnablement();
8888

8989
const enabledNetworks = useMemo(() => {
@@ -296,11 +296,11 @@ const NetworkManager = () => {
296296
NetworkController.removeNetwork(chainId);
297297
disableNetwork(showConfirmDeleteModal.caipChainId);
298298

299-
addTraitsToUser(removeItemFromChainIdList(chainId));
299+
identify(removeItemFromChainIdList(chainId));
300300

301301
setShowConfirmDeleteModal(initialShowConfirmDeleteModal);
302302
}
303-
}, [showConfirmDeleteModal, disableNetwork, addTraitsToUser]);
303+
}, [showConfirmDeleteModal, disableNetwork, identify]);
304304

305305
const cancelButtonProps: ButtonProps = useMemo(
306306
() => ({

app/components/Views/AccountActions/AccountActions.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import Logger from '../../../util/Logger';
2727
import { protectWalletModalVisible } from '../../../actions/user';
2828
import Routes from '../../../constants/navigation/Routes';
2929
import { AccountActionsBottomSheetSelectorsIDs } from './AccountActionsBottomSheet.testIds';
30-
import { useMetrics } from '../../../components/hooks/useMetrics';
30+
import { useAnalytics } from '../../../components/hooks/useAnalytics/useAnalytics';
3131
import {
3232
isHardwareAccount,
3333
isHDOrFirstPartySnapAccount,
@@ -64,7 +64,7 @@ const AccountActions = () => {
6464
const sheetRef = useRef<BottomSheetRef>(null);
6565
const { navigate } = useNavigation();
6666
const dispatch = useDispatch();
67-
const { trackEvent, createEventBuilder } = useMetrics();
67+
const { trackEvent, createEventBuilder } = useAnalytics();
6868
const { networkSupporting7702Present } = useEIP7702Networks(
6969
selectedAccount.address,
7070
);

app/components/Views/AccountsMenu/AccountsMenu.test.tsx

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,6 @@ jest.mock('../../hooks/useAnalytics/useAnalytics', () => ({
5050
}),
5151
}));
5252

53-
jest.mock('../../../core/Analytics', () => ({
54-
MetaMetrics: {
55-
getInstance: () => ({
56-
trackEvent: mockTrackEvent,
57-
}),
58-
},
59-
}));
60-
61-
jest.mock('../../../core/Analytics/MetaMetrics.events', () => ({
62-
EVENT_NAME: {
63-
CARD_HOME_CLICKED: 'Card Home Clicked',
64-
SETTINGS_VIEWED: 'Settings Viewed',
65-
SETTINGS_ABOUT: 'About MetaMask',
66-
NAVIGATION_TAPS_SEND_FEEDBACK: 'Send Feedback',
67-
NAVIGATION_TAPS_GET_HELP: 'Get Help',
68-
NAVIGATION_TAPS_LOGOUT: 'Logout',
69-
QR_SCANNER_OPENED: 'QR Scanner Opened',
70-
RAMPS_BUTTON_CLICKED: 'Ramps Button Clicked',
71-
NOTIFICATIONS_MENU_OPENED: 'Notifications Menu Opened',
72-
NOTIFICATIONS_ACTIVATED: 'Notifications Activated',
73-
},
74-
}));
75-
7653
jest.mock('../../../core/Analytics/MetricsEventBuilder', () => ({
7754
MetricsEventBuilder: {
7855
createEventBuilder: jest.fn(() => ({

app/components/Views/NetworksManagement/NetworkDetailsView/hooks/useNetworkOperations.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const mockCreateEventBuilder = jest.fn(() => ({
8484
jest.mock('../../../../hooks/useAnalytics/useAnalytics', () => ({
8585
useAnalytics: () => ({
8686
trackEvent: mockTrackEvent,
87-
addTraitsToUser: mockAddTraitsToUser,
87+
identify: mockAddTraitsToUser,
8888
createEventBuilder: mockCreateEventBuilder,
8989
}),
9090
}));

app/components/Views/NetworksManagement/NetworkDetailsView/hooks/useNetworkOperations.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export const useNetworkOperations = (): UseNetworkOperationsReturn => {
8484
const providerConfig = useSelector(selectProviderConfig);
8585
const isAllNetworks = useSelector(selectIsAllNetworks);
8686
const tokenNetworkFilter = useSelector(selectTokenNetworkFilter);
87-
const { trackEvent, addTraitsToUser, createEventBuilder } = useAnalytics();
87+
const { trackEvent, identify, createEventBuilder } = useAnalytics();
8888

8989
// ---- Handle network add/update ------------------------------------------
9090
const handleNetworkUpdate = useCallback(
@@ -204,7 +204,7 @@ export const useNetworkOperations = (): UseNetworkOperationsReturn => {
204204
await NetworkController.addNetwork({
205205
...networkConfig,
206206
} as unknown as AddNetworkFields);
207-
addTraitsToUser(addItemToChainIdList(networkConfig.chainId));
207+
identify(addItemToChainIdList(networkConfig.chainId));
208208
}
209209

210210
if (!skipPostSaveNavigation) {
@@ -221,7 +221,7 @@ export const useNetworkOperations = (): UseNetworkOperationsReturn => {
221221
navigation,
222222
networkConfigurations,
223223
trackEvent,
224-
addTraitsToUser,
224+
identify,
225225
createEventBuilder,
226226
],
227227
);
@@ -396,11 +396,11 @@ export const useNetworkOperations = (): UseNetworkOperationsReturn => {
396396
const { NetworkController } = Engine.context;
397397
NetworkController.removeNetwork(hexChainId);
398398

399-
addTraitsToUser(removeItemFromChainIdList(hexChainId));
399+
identify(removeItemFromChainIdList(hexChainId));
400400

401401
navigation.goBack();
402402
},
403-
[navigation, networkConfigurations, providerConfig, addTraitsToUser],
403+
[navigation, networkConfigurations, providerConfig, identify],
404404
);
405405

406406
// ---- Navigate to edit ---------------------------------------------------

app/components/Views/Settings/AutoDetectNFTSettings/index.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('AutoDetectNFTSettings', () => {
5959
jest.mocked(useAnalytics).mockReturnValue(
6060
createMockUseAnalyticsHook({
6161
trackEvent: mockTrackEvent,
62-
addTraitsToUser: mockAddTraitsToUser,
62+
identify: mockAddTraitsToUser,
6363
}),
6464
);
6565
(useNavigation as jest.Mock).mockImplementation(() => mockNavigation);

app/components/Views/Settings/AutoDetectNFTSettings/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import createStyles from './index.styles';
2323
import { NFT_AUTO_DETECT_MODE_SECTION } from './index.constants';
2424

2525
const AutoDetectNFTSettings = () => {
26-
const { trackEvent, addTraitsToUser, createEventBuilder } = useAnalytics();
26+
const { trackEvent, identify, createEventBuilder } = useAnalytics();
2727
const theme = useTheme();
2828
const { colors } = theme;
2929
const styles = createStyles();
@@ -38,7 +38,7 @@ const AutoDetectNFTSettings = () => {
3838
}
3939
PreferencesController.setUseNftDetection(value);
4040

41-
addTraitsToUser({
41+
identify({
4242
...(value && {
4343
[UserProfileProperty.ENABLE_OPENSEA_API]: value
4444
? UserProfileProperty.ON
@@ -57,7 +57,7 @@ const AutoDetectNFTSettings = () => {
5757
.build(),
5858
);
5959
},
60-
[addTraitsToUser, trackEvent, createEventBuilder],
60+
[identify, trackEvent, createEventBuilder],
6161
);
6262

6363
return (

app/components/Views/Settings/AutoDetectTokensSettings/index.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('AssetSettings', () => {
4444
jest
4545
.mocked(useAnalytics)
4646
.mockReturnValue(
47-
createMockUseAnalyticsHook({ addTraitsToUser: mockAddTraitsToUser }),
47+
createMockUseAnalyticsHook({ identify: mockAddTraitsToUser }),
4848
);
4949
});
5050

app/components/Views/Settings/AutoDetectTokensSettings/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ const AutoDetectTokensSettings = () => {
2626
const theme = useTheme();
2727
const { colors } = theme;
2828
const { styles } = useStyles(styleSheet, {});
29-
const { addTraitsToUser } = useAnalytics();
29+
const { identify } = useAnalytics();
3030

3131
const isTokenDetectionEnabled = useSelector(selectUseTokenDetection);
3232

3333
const toggleTokenDetection = useCallback(
3434
(value: boolean) => {
3535
Engine.context.PreferencesController.setUseTokenDetection(value);
36-
addTraitsToUser({
36+
identify({
3737
[UserProfileProperty.TOKEN_DETECTION]: value
3838
? UserProfileProperty.ON
3939
: UserProfileProperty.OFF,
4040
});
4141
},
42-
[addTraitsToUser],
42+
[identify],
4343
);
4444

4545
return (

0 commit comments

Comments
 (0)