refactor(analytics): migrate Batch 2-11: metamask-earn#26292
Conversation
Updated components and hooks to replace the useMetrics hook with useAnalytics, ensuring consistent analytics tracking across the application. This change includes modifications to LearnMoreModalFooter, StakeButton, StakingBalance, and related test files to utilize the new analytics structure. Additionally, adjusted event tracking methods to align with the new analytics implementation.
Refactored the StakeButton test file to replace the previous useMetrics mock with a new mock for useStablecoinLendingRedirect. This change ensures that the tests accurately reflect the current analytics structure and improve the clarity of the test setup.
|
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. |
…2-11_metamask-earn
🔍 Smart E2E Test Selection
click to see 🤖 AI reasoning detailsE2E Test Selection:
The The affected components are:
Since staking flows are mentioned in the SmokeTrade tag description ("native ETH staking flows initiated from the wallet actions menu"), I'm selecting SmokeTrade as a precautionary measure. However, the risk is low because:
Performance Test Selection: |
|
|
Missing release label release-7.67.0 on PR. Adding release label release-7.67.0 on PR and removing other release labels(release-7.68.0), as PR was cherry-picked in branch 7.67.0. |



Description
Phase 2 analytics migration (Batch 2-11): migrate Stake/Earn's
useMetricshook andMetaMetrics.getInstance()calls from the legacy MetaMetrics system to the new analytics system.Reason: Deprecate MetaMetrics in favour of the shared analytics utility and AnalyticsController.
Changes: Stake components and hooks now use
useAnalyticsfromhooks/useAnalytics/useAnalyticsand importMetaMetricsEventsdirectly fromcore/Analytics;withMetaMetricsutility now usesanalytics.trackEvent()andAnalyticsEventBuilderinstead ofMetaMetrics.getInstance().trackEvent()andMetricsEventBuilder; test mocks updated accordingly.Changes
Source files (6):
LearnMoreModalFooter.tsx,StakingButtons.tsx,StakingBalance.tsx,StakeButton/index.tsx: replaceduseMetricswithuseAnalytics;MetaMetricsEventsnow imported fromcore/AnalyticsusePoolStakedDeposit/index.ts: replaceduseMetricswithuseAnalytics(also migrated as it's the source for a listed test file)withMetaMetrics.ts: replacedMetaMetrics.getInstance().trackEvent()withanalytics.trackEvent()andMetricsEventBuilderwithAnalyticsEventBuilderTest files (3):
StakeButton.test.tsx: replaceduseMetricsmock withuseAnalyticsmock; added transitiveuseMetricsmock for unmigrateduseStablecoinLendingRedirectdependency; replacedMetricsEventBuilderwithAnalyticsEventBuilderusePoolStakedDeposit.test.tsx: replaceduseMetricsmock/import withuseAnalytics; replacedMetricsEventBuilderwithAnalyticsEventBuilderwithMetaMetrics.test.ts: replacedMetaMetrics.getInstance()spy withanalyticsmodule mock; updatedMetaMetricsEventsimport fromcore/AnalyticsChangelog
CHANGELOG entry: null
Related issues
Fixes: https://consensyssoftware.atlassian.net/browse/MCWP-298 (Batch 2-11)
Manual testing steps
Screenshots/Recordings
N/A – analytics migration, no UI change.
Pre-merge author checklist
Pre-merge reviewer checklist
Note
Medium Risk
Touches analytics instrumentation across multiple stake/earn entry points and a shared
withMetaMetricswrapper, so incorrect wiring could silently drop or duplicate events; functional app behavior should be unchanged.Overview
Updates Stake/Earn UI components and staking hooks to use the new analytics stack by replacing legacy
useMetrics/MetricsEventBuilderandMetaMetrics.getInstance().trackEvent()calls withuseAnalytics,AnalyticsEventBuilder, andanalytics.trackEvent(), while importingMetaMetricsEventsfromcore/Analytics.Refactors
withMetaMetricsto build events with the new builder and dispatch via the shared analytics utility (including promise-handling), and updates affected unit tests/mocks accordingly (including auseStablecoinLendingRedirectmock to keep navigation tests stable).Written by Cursor Bugbot for commit 160d885. This will update automatically on new commits. Configure here.