From 5e9439dffce9ec8ed182f814662fc60d21f3e0f9 Mon Sep 17 00:00:00 2001 From: Harry Waye Date: Wed, 17 May 2023 07:56:22 +0100 Subject: [PATCH] fix(identify): actually send $set_once on identify calls (#629) We were handling $set but not $set_once. This fixes that. Fixes #615 --- src/__tests__/posthog-core.js | 22 ++++++++++++++++++++++ src/posthog-core.ts | 3 ++- src/types.ts | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/__tests__/posthog-core.js b/src/__tests__/posthog-core.js index 7840407e1..400906800 100644 --- a/src/__tests__/posthog-core.js +++ b/src/__tests__/posthog-core.js @@ -129,6 +129,28 @@ describe('capture()', () => { const event = given.subject() expect(event.properties.key.length).toBe(50000) }) + + it('passes through $set and $set_once into the request, if the event is an $identify event', () => { + // NOTE: this is slightly unusual to test capture for this specific case + // of being called with $identify as the event name. It might be that we + // decide that this shouldn't be a special case of capture in this case, + // but I'll add the case to capture current functionality. + // + // We check that if identify is called with user $set and $set_once + // properties, we also want to ensure capture does the expected thing + // with them. + const captureResult = given.lib.capture( + '$identify', + { distinct_id: 'some-distinct-id' }, + { $set: { email: 'john@example.com' }, $set_once: { howOftenAmISet: 'once!' } } + ) + + // We assume that the returned result is the object we would send to the + // server. + expect(captureResult).toEqual( + expect.objectContaining({ $set: { email: 'john@example.com' }, $set_once: { howOftenAmISet: 'once!' } }) + ) + }) }) describe('_calculate_event_properties()', () => { diff --git a/src/posthog-core.ts b/src/posthog-core.ts index 6aec9b05c..8f8957b02 100644 --- a/src/posthog-core.ts +++ b/src/posthog-core.ts @@ -846,8 +846,9 @@ export class PostHog { properties: this._calculate_event_properties(event_name, properties || {}), } - if (event_name === '$identify' && options.$set) { + if (event_name === '$identify') { data['$set'] = options['$set'] + data['$set_once'] = options['$set_once'] } data = _copyAndTruncateStrings( diff --git a/src/types.ts b/src/types.ts index 332d0e3d3..a010c7180 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,6 +9,7 @@ export interface CaptureResult { event: string properties: Properties $set?: Properties + $set_once?: Properties timestamp?: Date } export type CaptureCallback = (response: any, data: any) => void