diff --git a/app/scripts/controllers/metametrics-controller.ts b/app/scripts/controllers/metametrics-controller.ts index 9668a9f25b09..0ce243059843 100644 --- a/app/scripts/controllers/metametrics-controller.ts +++ b/app/scripts/controllers/metametrics-controller.ts @@ -205,6 +205,10 @@ const controllerMetadata = { persist: true, anonymous: false, }, + userTraitsBeforeMetricsOptIn: { + persist: true, + anonymous: false, + }, traits: { persist: true, anonymous: false, @@ -248,6 +252,7 @@ export type MetaMetricsControllerState = { latestNonAnonymousEventTimestamp: number; fragments: Record; eventsBeforeMetricsOptIn: MetaMetricsEventPayload[]; + userTraitsBeforeMetricsOptIn: MetaMetricsUserTraits[]; traits: MetaMetricsUserTraits; previousUserTraits?: MetaMetricsUserTraits; dataCollectionForMarketing: boolean | null; @@ -335,6 +340,7 @@ export const getDefaultMetaMetricsControllerState = marketingCampaignCookieId: null, latestNonAnonymousEventTimestamp: 0, eventsBeforeMetricsOptIn: [], + userTraitsBeforeMetricsOptIn: [], traits: {}, previousUserTraits: {}, fragments: {}, @@ -754,6 +760,11 @@ export default class MetaMetricsController extends BaseController< identify(userTraits: Partial): void { const { metaMetricsId, participateInMetaMetrics } = this.state; + if (participateInMetaMetrics === null && metaMetricsId === null ) { + this.addUserTraitsBeforeMetricsOptIn(userTraits); + return; + } + if (!participateInMetaMetrics || !metaMetricsId || !userTraits) { return; } @@ -820,6 +831,7 @@ export default class MetaMetricsController extends BaseController< if (participateInMetaMetrics) { this.trackEventsAfterMetricsOptIn(); + this.trackUserTraitsAfterMetricsOptIn(); this.clearEventsAfterMetricsOptIn(); } else if (this.state.marketingCampaignCookieId) { this.setMarketingCampaignCookieId(null); @@ -1019,6 +1031,13 @@ export default class MetaMetricsController extends BaseController< }); } + trackUserTraitsAfterMetricsOptIn(): void { + const { userTraitsBeforeMetricsOptIn } = this.state; + userTraitsBeforeMetricsOptIn.forEach((userTraitBeforeMetricsOptIn) => { + this.#identify(userTraitBeforeMetricsOptIn); + }); + } + // Once we track queued events after a user opts into metrics, we want to clear the event queue. clearEventsAfterMetricsOptIn(): void { this.update((state) => { @@ -1033,6 +1052,12 @@ export default class MetaMetricsController extends BaseController< }); } + addUserTraitsBeforeMetricsOptIn(userTraits: MetaMetricsUserTraits): void { + this.update((state) => { + state.userTraitsBeforeMetricsOptIn.push(userTraits); + }); + } + // Add or update traits for tracking. updateTraits(newTraits: MetaMetricsUserTraits): void { this.update((state) => {