Skip to content

feat: migrate all tracking calls to Segment#8387

Closed
NicolasMassart wants to merge 41 commits into
mainfrom
1461_migrate_all_track_events
Closed

feat: migrate all tracking calls to Segment#8387
NicolasMassart wants to merge 41 commits into
mainfrom
1461_migrate_all_track_events

Conversation

@NicolasMassart

@NicolasMassart NicolasMassart commented Jan 24, 2024

Copy link
Copy Markdown
Contributor

Description

Important

This PR will be split into smaller PRs. No way I ask you to review this many files at once.

This PR is migrating all analytics events tracking calls to new Segment MetaMetrics.

There's 3 cases:

  • File is a class component
  • File is a functional component
  • File is not a component

Common changes are:

  • remove InteractionManager.runAfterInteractions as it's handled in the hook now.
  • call trackAnonymousEvent where it was previously done by two calls
  • remove unused Analytics and trackEventV2 imports
  • update tests if needed

File is a class component

  • add prop
  • add withMetricsAwareness HOC
  • destructure this.props
import withMetricsAwareness from '../../hooks/useMetrics/withMetricsAwareness';
...
/**
 * Metrics injected by withMetricsAwareness HOC
 */
metrics: PropTypes.object,
...
const { metrics } = this.props;
metrics.trackEvent(
  MetaMetricsEvents.APPROVAL_STARTED,
  this.getAnalyticsParams(),
);

File is a functional component

  • import hook
  • destructure it in constants for each useful function (at least trackEvent usually)
  • add dependency to hooks (useEffect, useCallback, ...)
import { useMetrics } from '../../../../components/hooks/useMetrics';
...
const { trackEvent } = useMetrics();
...
trackEvent(MetaMetricsEvents.ACTIONS_BUTTON_CLICKED, {
  text: '',
  chain_id: chainId,
});

File is utils

  • use the class directly as you can't use hooks
  • make sure to pass a string for event name (add .category)
import { MetaMetrics, MetaMetricsEvents } from '../../core/Analytics';
...
MetaMetrics.getInstance().trackEvent(
  confirmation
    ? MetaMetricsEvents.SIGNATURE_APPROVED.category
    : MetaMetricsEvents.SIGNATURE_REJECTED.category,
  getAnalyticsParams(messageParams, signType, securityAlertResponse),
);

Related issues

Fixes: https://github.com/MetaMask/mobile-planning/issues/1461

Manual testing steps

  1. test all features that can send an event and check they are actually sent and received on Segment debugger

Screenshots/Recordings

Before

No change with previous app behaviour

After

No change with previous app behaviour

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • I've clearly explained what problem this PR is solving and how it is solved.
  • I've linked related issues
  • I've included manual testing steps
  • I've included screenshots/recordings if applicable
  • I’ve included tests if applicable
  • I’ve documented my code using JSDoc format if applicable
  • I’ve applied the right labels on the PR (see labeling guidelines). Not required for external contributors.
  • I’ve properly set the pull request status:
    • In case it's not yet "ready for review", I've set it to "draft".
    • In case it's "ready for review", I've changed it from "draft" to "non-draft".

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.

NicolasMassart and others added 22 commits December 6, 2023 11:14
…proxy (Batch 2 of 4) (#7520)

# What is the reason for the change?
- Implement a new metrics system for the Segment SDK.
- Make it work alongside the old one so that we can remove the old one
progressively.

# What is the improvement/solution?
- Create a new class to handle metrics with segment
- abstract all segment SDK calls
- provide our own user and anonymous Ids for improved privacy
- Reuse the Mixpanel user id it available, otherwise generates a new one
with the same format
- patch Segment SDK to allow the privacy aware user and anonymous Ids
- No event sent at all if user haven't accepted to send metrics info
- Total reset of the infos in case user asks to reset (in settings)
- Ability to disable all metrics
- Ability to ask for deletion of all data

Fixes MetaMask/mobile-planning#1222
…4) (#7990)

- Initialise MetaMetrics on app start without removing Analytics yet as
we still uses it outside of onboarding
- create a `trackAfterInteractions` utility function to reduce code when
tracking events.
- Replace calls to Analytics with calls to `trackAfterInteractions` in
all components involved in onboarding screens (see
[`OnboardingNav`](https://github.com/MetaMask/metamask-mobile/blob/feat%2F1275_segment_onboarding_impl/app/components/Nav/App/index.js#L122-L175)
in `app/components/Nav/App/index.js`)
- Fix onboarding events queue usage in
`app/components/UI/OptinMetrics/index.js`
  - Update Opt-on/Opt-out in onboarding
- Extract Analytics User profil properties in UserProfileMetaData util
and inject it in MetaMetrics identify request alongside device metadata

Fixes MetaMask/mobile-planning#1275
- rename Segment env vars:
  - `SEGMENT_WRITE_KEY`
  - `SEGMENT_PROXY_URL`
- add env var placeholders in example file

Fixes missing variables naming change in
MetaMask/mobile-planning#1222
# Conflicts:
#	app/components/Views/AccountBackupStep1/index.js
#	app/components/Views/AccountBackupStep1B/index.js
#	app/components/Views/ChoosePassword/index.js
#	app/components/Views/ManualBackupStep1/index.js
#	app/components/Views/ManualBackupStep2/index.js
#	app/components/Views/ManualBackupStep3/index.js
# Conflicts:
#	app/components/Views/Settings/SecuritySettings/SecuritySettings.tsx
- adds `createDataDeletionTask` to `MetaMetrics` to call the deletion
API in MetaMetrics class for method [create source
regulation](https://docs.segmentapis.com/tag/Deletion-and-Suppression#operation/createSourceRegulation)
- adds the `checkDataDeletionTaskStatus` to `MetaMetrics` to call
deletion API [get regulation
method](https://docs.segmentapis.com/tag/Deletion-and-Suppression#operation/getRegulation)
- calls the `createDataDeletionTask` and `checkDataDeletionTaskStatus`
from `DeleteMetaMetricsData.tsx` when user touches the delete button.
- extract the settings section logic to display allow deletion to a well
tested hook.
- Remove `Analytics.js` implementation of these features and point only
to the new `MetaMetrics` ones.
- update unit tests
- rename in `storage.js`:
- `METAMETRICS_SEGMENT_REGULATION_ID` ->
`METAMETRICS_DELETION_REGULATION_ID`

Fixes MetaMask/mobile-planning#1403
# Conflicts:
#	ios/Podfile.lock
-adds error handling on async `DefaultPreference.get` and
`DefaultPreference.set`
- makes building instance stronger by using the Config type to prevent
config mistakes

completes MetaMask/mobile-planning#1403
@NicolasMassart NicolasMassart self-assigned this Jan 24, 2024
@github-actions

Copy link
Copy Markdown
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@gauthierpetetin gauthierpetetin added the team-mobile-platform Mobile Platform team label Jan 30, 2024
NicolasMassart and others added 2 commits February 2, 2024 12:32
- make MetaMetrics.getInstance synchronous and add an async init
function + readiness check
- destructurable object hook return value to use metrics in components
without having to use useRef and useEffect to set the instance
- interface for the destructurable object
- HOC (High Order Component) wrapper for class components
- new and updated unit tests
@NicolasMassart NicolasMassart force-pushed the 1461_migrate_all_track_events branch from ef6aa36 to 99194b0 Compare February 2, 2024 11:38
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 2, 2024
@NicolasMassart NicolasMassart reopened this Feb 2, 2024
@NicolasMassart NicolasMassart added the DO-NOT-MERGE Pull requests that should not be merged label Feb 16, 2024
Base automatically changed from feat/batch_1129_segment to main February 20, 2024 14:36
@NicolasMassart

Copy link
Copy Markdown
Contributor Author

Closed as this was reworked and split differently in smaller PRs for the migration

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

blocked DO-NOT-MERGE Pull requests that should not be merged team-mobile-platform Mobile Platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants