Skip to content

Commit

Permalink
fix(identify): actually send $set_once on identify calls (#629)
Browse files Browse the repository at this point in the history
We were handling $set but not $set_once. This fixes that.

Fixes #615
  • Loading branch information
hazzadous committed May 17, 2023
1 parent da76ead commit 5e9439d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/__tests__/posthog-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5e9439d

Please sign in to comment.