Skip to content

feat: create new useAnalytics hook to replace useMetrics#25045

Merged
NicolasMassart merged 13 commits intomainfrom
feat/new_analytics_hook
Jan 23, 2026
Merged

feat: create new useAnalytics hook to replace useMetrics#25045
NicolasMassart merged 13 commits intomainfrom
feat/new_analytics_hook

Conversation

@NicolasMassart
Copy link
Copy Markdown
Contributor

@NicolasMassart NicolasMassart commented Jan 22, 2026

Description

This PR creates the useAnalytics hook to exclusively use the new analytics types, targetting future full removal of all legacy type support. This is part of the ongoing migration away from MetaMetrics internals to the new analytics system.

Changes:

  • Created new useAnalytics hook that exclusively uses new analytics types (AnalyticsTrackingEvent and AnalyticsUserTraits)
  • Created withAnalyticsAwareness HOC that injects the useAnalytics hook into component props
  • Added deprecation notices to useMetrics and withMetricsAwareness pointing to new implementations
  • Added global test mocks for useAnalytics and withAnalyticsAwareness in test setup

Reason for change:
This simplifies the hook API and forces migration to the new analytics types, reducing technical debt and ensuring all consumers use the modern analytics system.

Improvement:

  • Cleaner, more maintainable API with only new types
  • Better type safety without legacy type unions
  • Consistent naming with the rest of the analytics system
  • Forces migration to new types, preventing new code from using deprecated types

Changelog

CHANGELOG entry: null

Related issues

Fixes: #25036

Manual testing steps

N/A

Screenshots/Recordings

N/A

Before

N/A

After

N/A

Pre-merge author checklist

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.

Note

Introduces a new analytics hook and HOC aligned with the modern analytics system while keeping data deletion behavior via MetaMetrics during migration.

  • Add useAnalytics hook using AnalyticsEventBuilder and analytics helper: supports trackEvent, enable (opt-in/out), identify via addTraitsToUser, isEnabled, getAnalyticsId; delegates data deletion APIs (createDataDeletionTask, checkDataDeleteStatus, dates/IDs, isDataRecorded) to MetaMetrics and updates the recording flag
  • Add withAnalyticsAwareness HOC to inject metrics from useAnalytics
  • Deprecate useMetrics and withMetricsAwareness with JSDoc notices and minimal tweaks in exports/docs
  • Add tests for useAnalytics and the HOC; extend global test mocks in testSetup.js for the new hook/HOC

Written by Cursor Bugbot for commit d9260b0. This will update automatically on new commits. Configure here.

cursoragent and others added 4 commits January 22, 2026 13:51
Co-authored-by: nicolas.massart <nicolas.massart@consensys.net>
Co-authored-by: nicolas.massart <nicolas.massart@consensys.net>
- Remove legacy type support (ITrackingEvent, UserTraits, IMetaMetricsEvent)
- Accept only AnalyticsTrackingEvent and AnalyticsUserTraits
- Rename getMetaMetricsId to getAnalyticsId
- Rename interface IUseAnalyticsHook to UseAnalyticsHook (remove I prefix)
- Remove type cast from addTraitsToUser
- Update createEventBuilder to only accept string | AnalyticsTrackingEvent
@NicolasMassart NicolasMassart self-assigned this Jan 22, 2026
@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.

@metamaskbot metamaskbot added the team-mobile-platform Mobile Platform team label Jan 22, 2026
@NicolasMassart NicolasMassart marked this pull request as ready for review January 22, 2026 17:33
@NicolasMassart NicolasMassart requested a review from a team as a code owner January 22, 2026 17:33
@NicolasMassart NicolasMassart moved this to Needs dev review in PR review queue Jan 22, 2026
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 4 potential issues.

…ve hook implementation

- Refactored the `useAnalytics` hook to use instance methods of `MetaMetrics` directly, enhancing clarity and maintainability.
- Updated tests to mock `MetaMetrics` methods more effectively, ensuring accurate tracking of analytics events.
- Improved test structure by resetting mocks after each test to prevent state leakage.
- Adjusted mock return values for better alignment with expected outcomes in tests.
@github-actions
Copy link
Copy Markdown
Contributor

🔍 Smart E2E Test Selection

  • Selected E2E tags: None (no tests recommended)
  • Risk Level: low
  • AI Confidence: 85%
click to see 🤖 AI reasoning details

This PR introduces a new useAnalytics hook as a replacement for the existing useMetrics hook, along with associated types, HOC wrapper, and unit tests. The changes are primarily additive:

  1. New hook infrastructure: The new useAnalytics hook provides the same API as useMetrics but uses the newer analytics utility instead of direct MetaMetrics calls. This is internal refactoring that doesn't change user-facing behavior.

  2. Deprecation notices only: The existing useMetrics hook and withMetricsAwareness HOC are marked as deprecated but continue to work unchanged. No breaking changes to existing code.

  3. Test setup mocks: The testSetup.js changes add mocks for the new analytics hooks, which helps unit tests work properly but doesn't affect E2E tests.

  4. No E2E test infrastructure changes: The changes don't modify any E2E test pages, fixtures, or utilities.

  5. Analytics is internal infrastructure: The analytics/metrics tracking is internal telemetry that doesn't affect user-facing functionality or E2E test flows.

The new hook is already being imported by Ramp components, but this PR appears to be establishing the hook infrastructure and test mocks. Since the changes are additive, don't modify existing behavior, and don't touch E2E test infrastructure, no E2E tags are required.

View GitHub Actions results

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Jan 23, 2026

Copy link
Copy Markdown
Contributor

@Cal-L Cal-L left a comment

Choose a reason for hiding this comment

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

LGTM

@github-project-automation github-project-automation bot moved this from Needs dev review to Review finalised - Ready to be merged in PR review queue Jan 23, 2026
@NicolasMassart NicolasMassart added this pull request to the merge queue Jan 23, 2026
Merged via the queue into main with commit 42dd083 Jan 23, 2026
57 checks passed
@NicolasMassart NicolasMassart deleted the feat/new_analytics_hook branch January 23, 2026 23:18
@github-actions github-actions bot locked and limited conversation to collaborators Jan 23, 2026
@metamaskbot metamaskbot added the release-7.65.0 Issue or pull request that will be included in release 7.65.0 label Jan 23, 2026
@metamaskbot metamaskbot added release-7.64.0 Issue or pull request that will be included in release 7.64.0 and removed release-7.65.0 Issue or pull request that will be included in release 7.65.0 labels Feb 3, 2026
@metamaskbot
Copy link
Copy Markdown
Collaborator

Missing release label release-7.64.0 on PR. Adding release label release-7.64.0 on PR and removing other release labels(release-7.65.0), as PR was added to branch 7.64.0 when release was cut.

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

Labels

release-7.64.0 Issue or pull request that will be included in release 7.64.0 size-L team-mobile-platform Mobile Platform team

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

feat: create new useAnalytics hook to replace useMetrics

4 participants