Skip to content

feat(analytics-controller): add optional pre-consent event queue - #9252

Merged
gauthierpetetin merged 3 commits into
mainfrom
feat/analytics-controller-pre-consent-queue
Jun 25, 2026
Merged

feat(analytics-controller): add optional pre-consent event queue#9252
gauthierpetetin merged 3 commits into
mainfrom
feat/analytics-controller-pre-consent-queue

Conversation

@gauthierpetetin

@gauthierpetetin gauthierpetetin commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Explanation

Adds an opt-in pre-consent event queue to AnalyticsController, gated by the new isPreConsentQueueEnabled option (disabled by default).

Track events received while the user is undecided are persisted and replayed on opt-in, or discarded on opt-out.

References

Fixes: N/A

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

Medium Risk
Touches analytics consent and what gets stored or sent before/after opt-in/out; opt-out and reset now purge more persisted data, but the feature is off by default and track-only for the pre-consent path.

Overview
Adds an opt-in pre-consent flow to AnalyticsController via isPreConsentQueueEnabled (default off). When enabled, track events fired while the user is undecided (consentDecisionMade false) are stored in persisted preConsentEventQueue instead of being dropped; they are replayed on opt-in (through the normal delivery path, including eventQueue when persistence is on) and discarded on opt-out.

Introduces consentDecisionMade (default false, set by optIn/optOut), selectConsentDecisionMade, and resetConsentDecision (undecided again; clears delivery and pre-consent queues). optOut now also clears queued delivery and pre-consent events. init reconciles stale persisted pre-consent queues (replay if opted in, clear if opted out or feature disabled).

Legacy behavior is unchanged when the flag is off: undecided users still do not get events queued or sent.

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

Add an opt-in pre-consent event queue to AnalyticsController, gated by the
new isPreConsentQueueEnabled option (disabled by default). Track events
received while the user is undecided are persisted and replayed on opt-in,
or discarded on opt-out. Adds a consentDecisionMade state field, a
selectConsentDecisionMade selector, and a resetConsentDecision action.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

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.

Reviewed by Cursor Bugbot for commit b05a866. Configure here.

Comment thread packages/analytics-controller/src/AnalyticsController.ts Outdated
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds an opt-in “pre-consent” track-event queue to AnalyticsController, allowing clients to persist track events while the user is undecided and then replay them on opt-in (or discard on opt-out), gated behind a new isPreConsentQueueEnabled option (default off).

Changes:

  • Introduces consentDecisionMade and preConsentEventQueue controller state, with metadata and reconciliation/replay/clear logic.
  • Adds resetConsentDecision to the controller API/messenger actions, plus a new selectConsentDecisionMade selector.
  • Extends the Jest test suite and updates the package changelog.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
packages/analytics-controller/src/selectors.ts Adds selectConsentDecisionMade selector for distinguishing undecided vs explicit opt-out.
packages/analytics-controller/src/index.ts Exports the new AnalyticsControllerResetConsentDecisionAction type.
packages/analytics-controller/src/AnalyticsController.ts Adds pre-consent queue state/metadata, feature flag, queueing/replay/cleanup logic, and resetConsentDecision.
packages/analytics-controller/src/AnalyticsController.test.ts Adds/updates tests for consentDecisionMade, pre-consent queue behavior, and the new selector.
packages/analytics-controller/src/AnalyticsController-method-action-types.ts Adds resetConsentDecision method action type and includes it in the union.
packages/analytics-controller/CHANGELOG.md Documents the new optional pre-consent queue feature.
Comments suppressed due to low confidence (1)

packages/analytics-controller/src/AnalyticsController.ts:421

  • initialState is built by spreading getDefaultAnalyticsControllerState() (which sets consentDecisionMade: false) over the provided state. For persisted states from before consentDecisionMade existed, this forces consentDecisionMade to false ("undecided"). If a client later enables isPreConsentQueueEnabled, users who previously had only optedIn: false will start persisting pre-consent events, which is a backward-compatibility behavior change and may conflict with prior opt-out expectations. Consider defaulting consentDecisionMade to true when the incoming state does not specify it, preserving the legacy "disabled means drop" behavior unless clients explicitly opt into the undecided state.
    isPreConsentQueueEnabled = false,
  }: AnalyticsControllerOptions) {
    const initialState: AnalyticsControllerState = {
      ...getDefaultAnalyticsControllerState(),
      ...state,

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/analytics-controller/CHANGELOG.md Outdated
Comment thread packages/analytics-controller/src/AnalyticsController.test.ts
Comment thread packages/analytics-controller/src/AnalyticsController.test.ts Outdated
- Drop a stale persisted pre-consent queue on init when the queue is
  disabled, so it can never be replayed by a later enabled instance.
- Add a deriveStateFromMetadata test asserting preConsentEventQueue is
  persisted but excluded from logs, debug snapshots, and UI.
- Move selectConsentDecisionMade tests into selectors.test.ts.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

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

Really clean implementation of the pre-consent queue! Nice work!

@gauthierpetetin

Copy link
Copy Markdown
Contributor Author

@metamaskbot publish-previews

@github-actions

Copy link
Copy Markdown
Contributor

Preview builds have been published. Learn how to use preview builds in other projects.

Expand for full list of packages and versions.
@metamask-previews/account-tree-controller@7.5.3-preview-5133205c8
@metamask-previews/accounts-controller@39.0.3-preview-5133205c8
@metamask-previews/address-book-controller@7.1.2-preview-5133205c8
@metamask-previews/ai-controllers@0.7.0-preview-5133205c8
@metamask-previews/analytics-controller@1.1.1-preview-5133205c8
@metamask-previews/analytics-data-regulation-controller@0.0.0-preview-5133205c8
@metamask-previews/announcement-controller@8.1.0-preview-5133205c8
@metamask-previews/app-metadata-controller@2.0.1-preview-5133205c8
@metamask-previews/approval-controller@9.0.2-preview-5133205c8
@metamask-previews/assets-controller@9.0.2-preview-5133205c8
@metamask-previews/assets-controllers@109.2.2-preview-5133205c8
@metamask-previews/authenticated-user-storage@2.1.0-preview-5133205c8
@metamask-previews/base-controller@9.1.0-preview-5133205c8
@metamask-previews/base-data-service@0.1.3-preview-5133205c8
@metamask-previews/bitcoin-regtest-up@0.0.0-preview-5133205c8
@metamask-previews/bridge-controller@76.0.0-preview-5133205c8
@metamask-previews/bridge-status-controller@72.2.0-preview-5133205c8
@metamask-previews/build-utils@3.0.4-preview-5133205c8
@metamask-previews/chain-agnostic-permission@1.6.2-preview-5133205c8
@metamask-previews/chomp-api-service@3.1.0-preview-5133205c8
@metamask-previews/claims-controller@0.5.3-preview-5133205c8
@metamask-previews/client-controller@1.0.1-preview-5133205c8
@metamask-previews/compliance-controller@2.1.0-preview-5133205c8
@metamask-previews/composable-controller@12.0.1-preview-5133205c8
@metamask-previews/config-registry-controller@0.4.1-preview-5133205c8
@metamask-previews/connectivity-controller@0.2.0-preview-5133205c8
@metamask-previews/controller-utils@12.3.0-preview-5133205c8
@metamask-previews/core-backend@6.3.3-preview-5133205c8
@metamask-previews/delegation-controller@3.0.2-preview-5133205c8
@metamask-previews/earn-controller@12.2.1-preview-5133205c8
@metamask-previews/eip-5792-middleware@3.0.4-preview-5133205c8
@metamask-previews/eip-7702-internal-rpc-middleware@0.1.1-preview-5133205c8
@metamask-previews/eip1193-permission-middleware@2.0.1-preview-5133205c8
@metamask-previews/ens-controller@19.1.4-preview-5133205c8
@metamask-previews/eth-block-tracker@15.0.1-preview-5133205c8
@metamask-previews/eth-json-rpc-middleware@23.1.3-preview-5133205c8
@metamask-previews/eth-json-rpc-provider@6.0.1-preview-5133205c8
@metamask-previews/foundryup@1.0.1-preview-5133205c8
@metamask-previews/gas-fee-controller@26.2.3-preview-5133205c8
@metamask-previews/gator-permissions-controller@4.2.1-preview-5133205c8
@metamask-previews/geolocation-controller@0.1.3-preview-5133205c8
@metamask-previews/java-tron-up@0.0.0-preview-5133205c8
@metamask-previews/json-rpc-engine@10.5.0-preview-5133205c8
@metamask-previews/json-rpc-middleware-stream@8.0.8-preview-5133205c8
@metamask-previews/keyring-controller@27.1.0-preview-5133205c8
@metamask-previews/local-node-utils@0.0.0-preview-5133205c8
@metamask-previews/logging-controller@8.0.2-preview-5133205c8
@metamask-previews/message-manager@14.1.2-preview-5133205c8
@metamask-previews/messenger@1.2.0-preview-5133205c8
@metamask-previews/messenger-cli@0.2.0-preview-5133205c8
@metamask-previews/money-account-balance-service@2.1.1-preview-5133205c8
@metamask-previews/money-account-controller@0.3.3-preview-5133205c8
@metamask-previews/money-account-upgrade-controller@2.1.0-preview-5133205c8
@metamask-previews/multichain-account-service@11.0.0-preview-5133205c8
@metamask-previews/multichain-api-middleware@3.1.5-preview-5133205c8
@metamask-previews/multichain-network-controller@3.1.4-preview-5133205c8
@metamask-previews/multichain-transactions-controller@7.1.1-preview-5133205c8
@metamask-previews/name-controller@9.1.2-preview-5133205c8
@metamask-previews/network-controller@33.0.0-preview-5133205c8
@metamask-previews/network-enablement-controller@5.4.0-preview-5133205c8
@metamask-previews/notification-services-controller@24.2.0-preview-5133205c8
@metamask-previews/passkey-controller@2.0.1-preview-5133205c8
@metamask-previews/permission-controller@13.1.1-preview-5133205c8
@metamask-previews/permission-log-controller@5.1.0-preview-5133205c8
@metamask-previews/perps-controller@8.3.0-preview-5133205c8
@metamask-previews/phishing-controller@17.2.0-preview-5133205c8
@metamask-previews/polling-controller@16.0.7-preview-5133205c8
@metamask-previews/preferences-controller@23.1.0-preview-5133205c8
@metamask-previews/profile-metrics-controller@4.0.0-preview-5133205c8
@metamask-previews/profile-sync-controller@28.2.0-preview-5133205c8
@metamask-previews/ramps-controller@14.3.0-preview-5133205c8
@metamask-previews/rate-limit-controller@7.0.1-preview-5133205c8
@metamask-previews/react-data-query@0.2.1-preview-5133205c8
@metamask-previews/remote-feature-flag-controller@4.2.2-preview-5133205c8
@metamask-previews/sample-controllers@5.0.2-preview-5133205c8
@metamask-previews/seedless-onboarding-controller@10.0.2-preview-5133205c8
@metamask-previews/selected-network-controller@26.1.4-preview-5133205c8
@metamask-previews/shield-controller@5.1.2-preview-5133205c8
@metamask-previews/signature-controller@39.2.6-preview-5133205c8
@metamask-previews/smart-transactions-controller@24.2.3-preview-5133205c8
@metamask-previews/snap-account-service@1.0.0-preview-5133205c8
@metamask-previews/social-controllers@2.3.1-preview-5133205c8
@metamask-previews/solana-test-validator-up@0.0.0-preview-5133205c8
@metamask-previews/storage-service@1.0.2-preview-5133205c8
@metamask-previews/subscription-controller@6.2.0-preview-5133205c8
@metamask-previews/transaction-controller@68.1.1-preview-5133205c8
@metamask-previews/transaction-pay-controller@23.14.0-preview-5133205c8
@metamask-previews/user-operation-controller@41.2.5-preview-5133205c8
@metamask-previews/wallet@4.0.0-preview-5133205c8
@metamask-previews/wallet-cli@0.0.0-preview-5133205c8

@gauthierpetetin
gauthierpetetin added this pull request to the merge queue Jun 25, 2026
Merged via the queue into main with commit 6a33636 Jun 25, 2026
396 checks passed
@gauthierpetetin
gauthierpetetin deleted the feat/analytics-controller-pre-consent-queue branch June 25, 2026 08:55
@gauthierpetetin gauthierpetetin mentioned this pull request Jun 25, 2026
4 tasks
pull Bot pushed a commit to dmrazzy/core that referenced this pull request Jun 25, 2026
## Explanation

Monorepo release that publishes the analytics pre-consent queue work:

- **`@metamask/analytics-controller`** → **1.2.0** (minor)
- Adds an optional pre-consent event queue to `AnalyticsController`
(disabled by default via `isPreConsentQueueEnabled`), with a
`consentDecisionMade` state field, `selectConsentDecisionMade` selector,
and `resetConsentDecision` action
([MetaMask#9252](MetaMask#9252)).
- Bumps `@metamask/utils` from `^11.9.0` to `^11.11.0`
([MetaMask#9074](MetaMask#9074)).

Root `package.json` is bumped from `1072.0.0` to `1073.0.0`. There are
no application source changes in this diff.

## References

- MetaMask#9252
- MetaMask#9074

## Checklist

- [ ] I've updated the test suite for new or updated code as appropriate
- [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [ ] I've communicated my changes to consumers by [updating changelogs
for packages I've
changed](https://github.com/MetaMask/core/tree/main/docs/processes/updating-changelogs.md)
- [ ] I've introduced [breaking
changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md)
in this PR and have prepared draft pull requests for clients and
consumer packages to resolve them
pull Bot pushed a commit to Reality2byte/metamask-extension that referenced this pull request Jul 28, 2026
…taMask#43869)

## **Description**

Move consent decision state and pre-consent event queue from
MetaMetricsController to AnalyticsController.

It replaces this state:
- MetaMetricsController's `completedMetaMetricsOnboarding`
- MetaMetricsController's `eventsBeforeMetricsOptIn`

by this state:
- AnalyticsController's `consentDecisionMade`
- AnalyticsController's `preConsentEventQueue`

It leverage pre-consent event queue feature added to AnalyticsController
in this PR: MetaMask/core#9252 (and this PR:
MetaMask/core#9284)

## **Changelog**

CHANGELOG entry: null

## **Related issues**

Contributes to:
MetaMask/MetaMask-planning#7337

## **Manual testing steps**

1. Start the on-boarding until you land on MetaMetrics screen
2. Open inspector > Application > Extension storage > Local >
AnalyticsController
3. Confirm `consentDecisionMade` is set to false
4. Confirm `preConsentEventQueue` includes a list of events
5. Get your analyticsId, open the Segment debugger, past your
analyticsId in the search bar and confirm there are no events listed
6. Opt in to MetaMetrics on the MetaMetrics screen
7. Confirm the events previously listed in `preConsentEventQueue` now
show up in Segment
8. Refresh the inspector
9. Confirm `consentDecisionMade` is now set to true
10. Confirm `preConsentEventQueue` is now empty

## **Screenshots/Recordings**

NA

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **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.


<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Medium Risk**
> Touches privacy/consent and what gets sent before/after opt-in, plus a
persisted migration that reshapes queued events; incorrect gating could
leak or drop analytics/Sentry data.
> 
> **Overview**
> **Analytics consent and pre-consent buffering now live on
`AnalyticsController`**, replacing
`MetaMetricsController.completedMetaMetricsOnboarding` and
`eventsBeforeMetricsOptIn`. Call sites (background metrics gates, cookie
handler, participation checks, Sentry) read **`consentDecisionMade`**
and **`optedIn`** from analytics state only.
> 
> **`MetaMetricsController` no longer queues or replays pre-consent
events**—those messenger actions and state fields are removed.
**`setParticipateInMetaMetrics`** delegates opt-in/out to
`AnalyticsController:optIn` / `optOut`, uses **`resetConsentDecision`**
when set to `null`, and still flushes/clears **trace** buffering
locally. Init enables **`isPreConsentQueueEnabled`** on analytics
(package bump to `@metamask/analytics-controller` ^1.2.1).
> 
> **Install and OAuth paths** stop manual buffering: **`App Installed`**
always goes through `trackEvent`, and OAuth injects **`trackEvent`**
instead of `addEventBeforeMetricsOptIn`. Extension **`trackEvent`** only
bails when basic external services are disabled; consent gating is
assumed downstream in the controller queue.
> 
> **Migration #220** copies consent from the old flag, migrates buffered
events into `preConsentEventQueue` (including anonymous splits for
sensitive properties), and strips the legacy MetaMetrics fields.
Fixtures, types, and e2e metrics tests are updated to the new shape.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
c0532b1. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants