MetaMask / MetaMask/metamask-extension
[P1] Extract dapp + app-opened metrics from `background.js`
- Dominant language
- TypeScript
- Stars
- 13.2k
- Forks
- 5.6k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 451
Description
**Parent Epic:** [#44543 — `app/scripts/background.js` decomposition](https://github.com/MetaMask/metamask-extension/issues/44543)
**File:** `app/scripts/background.js` → `app/scripts/lib/metrics/`
**Size:** M
---
## Problem
`emitDappViewedMetricEvent`, `trackDappView`, `emitAppOpenedMetricEvent`, `shouldEmitAppOpened`, `trackAppOpened` (~1278–1435), and `onNavigateToTab` (~2273–2319) emit dapp/app-opened metrics and DeFi-referral navigation. They own the tab→origin registries `senderOriginMapping`/`tabOriginMapping`/`frameIdMapping` (confined to this cluster). `shouldEmitAppOpened` reads the shared UI-presence counters — the one cross-cluster read. Called from the connection cluster.
## Solution
`app/scripts/lib/metrics/dapp-metrics.ts`, injecting `getController()` + a single `isAnyUiOpen()` predicate. Extract `createUiPresenceTracker()` from the root as part of this ticket (the root keeps ownership of the counters; the module receives the predicate). The three origin registries move entirely into this module (self-owned state).
## Acceptance Criteria
- [ ] TS module owning the origin registries; `isAnyUiOpen()` presence seam extracted from the root.
- [ ] Originals deleted; unit tests against a mock controller.
- [ ] `DappViewed` / `AppOpened` / DeFi-referral parity in a dev build.
Contributor guide
Research direction
Start in app/scripts/background.js with emitDappViewedMetricEvent, trackDappView, emitAppOpenedMetricEvent, shouldEmitAppOpened, trackAppOpened, and onNavigateToTab, then compare the target app/scripts/lib/metrics/dapp-metrics.ts design. Extract createUiPresenceTracker while keeping the root's counters and move the origin registries into the module. Add unit tests with a mock controller and verify DappViewed, AppOpened, and DeFi-referral parity in a dev build.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- observability
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 50/100