Skip to content

feat(perps): track pay-token validation errors in analytics#29170

Merged
michalconsensys merged 12 commits into
mainfrom
feat/track-perps-pay-token-validation-errors
Apr 24, 2026
Merged

feat(perps): track pay-token validation errors in analytics#29170
michalconsensys merged 12 commits into
mainfrom
feat/track-perps-pay-token-validation-errors

Conversation

@michalconsensys

@michalconsensys michalconsensys commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

Description

In the "trade with any token" flow, the custom confirm button performs its own validation but was not tracking when standardized blocking alerts (e.g. useInsufficientPayTokenBalanceAlert, useNoPayTokenQuotesAlert) fire. This meant validation errors like "insufficient funds" and "insufficient source token balance for relay deposit" — which account for the majority of deposit failures — were invisible in Mixpanel.

This PR adds two useEffect hooks in PerpsOrderViewContentBase that fire PERPS_ERROR analytics events when:

  1. Blocking pay-token alerts appear (e.g. insufficient balance for relay quote totals, no quotes available) — tracked as error_type: 'validation' with the alert's human-readable message.
  2. Insufficient pay-token balance warning appears (custom margin-only check) — tracked as error_type: 'warning' with standardized insufficient_balance key.

Both events include error_type, error_message / warning_message, screen_name, and screen_type properties, following the existing PERPS_ERROR schema.

Note: the UI integration (button disabling + alert message rendering) was already in place. This PR adds only the missing analytics tracking and corresponding unit tests.

Changelog

CHANGELOG entry: null

Related issues

Fixes: https://consensyssoftware.atlassian.net/browse/TAT-2720

Manual testing steps

Feature: Pay-token validation error tracking in analytics

  Scenario: user sees blocking pay-token alert tracked in analytics
    Given the user is on the Perps order screen with "trade with any token" enabled
    And the user has selected a custom payment token (not Perps balance)

    When the relay quote returns an insufficient balance alert
    Then a PERPS_ERROR event fires with error_type "validation" and the alert message
    And the Place Order button is disabled

  Scenario: user sees no analytics event when using Perps balance
    Given the user is on the Perps order screen
    And the user is paying with their Perps balance (not a custom token)

    When blocking pay-token alerts would otherwise fire
    Then no PERPS_ERROR event is tracked (hasCustomTokenSelected is false)

Screenshots/Recordings

N/A — no UI changes; analytics-only.

Pre-merge author checklist

Performance checks (if applicable)

  • I've tested on Android
    • Ideally on a mid-range device; emulator is acceptable
  • I've tested with a power user scenario
    • Use these power-user SRPs to import wallets with many accounts and tokens
  • I've instrumented key operations with Sentry traces for production performance metrics

For performance guidelines and tooling, see the Performance Guide.

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

Low Risk
Low risk: adds analytics-only PERPS_ERROR tracking in PerpsOrderView plus unit tests; behavior changes are limited to when events fire and could affect metrics volume if conditions are mis-triggered.

Overview
Adds declarative usePerpsEventTracking instrumentation in PerpsOrderView to emit MetaMetricsEvents.PERPS_ERROR when blocking pay-token alerts appear (tracked as validation with a robust message/title/key fallback) and when the insufficient pay-token balance check is true (tracked as warning with the standardized insufficient_balance key).

Extends PerpsOrderView.test.tsx with coverage ensuring events fire only for custom-token flows (not Perps balance), don’t fire when no blocking alerts exist, and validate the fallback message selection logic; introduces a mock for useTransactionPayToken to support the warning-path tests.

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

Fire PERPS_ERROR events when blocking pay-token alerts or insufficient
balance warnings are displayed on the order screen, enabling Mixpanel
visibility into deposit failure causes in the trade-with-any-token flow.
Verify that PERPS_ERROR analytics events fire when blocking pay-token
alerts appear on the order screen, and do not fire when alerts are
absent or when Perps balance (not a custom token) is selected.
@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.

@metamaskbotv2 metamaskbotv2 Bot added the team-perps Perps team label Apr 22, 2026
…ility

BaseController.update() now expects Draft<State> from immer, which is
incompatible with the plain state type used in test helpers. Cast via
`any` to avoid excessively deep type instantiation with complex state.
@codecov-commenter

codecov-commenter commented Apr 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.28%. Comparing base (7f5ea83) to head (383ba1f).
⚠️ Report is 147 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #29170      +/-   ##
==========================================
+ Coverage   82.26%   82.28%   +0.02%     
==========================================
  Files        5127     5132       +5     
  Lines      135863   135937      +74     
  Branches    30586    30607      +21     
==========================================
+ Hits       111766   111862      +96     
+ Misses      16467    16454      -13     
+ Partials     7630     7621       -9     

☔ 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.

@michalconsensys michalconsensys marked this pull request as ready for review April 23, 2026 08:19
@michalconsensys michalconsensys requested review from a team as code owners April 23, 2026 08:19
@github-actions github-actions Bot added the risk:high AI analysis: high risk label Apr 23, 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.

There are 2 total unresolved issues (including 1 from previous review).

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.

Reviewed by Cursor Bugbot for commit 1818fbf. Configure here.

@github-actions

Copy link
Copy Markdown
Contributor

AI PR Analysis

🚫 Merge safe: false | 🟠 Risk: high

Merge decision: AI analysis did not complete — manual review required before merging.

AI analysis did not complete. Manual review recommended.

View run

Add tests for the hasInsufficientPayTokenBalance warning useEffect and
alertMessage fallback branches to meet the 80% new-code coverage gate.

@abretonc7s abretonc7s 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.

can we separate concerns and have those business effect in hooks?

Move the two inline useEffect + track() blocks into
usePerpsPayTokenErrorTracking, which uses the declarative
usePerpsEventTracking API consistent with the rest of the codebase.
Remove the usePerpsPayTokenErrorTracking wrapper hook and call
usePerpsEventTracking directly in PerpsOrderView, consistent with
existing Perps UI patterns.
@github-actions

Copy link
Copy Markdown
Contributor

🔍 Smart E2E Test Selection

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

E2E Test Selection:
The PR adds two usePerpsEventTracking calls to PerpsOrderView.tsx for analytics event tracking (PERPS_ERROR events) when pay-token validation alerts appear (blocking alerts and insufficient balance warnings). The companion test file adds comprehensive unit tests for this new behavior.

Key observations:

  1. Scope is narrow: Changes are isolated to the Perps Order View component - only analytics tracking hooks are added, no UI rendering changes, no business logic modifications, no navigation changes.
  2. No shared component impact: The changes don't touch Browser, TabBar, Modals, or Confirmations shared components that could break other test suites.
  3. Low risk: Adding event tracking hooks is additive and non-breaking. The hooks fire side effects (analytics) but don't affect component rendering or user-facing behavior.
  4. Unit tests added: The test file adds thorough coverage for the new tracking behavior, reducing regression risk.

Tag selection rationale:

  • SmokePerps: Primary tag - directly tests the Perps feature area where changes were made (PerpsOrderView is a core Perps component).
  • SmokeWalletPlatform: Required by SmokePerps description ("When selecting SmokePerps, also select SmokeWalletPlatform (Trending section)") since Perps is embedded in the Trending tab.
  • SmokeConfirmations: Required by SmokePerps description ("also select SmokeConfirmations (Add Funds deposits are on-chain transactions)") as the dependent tag for on-chain transaction flows.

Performance Test Selection:
The changes only add analytics event tracking hooks (usePerpsEventTracking) to the PerpsOrderView component. These hooks fire analytics side effects and do not affect UI rendering performance, data loading, state management, or any critical user flow timing. No performance tests are warranted for pure analytics additions.

View GitHub Actions results

@github-actions github-actions Bot removed the size-L label Apr 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

@sonarqubecloud

Copy link
Copy Markdown

@michalconsensys michalconsensys added this pull request to the merge queue Apr 24, 2026
Merged via the queue into main with commit ab0790e Apr 24, 2026
106 checks passed
@michalconsensys michalconsensys deleted the feat/track-perps-pay-token-validation-errors branch April 24, 2026 10:44
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 24, 2026
@metamaskbotv2 metamaskbotv2 Bot added the release-7.76.0 Issue or pull request that will be included in release 7.76.0 label Apr 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-7.76.0 Issue or pull request that will be included in release 7.76.0 risk:high AI analysis: high risk size-M team-perps Perps team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants