Skip to content

refactor(analytics): D2 migrate useMetrics to useAnalytics in Stake and Earn components#28263

Merged
NicolasMassart merged 12 commits into
mainfrom
refactor/26686_d2-earn_use-analytics-migration
Apr 9, 2026
Merged

refactor(analytics): D2 migrate useMetrics to useAnalytics in Stake and Earn components#28263
NicolasMassart merged 12 commits into
mainfrom
refactor/26686_d2-earn_use-analytics-migration

Conversation

@NicolasMassart

@NicolasMassart NicolasMassart commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

Important

Depends on D1 #28262 for createMockEventBuilder added in app/util/test/analyticsMock.ts.
if D1 revert is ever needed, you have to also revert this one

Description

Part of the analytics system migration from the legacy useMetrics hook to the new useAnalytics hook across Stake and Earn components.

What changed:

  • Replaced useMetrics with useAnalytics in 8 production components: GasImpactModal, ClaimBanner, StakingCta, FooterButtonGroup, RewardsCard, UnstakeTimeCard, and related views (StakeConfirmationView, UnstakeConfirmationView).
  • Moved MetaMetricsEvents imports from hooks/useMetrics to core/Analytics (the canonical source) in all affected files.
  • Replaced MetricsEventBuilder.createEventBuilder with AnalyticsEventBuilder.createEventBuilder in tooltipMetaMetricsUtils.ts.
  • Updated 5 test files to use the createMockUseAnalyticsHook factory + jest.mocked(useAnalytics) pattern instead of manual inline mock objects, reducing boilerplate and improving type safety.

Changelog

CHANGELOG entry: null

Related issues

Fixes: #26817

Manual testing steps

N/A

Screenshots/Recordings

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

Medium Risk
Moderate risk because it changes analytics instrumentation in staking/earn user flows; functional behavior should be unchanged but event building/tracking could regress or break silently.

Overview
Migrates Stake/Earn UI analytics from the legacy useMetrics hook to the newer useAnalytics hook across staking modals/cards/confirmation flows, and standardizes MetaMetricsEvents imports to come from core/Analytics.

Updates tooltip event construction to use AnalyticsEventBuilder instead of MetricsEventBuilder, and refactors affected unit tests to mock useAnalytics via shared helpers (createMockUseAnalyticsHook / createMockEventBuilder) to avoid async Engine readiness polling and reduce inline mock boilerplate.

Reviewed by Cursor Bugbot for commit ca3af27. Bugbot is set up for automated code reviews on this repo. Configure here.

Replace all useMetrics usages in the Rewards module with useAnalytics,
import MetaMetricsEvents from core/Analytics instead of hooks/useMetrics,
and add createMockEventBuilder factory to analyticsMock to eliminate
inline builder boilerplate across test files.
@NicolasMassart NicolasMassart self-assigned this Apr 1, 2026
@github-actions

github-actions Bot commented Apr 1, 2026

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 Apr 1, 2026
@github-project-automation github-project-automation Bot moved this to Needs dev review in PR review queue Apr 1, 2026
@github-actions github-actions Bot added the size-M label Apr 1, 2026
@NicolasMassart NicolasMassart marked this pull request as ready for review April 2, 2026 10:47
@NicolasMassart NicolasMassart requested a review from a team as a code owner April 2, 2026 10:47
@github-actions github-actions Bot added the risk-low Low testing needed · Low bug introduction risk label Apr 2, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

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 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

@NicolasMassart NicolasMassart changed the base branch from main to refactor/26686_d1-rewards_use-analytics-migration April 2, 2026 14:19
@NicolasMassart NicolasMassart added the DO-NOT-MERGE Pull requests that should not be merged label Apr 2, 2026
… refactor/26686_d2-earn_use-analytics-migration
Base automatically changed from refactor/26686_d1-rewards_use-analytics-migration to main April 8, 2026 12:57
@NicolasMassart NicolasMassart requested a review from a team as a code owner April 8, 2026 12:57

@Matt561 Matt561 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving for Earn-owned files.

@github-actions github-actions Bot added risk-low Low testing needed · Low bug introduction risk and removed risk-low Low testing needed · Low bug introduction risk labels Apr 9, 2026
@github-actions

github-actions Bot commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

🔍 Smart E2E Test Selection

  • Selected E2E tags: SmokeTrade, SmokeConfirmations
  • Selected Performance tags: None (no tests recommended)
  • Risk Level: low
  • AI Confidence: 82%
click to see 🤖 AI reasoning details

E2E Test Selection:
All 18 changed files are part of a refactoring migration in the Stake/Earn UI components. The changes migrate analytics hook usage from useMetrics to useAnalytics and move MetaMetricsEvents imports from hooks/useMetrics to core/Analytics. Additionally, MetricsEventBuilder is replaced with AnalyticsEventBuilder in tooltipMetaMetricsUtils.ts.

Key findings:

  1. No business logic changes - purely import path/hook refactoring
  2. Affected components: StakeConfirmationView, UnstakeConfirmationView, GasImpactModal, ClaimBanner, StakingCta, FooterButtonGroup, RewardsCard, UnstakeTimeCard, StakingEarnings
  3. Test files updated to use new createMockUseAnalyticsHook pattern
  4. The useAnalytics hook provides the same interface as useMetrics (trackEvent, createEventBuilder)

The staking flows (stake, unstake, claim) are covered by SmokeTrade (ETH staking flows). The StakeConfirmationView and UnstakeConfirmationView are confirmation screens, so SmokeConfirmations is also warranted per the tag dependency rules (SmokeTrade → SmokeConfirmations).

Risk is low because this is a pure refactoring with no behavioral changes, but we should validate the staking flows still work correctly with the new analytics hook.

Performance Test Selection:
These changes are purely analytics hook import refactoring with no UI rendering changes, no state management changes, no data loading changes, and no performance-sensitive code paths. The migration from useMetrics to useAnalytics does not affect rendering performance, asset loading, or any other performance-critical flows.

View GitHub Actions results

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.16%. Comparing base (19e3757) to head (ca3af27).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #28263      +/-   ##
==========================================
- Coverage   82.16%   82.16%   -0.01%     
==========================================
  Files        4924     4927       +3     
  Lines      129321   129398      +77     
  Branches    28821    28833      +12     
==========================================
+ Hits       106256   106317      +61     
- Misses      15791    15808      +17     
+ Partials     7274     7273       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sonarqubecloud

sonarqubecloud Bot commented Apr 9, 2026

Copy link
Copy Markdown

@github-actions

github-actions Bot commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

E2E Fixture Validation — Schema is up to date
16 value mismatches detected (expected — fixture represents an existing user).
View details

@github-project-automation github-project-automation Bot moved this from Needs dev review to Review finalised - Ready to be merged in PR review queue Apr 9, 2026
@NicolasMassart NicolasMassart added this pull request to the merge queue Apr 9, 2026
Merged via the queue into main with commit 3708dbf Apr 9, 2026
108 of 111 checks passed
@NicolasMassart NicolasMassart deleted the refactor/26686_d2-earn_use-analytics-migration branch April 9, 2026 15:25
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 9, 2026
@metamaskbot metamaskbot added the release-7.74.0 Issue or pull request that will be included in release 7.74.0 label Apr 9, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

DO-NOT-MERGE Pull requests that should not be merged release-7.74.0 Issue or pull request that will be included in release 7.74.0 risk-low Low testing needed · Low bug introduction risk size-M team-mobile-platform Mobile Platform team

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

PR D2: migrate useMetrics to useAnalytics in Earn/Stake team (~10 files)

5 participants