Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure user traits set before opting in to participate in metametrics… #29871

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions app/scripts/controllers/metametrics-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@
persist: true,
anonymous: false,
},
userTraitsBeforeMetricsOptIn: {
persist: true,
anonymous: false,
},
traits: {
persist: true,
anonymous: false,
Expand Down Expand Up @@ -248,6 +252,7 @@
latestNonAnonymousEventTimestamp: number;
fragments: Record<string, MetaMetricsEventFragment>;
eventsBeforeMetricsOptIn: MetaMetricsEventPayload[];
userTraitsBeforeMetricsOptIn: MetaMetricsUserTraits[];
traits: MetaMetricsUserTraits;
previousUserTraits?: MetaMetricsUserTraits;
dataCollectionForMarketing: boolean | null;
Expand Down Expand Up @@ -335,6 +340,7 @@
marketingCampaignCookieId: null,
latestNonAnonymousEventTimestamp: 0,
eventsBeforeMetricsOptIn: [],
userTraitsBeforeMetricsOptIn: [],
traits: {},
previousUserTraits: {},
fragments: {},
Expand Down Expand Up @@ -754,6 +760,11 @@
identify(userTraits: Partial<MetaMetricsUserTraits>): void {
const { metaMetricsId, participateInMetaMetrics } = this.state;

if (participateInMetaMetrics === null && metaMetricsId === null ) {

Check failure on line 763 in app/scripts/controllers/metametrics-controller.ts

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

Delete `·`
this.addUserTraitsBeforeMetricsOptIn(userTraits);
return;
}

if (!participateInMetaMetrics || !metaMetricsId || !userTraits) {
return;
}
Expand Down Expand Up @@ -820,6 +831,7 @@

if (participateInMetaMetrics) {
this.trackEventsAfterMetricsOptIn();
this.trackUserTraitsAfterMetricsOptIn();
this.clearEventsAfterMetricsOptIn();
} else if (this.state.marketingCampaignCookieId) {
this.setMarketingCampaignCookieId(null);
Expand Down Expand Up @@ -1019,6 +1031,13 @@
});
}

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) => {
Expand All @@ -1033,6 +1052,12 @@
});
}

addUserTraitsBeforeMetricsOptIn(userTraits: MetaMetricsUserTraits): void {
this.update((state) => {
state.userTraitsBeforeMetricsOptIn.push(userTraits);
});
}

// Add or update traits for tracking.
updateTraits(newTraits: MetaMetricsUserTraits): void {
this.update((state) => {
Expand Down
Loading