From 52a7aea09dcdb1c04542cc9b441ea241ccb2e2e3 Mon Sep 17 00:00:00 2001 From: Michael Matloka Date: Tue, 3 Oct 2023 23:30:04 +0200 Subject: [PATCH] fix(capture): Always update stored person props from $set --- src/__tests__/posthog-core.js | 17 +++++++++++++++++ src/posthog-core.ts | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/src/__tests__/posthog-core.js b/src/__tests__/posthog-core.js index 4991c86cb..3afe3a190 100644 --- a/src/__tests__/posthog-core.js +++ b/src/__tests__/posthog-core.js @@ -43,6 +43,11 @@ describe('capture()', () => { remove_event_timer: jest.fn(), properties: jest.fn(), update_config: jest.fn(), + register(properties) { + // Simplified version of the real thing + Object.assign(this.props, properties) + }, + props: {}, }, sessionPersistence: { update_search_keyword: jest.fn(), @@ -169,6 +174,18 @@ describe('capture()', () => { ) }) + it('updates persisted person properties for feature flags if $set is present', () => { + given('config', () => ({ + property_blacklist: [], + _onCapture: jest.fn(), + })) + given('eventProperties', () => ({ + $set: { foo: 'bar' }, + })) + given.subject() + expect(given.overrides.persistence.props.$stored_person_properties).toMatchObject({ foo: 'bar' }) + }) + it('correctly handles the "length" property', () => { const captureResult = given.lib.capture('event-name', { foo: 'bar', length: 0 }) expect(captureResult.properties).toEqual(expect.objectContaining({ foo: 'bar', length: 0 })) diff --git a/src/posthog-core.ts b/src/posthog-core.ts index 58b39b775..9f8dbf11d 100644 --- a/src/posthog-core.ts +++ b/src/posthog-core.ts @@ -7,6 +7,7 @@ import { _info, _isArray, _isBlockedUA, + _isEmptyObject, _isObject, _isUndefined, _register_event, @@ -881,6 +882,13 @@ export class PostHog { data = _copyAndTruncateStrings(data, options._noTruncate ? null : this.config.properties_string_max_length) data.timestamp = options.timestamp || new Date() + // Top-level $set overriding values from the one from properties is taken from the plugin-server normalizeEvent + // This doesn't handle $set_once, because posthog-people doesn't either + const finalSet = { ...data.properties['$set'], ...data['$set'] } + if (!_isEmptyObject(finalSet)) { + this.setPersonPropertiesForFlags(finalSet) + } + if (this.config.debug) { logger.log('PostHog.js send', data) }