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

fix: anonymous props that's not moved to sensitive props #12931

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions app/core/Analytics/MetricsEventBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ describe('MetricsEventBuilder', () => {
expect(rebuiltEvent.sensitiveProperties).toEqual(newSensitiveProps);
});

// test fix for https://github.com/MetaMask/metamask-mobile/issues/12728
it('adds properties with legacy sensitive properties objects', () => {
const newProps: JsonMap = {
newProp: 'newValue',
newSensitiveLegacyProp: { value: 'newSensitiveLegacyValue', anonymous: true}
};

const event = MetricsEventBuilder.createEventBuilder(mockEvent)
.addProperties(newProps)
.build();
expect(event.properties).toEqual({newProp: 'newValue'});
expect(event.sensitiveProperties).toEqual({newSensitiveLegacyProp: 'newSensitiveLegacyValue'});

const rebuiltEvent = MetricsEventBuilder.createEventBuilder(event)
.addProperties(newProps)
.build();
expect(rebuiltEvent.properties).toEqual({newProp: 'newValue'});
expect(rebuiltEvent.sensitiveProperties).toEqual({newSensitiveLegacyProp: 'newSensitiveLegacyValue'});
});

it('removes properties', () => {
const event = MetricsEventBuilder.createEventBuilder(mockEvent)
.addProperties({ newProp: 'newValue' })
Expand Down
32 changes: 31 additions & 1 deletion app/core/Analytics/MetricsEventBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,45 @@ class MetricsEventBuilder {
return new MetricsEventBuilder(event);
}

#getFilteredLegacyProperties = (properties: JsonMap) => {
const [filteredProps, anonymousProps] = Object.entries(properties).reduce(
([filtered, anonymous], [key, value]) => {
if (
typeof value === 'object' &&
value !== null &&
'anonymous' in value &&
value.anonymous === true
) {
// Add to anonymousProps map with the value of the `value` key
anonymous[key] = value.value;
} else {
// Add to filteredProps map
filtered[key] = value;
}
return [filtered, anonymous];
},
[{}, {}] as [JsonMap, JsonMap]
);
return [filteredProps, anonymousProps];
};

/**
* Add regular properties (non-anonymous) to the event
* @param properties a map of properties to add to the event
*/
addProperties(properties: JsonMap) {

const [filteredProps, anonymousProps] = this.#getFilteredLegacyProperties(properties);

this.#trackingEvent.properties = {
...this.#trackingEvent.properties,
...properties,
...filteredProps,
};

if (Object.keys(anonymousProps).length > 0) {
this.addSensitiveProperties(anonymousProps);
}

return this;
}

Expand Down
Loading