Skip to main content

AnalyticsController Actions

18 actions registered.

NameDeprecated
createEventFragment
deleteEventFragment
finalizeEventFragment
getEventFragmentById
getState
identify
optIn
optInToMarketing
optOut
optOutOfMarketing
resetConsentDecision
resetMarketingConsentDecision
setMarketingCampaignCookieId
trackErrorAsAnalytics
trackEvent
trackView
updateEventFragment
upsertEventFragment

AnalyticsController:createEventFragment​

Package: @metamask/analytics-controller

Create an event fragment.

A fragment accumulates properties across a user journey so that several parts of a client can contribute to the same set of events without re-deriving them. Declaring successEvent and failureEvent turns the fragment into a funnel that finalizeEventFragment closes. Declaring none of the event names makes it a pure property bag that the client reads back with getEventFragmentById when it emits its own events.

Any existing fragment with the same ID is replaced, so a new journey never inherits properties from a stale one.

Nothing is created unless the user is opted in, or undecided with the pre-consent queue enabled, so an opted-out user accumulates no fragment data.

Parameters:

NameDescription
optionsThe fragment definition. An ID is generated when one is not supplied.

Returns: A read-only copy of the created fragment, or undefined when the event fragments feature is disabled or the consent state does not allow capture. Mutating the returned object does not change controller state. Use updateEventFragment or upsertEventFragment to write.

Handler signature:

(options?: AnalyticsEventFragmentOptions) => ReadonlyAnalyticsEventFragment | undefined

AnalyticsController:deleteEventFragment​

Package: @metamask/analytics-controller

Discard an event fragment without emitting anything.

Parameters:

NameDescription
idThe fragment ID.

Handler signature:

(id: string) => void

AnalyticsController:finalizeEventFragment​

Package: @metamask/analytics-controller

Close an event fragment, emitting its closing event and discarding it.

The event emitted is failureEvent when the journey was abandoned and successEvent otherwise. A fragment that does not declare the relevant event name is discarded silently, which is what makes a pure property bag possible.

Parameters:

NameDescription
idThe fragment ID.
optionsFinalization options.
options.abandonedWhether the journey was abandoned.
options.contextContext merged over the fragment's own context.

Handler signature:

(id: string, { abandoned, context }?: AnalyticsEventFragmentFinalizeOptions) => void

AnalyticsController:getEventFragmentById​

Package: @metamask/analytics-controller

Read an event fragment.

Parameters:

NameDescription
idThe fragment ID.

Returns: A read-only copy of the fragment, or undefined when no fragment has that ID, the event fragments feature is disabled, or the consent state does not allow capture. Mutating the returned object does not change controller state. Use updateEventFragment or upsertEventFragment to write.

Handler signature:

(id: string) => ReadonlyAnalyticsEventFragment | undefined

AnalyticsController:getState​

Package: @metamask/analytics-controller

Returns the state of the AnalyticsController.

Handler signature:

() => AnalyticsControllerState

AnalyticsController:identify​

Package: @metamask/analytics-controller

Identify a user for analytics.

Parameters:

NameDescription
traitsUser traits/properties
contextOptional platform-specific context forwarded to the platform adapter.

Handler signature:

(traits?: AnalyticsUserTraits, context?: AnalyticsContext) => void

AnalyticsController:optIn​

Package: @metamask/analytics-controller

Opt in to analytics.

Records that a consent decision has been made and replays any events that were queued while the user was undecided.

When geolocation enrichment is enabled, geolocation is resolved here (once the user has consented) and awaited before the queued events are replayed, so those events are enriched with the resolved location as they are sent.

Returns: A promise that resolves once opt-in processing has completed.

Handler signature:

() => Promise<void>

AnalyticsController:optInToMarketing​

Package: @metamask/analytics-controller

Opt in to marketing analytics.

Independent of optIn. Replays queued marketing events.

Returns: A promise that resolves once opt-in processing has completed.

Handler signature:

() => Promise<void>

AnalyticsController:optOut​

Package: @metamask/analytics-controller

Opt out of analytics.

Records that a consent decision has been made and discards any persisted events and in-progress event fragments so nothing captured before the decision is ever delivered.

Handler signature:

() => void

AnalyticsController:optOutOfMarketing​

Package: @metamask/analytics-controller

Opt out of marketing analytics.

Independent of optOut. Discards queued marketing events and marketing event fragments.

Handler signature:

() => void

AnalyticsController:resetConsentDecision​

Package: @metamask/analytics-controller

Reset the consent decision back to undecided.

Intended for client flows that restart onboarding. Clears the opt-in preference and discards the delivery queue, but preserves any pre-consent events so they can still be replayed if the user opts in again. The user is treated as undecided again.

In-progress event fragments are kept only while the undecided user can still accumulate them, and discarded otherwise, so no fragment outlives the consent state that allowed it.

Handler signature:

() => void

AnalyticsController:resetMarketingConsentDecision​

Package: @metamask/analytics-controller

Reset the marketing consent decision back to undecided.

Independent of resetConsentDecision.

Handler signature:

() => void

AnalyticsController:setMarketingCampaignCookieId​

Package: @metamask/analytics-controller

Set the marketing campaign cookie ID.

Stores the ID of the marketing campaign cookie (e.g. a Google Analytics client ID) that was active when the user arrived. Pass null to clear it. The value is automatically cleared by optOutOfMarketing and resetMarketingConsentDecision.

Parameters:

NameDescription
marketingCampaignCookieIdThe marketing campaign cookie ID, or null to clear it.

Handler signature:

(marketingCampaignCookieId: string | null) => void

AnalyticsController:trackErrorAsAnalytics​

Package: @metamask/analytics-controller

Report a caught error as an Error Occurred analytics event.

Properties always include source and a sanitized error_message. Whitespace is collapsed and the message is capped at 500 characters. Stack traces are never included. Optional properties are merged in as well.

source must uniquely identify the error case (for example swap-quote-fetch). Do not reuse one value for every error in a file or package.

Sampling, a deduplication window, and a per-session cap bound volume. Those controls default to always emitting, 10 seconds, and 50 events. A caller can only lower the session cap. The budget belongs to this controller instance and is not persisted.

Delivery goes through trackEvent, so consent gating, the pre-consent queue, and geolocation enrichment all apply. Opted-out calls return before the volume controls run, so they do not consume the session budget.

Fail-safe: this method never throws. If delivery throws, the session reservation is released.

Parameters:

NameDescription
errorThe caught value to report.
optionsSource and optional volume controls.
options.sourceUnique discriminator for this error case.
options.volumeControlsOptional sampling, dedup, and session cap.
propertiesOptional extra properties.

Handler signature:

(error: unknown, options: TrackErrorAsAnalyticsOptions, properties?: AnalyticsEventProperties) => void

AnalyticsController:trackEvent​

Package: @metamask/analytics-controller

Track an analytics event.

Events are only tracked if analytics is enabled.

Parameters:

NameDescription
eventAnalytics event with properties and sensitive properties
contextOptional platform-specific context forwarded to the platform adapter.

Handler signature:

(event: AnalyticsTrackingEvent, context?: AnalyticsContext) => void

AnalyticsController:trackView​

Package: @metamask/analytics-controller

Track a page or screen view.

Parameters:

NameDescription
nameThe identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
propertiesOptional properties associated with the view
contextOptional platform-specific context forwarded to the platform adapter.

Handler signature:

(name: string, properties?: AnalyticsEventProperties, context?: AnalyticsContext) => void

AnalyticsController:updateEventFragment​

Package: @metamask/analytics-controller

Write to an existing event fragment.

Parameters:

NameDescription
idThe fragment ID.
payloadThe properties and context to merge in.

Handler signature:

(id: string, payload?: AnalyticsEventFragmentPayload) => void

AnalyticsController:upsertEventFragment​

Package: @metamask/analytics-controller

Write to an event fragment, creating a property bag if none exists.

This is the ergonomic entry point for contributors that do not know whether the journey has been started yet, and it avoids the read then write race a caller would otherwise have to implement itself.

Parameters:

NameDescription
idThe fragment ID.
payloadThe properties and context to merge in.

Handler signature:

(id: string, payload?: AnalyticsEventFragmentPayload) => void