Skip to content

Commit

Permalink
fix: Allow sending distinctID and fix bug with active flags (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar authored Feb 21, 2023
1 parent a182de8 commit c9ef96d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
4 changes: 4 additions & 0 deletions posthog-node/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion posthog-node/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
11 changes: 7 additions & 4 deletions posthog-node/src/posthog-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,14 @@ export class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
const featureVariantProperties: Record<string, string | boolean> = {}
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 })
Expand Down Expand Up @@ -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<void> {
Expand Down
1 change: 1 addition & 0 deletions posthog-node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface GroupIdentifyMessage {
groupType: string
groupKey: string // Unique identifier for the group
properties?: Record<string | number, any>
distinctId?: string // optional distinctId to associate message with a person
}

export type FeatureFlagCondition = {
Expand Down
25 changes: 24 additions & 1 deletion posthog-node/test/posthog-node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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', () => {
Expand All @@ -256,6 +278,7 @@ describe('PostHog Node.js', () => {
'feature-1': true,
'feature-2': true,
'feature-variant': 'variant',
'disabled-flag': false,
}

const mockFeatureFlagPayloads = {
Expand Down

0 comments on commit c9ef96d

Please sign in to comment.