[Bug]: analytics-controller.init() not awaited Segment events sent with userId: undefined for opted in users

Open Beginner friendly
#45,125 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
typescript
Domain
analytics

Research direction

Start with app/scripts/messenger-client-init/analytics-controller-init.ts at line 63, then read the @metamask/analytics-controller 2.0.0 changelog entry about asynchronous init. Ensure initialization completes before startup analytics events are tracked, and verify that resulting Segment track/view payloads contain the analyticsId rather than undefined.

Written by the indexing model from the issue text.

Description

cs-triaged external-contributor regression-prod-2.0.0 Sev2-normal team-data-and-analytics team-wallet-platform type-bug
Describe the bug

AnalyticsController.init() is called without await in app/scripts/messenger-client-init/analytics-controller-init.ts (line 63), but @metamask/analytics-controller@2.0.0 (the version locked in yarn.lock) made init() asynchronous a breaking change explicitly documented in its CHANGELOG:

"AnalyticsController.init ... are now asynchronous and return a Promise, so await them before tracking events"

Inside init(), the call order is:

  1. await #maybeResolveLocation() async geolocation network request (runs because isGeolocationEnabled: true)
  2. platformAdapter.onSetupCompleted(analyticsId) this sets cachedAnalyticsId in the platform adapter
  3. Replay queued events

Because init() is not awaited, onSetupCompleted hasn't run yet when the first analytics events fire after startup. The platform adapter's cachedAnalyticsId variable remains undefined, so all track() and view() calls send Segment payloads with userId: undefined until the geolocation network request completes.

This affects all returning (already opted-in) users on every startup.

Expected behavior

All Segment track/view payloads include the correct userId (analyticsId). controller.init() must be awaited before any analytics events are tracked.

Screenshots/Recordings

No response

Steps to reproduce
  1. Be a returning MetaMask user (already opted in to analytics)
  2. Open MetaMask background service worker initializes AnalyticsController
  3. Any analytics event fired during the geolocation resolution window is sent to Segment with userId: undefined
Error messages or log output
No thrown error — silent data corruption.
Segment track/view payloads contain: { userId: undefined }

Root cause:
File: app/scripts/messenger-client-init/analytics-controller-init.ts, line 63

Current (buggy):
  controller.init();  // Promise not awaited

Fix:
  await controller.init();

Upstream CHANGELOG: https://github.com/MetaMask/core/blob/main/packages/analytics-controller/CHANGELOG.md#200
Where was this bug found?

Live version (from official store)

Version

12.x (main branch yarn.lock resolves @metamask/analytics-controller@2.0.0)

Build type

No response

Browser

Chrome

Operating system

Windows, Linux

Hardware wallet

No response

Additional context

Related to the @metamask/analytics-controller@2.0.0 upgrade. The CHANGELOG for that package explicitly warns that init() is now async and must be awaited, but analytics-controller-init.ts was not updated accordingly.

Severity

No response

Dominant language
TypeScript
Stars
13.2k
Forks
5.6k
Avg merge
2d 6h
Merged PRs (30d)
443

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from MetaMask/metamask-extension

All issues in MetaMask/metamask-extension

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.