diff --git a/app/core/Analytics/MetricsEventBuilder.test.ts b/app/core/Analytics/MetricsEventBuilder.test.ts index 2fdf2ebc480..38b7bfe75a9 100644 --- a/app/core/Analytics/MetricsEventBuilder.test.ts +++ b/app/core/Analytics/MetricsEventBuilder.test.ts @@ -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' }) diff --git a/app/core/Analytics/MetricsEventBuilder.ts b/app/core/Analytics/MetricsEventBuilder.ts index 79fd443d0bc..ccf6093f58d 100644 --- a/app/core/Analytics/MetricsEventBuilder.ts +++ b/app/core/Analytics/MetricsEventBuilder.ts @@ -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; }