Skip to content

Commit

Permalink
feat(3299): add tracking to network switching and confirmation (#11386)
Browse files Browse the repository at this point in the history
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**
Adds tracking to the user requesting network switching as well as his
press of the confirmation button.
<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

## **Related issues**

Fixes: MetaMask/MetaMask-planning#3299

## **Manual testing steps**

1. Go to the in app browser and open have a connected dapp for example
to ethereum main net
2. In the dapp select to change network, the modal will come up, thats
when the tracking event if fire for user requesting a network change
3. Confirm the network change by pressing the button, this corresponds
to the confirm pressed tracked event

## **Screenshots/Recordings**


<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

#### Mixpanel logs for the two events


<img width="1024" alt="Mixpanel logs for the two events"
src="https://github.com/user-attachments/assets/6ab51bd5-42a3-4564-94e3-6c8f76999cb8">

#### Network Switch Requested and Modal Shown
| Interaction     | Tracked event        |
|--------------|--------------|
| <img width="350" alt="Screenshot 2024-04-18 at 3 56 43 PM"
src="https://github.com/user-attachments/assets/f627e1a2-7503-4f56-ae36-6702077ee7d9">
|<img width="350" alt="Screenshot 2024-04-18 at 3 56 43 PM"
src="https://github.com/user-attachments/assets/d631bccd-f600-439b-9341-fe76ebbba2a1">
|

#### Network Switch Confirm Pressed
| **Interaction**       | **Tracked event**        |
|--------------|--------------|
| <img width="350" alt="Screenshot 2024-04-18 at 3 56 43 PM"
src="https://github.com/user-attachments/assets/4b6508d0-c1e3-4dd9-8210-5e63d75b65c0">
|<img width="350" alt="Screenshot 2024-04-18 at 3 56 43 PM"
src="https://github.com/user-attachments/assets/ecfc708c-b7e2-4530-9fe7-11740c293b12">
|




<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I’ve followed [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).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] 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)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
  • Loading branch information
EtherWizard33 authored Sep 25, 2024
1 parent 36eade5 commit a616742
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
32 changes: 30 additions & 2 deletions app/components/UI/SwitchCustomNetwork/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useMemo } from 'react';
import PropTypes from 'prop-types';
import StyledButton from '../StyledButton';
import { StyleSheet, View } from 'react-native';
Expand All @@ -8,8 +8,14 @@ import Device from '../../../util/device';
import Text from '../../Base/Text';
import { useTheme } from '../../../util/theme';
import { CommonSelectorsIDs } from '../../../../e2e/selectors/Common.selectors';
import { isMultichainVersion1Enabled } from '../../../util/networks';
import {
isMultichainVersion1Enabled,
getDecimalChainId,
} from '../../../util/networks';
import PermissionSummary from '../PermissionsSummary';
import { MetaMetricsEvents } from '../../../core/Analytics';
import { useNetworkInfo } from '../../../selectors/selectedNetworkController';
import { useMetrics } from '../../../components/hooks/useMetrics';

const createStyles = (colors) =>
StyleSheet.create({
Expand Down Expand Up @@ -90,11 +96,33 @@ const SwitchCustomNetwork = ({
}) => {
const { colors } = useTheme();
const styles = createStyles(colors);
const { trackEvent } = useMetrics();

const { networkName } = useNetworkInfo(
new URL(currentPageInformation.url).hostname,
);

const trackingData = useMemo(
() => ({
chain_id: getDecimalChainId(customNetworkInformation.chainId),
from_network: networkName,
to_network: customNetworkInformation.chainName,
}),
[customNetworkInformation, networkName],
);

useEffect(() => {
trackEvent(
MetaMetricsEvents.NETWORK_SWITCH_REQUESTED_AND_MODAL_SHOWN,
trackingData,
);
}, [trackEvent, trackingData]);

/**
* Calls onConfirm callback and analytics to track connect confirmed event
*/
const confirm = () => {
trackEvent(MetaMetricsEvents.NETWORK_SWITCH_CONFIRM_PRESSED, trackingData);
onConfirm && onConfirm();
};

Expand Down
16 changes: 16 additions & 0 deletions app/components/UI/SwitchCustomNetwork/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import React from 'react';
import SwitchCustomNetwork from './';
import renderWithProvider from '../../../util/test/renderWithProvider';
import { backgroundState } from '../../../util/test/initial-root-state';
import { MOCK_ACCOUNTS_CONTROLLER_STATE } from '../../../util/test/accountsControllerTestUtils';

const mockInitialState = {
wizard: {
step: 1,
},
engine: {
backgroundState: {
...backgroundState,
AccountsController: MOCK_ACCOUNTS_CONTROLLER_STATE,
},
},
};

describe('SwitchCustomNetwork', () => {
it('should render correctly', () => {
const { toJSON } = renderWithProvider(
<SwitchCustomNetwork
customNetworkInformation={{ chainName: '', chainId: '' }}
currentPageInformation={{ url: 'https://app.uniswap.org/' }}
/>,
{ state: mockInitialState },
);
expect(toJSON()).toMatchSnapshot();
});
Expand Down
8 changes: 8 additions & 0 deletions app/core/Analytics/MetaMetrics.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ enum EVENT_NAME {

// Network
NETWORK_SWITCHED = 'Network Switched',
NETWORK_SWITCH_REQUESTED_AND_MODAL_SHOWN = 'Network Switch Requested and Modal Shown',
NETWORK_SWITCH_CONFIRM_PRESSED = 'Network Switch Confirm Pressed',
NETWORK_ADDED = 'Network Added',
NETWORK_REQUESTED = 'Network Requested',
NETWORK_REQUEST_REJECTED = 'Network Request Rejected',
Expand Down Expand Up @@ -482,6 +484,12 @@ const events = {
COLLECTIBLE_REMOVED: generateOpt(EVENT_NAME.COLLECTIBLE_REMOVED),
CURRENCY_CHANGED: generateOpt(EVENT_NAME.CURRENCY_CHANGED),
NETWORK_SWITCHED: generateOpt(EVENT_NAME.NETWORK_SWITCHED),
NETWORK_SWITCH_REQUESTED_AND_MODAL_SHOWN: generateOpt(
EVENT_NAME.NETWORK_SWITCH_REQUESTED_AND_MODAL_SHOWN,
),
NETWORK_SWITCH_CONFIRM_PRESSED: generateOpt(
EVENT_NAME.NETWORK_SWITCH_CONFIRM_PRESSED,
),
NETWORK_ADDED: generateOpt(EVENT_NAME.NETWORK_ADDED),
NETWORK_REQUESTED: generateOpt(EVENT_NAME.NETWORK_REQUESTED),
NETWORK_REQUEST_REJECTED: generateOpt(EVENT_NAME.NETWORK_REQUEST_REJECTED),
Expand Down

0 comments on commit a616742

Please sign in to comment.