From c9ef96d7ebe5063d66784670324a761337e497be Mon Sep 17 00:00:00 2001 From: Neil Kakkar Date: Tue, 21 Feb 2023 15:30:54 +0000 Subject: [PATCH] fix: Allow sending distinctID and fix bug with active flags (#83) --- posthog-node/CHANGELOG.md | 4 ++++ posthog-node/package.json | 2 +- posthog-node/src/posthog-node.ts | 11 +++++++---- posthog-node/src/types.ts | 1 + posthog-node/test/posthog-node.spec.ts | 25 ++++++++++++++++++++++++- 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/posthog-node/CHANGELOG.md b/posthog-node/CHANGELOG.md index beebf4d3..5e54967e 100644 --- a/posthog-node/CHANGELOG.md +++ b/posthog-node/CHANGELOG.md @@ -1,3 +1,7 @@ +# 2.5.3 - 2023-02-21 + +1. Allow passing in a distinctId to `groupIdentify()`. +2. Fix a bug with active feature flags on capture events, where non-active flags would be added to the list as well. # 2.5.2 - 2023-02-17 1. Fix issue where properties passed to `.identify` were not set correctly diff --git a/posthog-node/package.json b/posthog-node/package.json index a82f36ac..a32073e1 100644 --- a/posthog-node/package.json +++ b/posthog-node/package.json @@ -1,6 +1,6 @@ { "name": "posthog-node", - "version": "2.5.2", + "version": "2.5.3", "description": "PostHog Node.js integration", "repository": "PostHog/posthog-node", "scripts": { diff --git a/posthog-node/src/posthog-node.ts b/posthog-node/src/posthog-node.ts index 9e92549b..57f8315d 100644 --- a/posthog-node/src/posthog-node.ts +++ b/posthog-node/src/posthog-node.ts @@ -102,11 +102,14 @@ export class PostHog extends PostHogCoreStateless implements PostHogNodeV1 { const featureVariantProperties: Record = {} if (flags) { for (const [feature, variant] of Object.entries(flags)) { - featureVariantProperties[`$feature/${feature}`] = variant + if (variant !== false) { + featureVariantProperties[`$feature/${feature}`] = variant + } } } + const activeFlags = Object.keys(flags || {}).filter((flag) => flags?.[flag] !== false) const flagProperties = { - $active_feature_flags: flags ? Object.keys(flags) : undefined, + $active_feature_flags: activeFlags || undefined, ...featureVariantProperties, } _capture({ ...properties, $groups: groups, ...flagProperties }) @@ -332,8 +335,8 @@ export class PostHog extends PostHogCoreStateless implements PostHogNodeV1 { return { featureFlags, featureFlagPayloads } } - groupIdentify({ groupType, groupKey, properties }: GroupIdentifyMessage): void { - super.groupIdentifyStateless(groupType, groupKey, properties) + groupIdentify({ groupType, groupKey, properties, distinctId }: GroupIdentifyMessage): void { + super.groupIdentifyStateless(groupType, groupKey, properties, undefined, distinctId) } async reloadFeatureFlags(): Promise { diff --git a/posthog-node/src/types.ts b/posthog-node/src/types.ts index c1088709..81231663 100644 --- a/posthog-node/src/types.ts +++ b/posthog-node/src/types.ts @@ -16,6 +16,7 @@ export interface GroupIdentifyMessage { groupType: string groupKey: string // Unique identifier for the group properties?: Record + distinctId?: string // optional distinctId to associate message with a person } export type FeatureFlagCondition = { diff --git a/posthog-node/test/posthog-node.spec.ts b/posthog-node/test/posthog-node.spec.ts index 58541a99..d164345f 100644 --- a/posthog-node/test/posthog-node.spec.ts +++ b/posthog-node/test/posthog-node.spec.ts @@ -234,7 +234,6 @@ describe('PostHog Node.js', () => { posthog.groupIdentify({ groupType: 'posthog', groupKey: 'team-1', properties: { analytics: true } }) jest.runOnlyPendingTimers() const batchEvents = getLastBatchEvents() - console.log(batchEvents) expect(batchEvents).toMatchObject([ { distinct_id: '$posthog_team-1', @@ -248,6 +247,29 @@ describe('PostHog Node.js', () => { }, ]) }) + + it('should allow passing optional distinctID to identify group', () => { + posthog.groupIdentify({ + groupType: 'posthog', + groupKey: 'team-1', + properties: { analytics: true }, + distinctId: '123', + }) + jest.runOnlyPendingTimers() + const batchEvents = getLastBatchEvents() + expect(batchEvents).toMatchObject([ + { + distinct_id: '123', + event: '$groupidentify', + properties: { + $group_type: 'posthog', + $group_key: 'team-1', + $group_set: { analytics: true }, + $lib: 'posthog-node', + }, + }, + ]) + }) }) describe('feature flags', () => { @@ -256,6 +278,7 @@ describe('PostHog Node.js', () => { 'feature-1': true, 'feature-2': true, 'feature-variant': 'variant', + 'disabled-flag': false, } const mockFeatureFlagPayloads = {