posthog-node 2.5.1 → 2.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/lib/index.cjs.js +19 -7
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.d.ts +2 -1
- package/lib/index.esm.js +19 -7
- package/lib/index.esm.js.map +1 -1
- package/lib/posthog-node/src/posthog-node.d.ts +1 -1
- package/lib/posthog-node/src/types.d.ts +1 -0
- package/package.json +1 -1
- package/src/posthog-node.ts +13 -5
- package/src/types.ts +1 -0
- package/test/posthog-node.spec.ts +45 -2
|
@@ -62,7 +62,7 @@ export declare class PostHog extends PostHogCoreStateless implements PostHogNode
|
|
|
62
62
|
groupProperties?: Record<string, Record<string, string>>;
|
|
63
63
|
onlyEvaluateLocally?: boolean;
|
|
64
64
|
}): Promise<PosthogFlagsAndPayloadsResponse>;
|
|
65
|
-
groupIdentify({ groupType, groupKey, properties }: GroupIdentifyMessage): void;
|
|
65
|
+
groupIdentify({ groupType, groupKey, properties, distinctId }: GroupIdentifyMessage): void;
|
|
66
66
|
reloadFeatureFlags(): Promise<void>;
|
|
67
67
|
shutdown(): void;
|
|
68
68
|
shutdownAsync(): Promise<void>;
|
package/package.json
CHANGED
package/src/posthog-node.ts
CHANGED
|
@@ -102,11 +102,14 @@ export class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
|
|
|
102
102
|
const featureVariantProperties: Record<string, string | boolean> = {}
|
|
103
103
|
if (flags) {
|
|
104
104
|
for (const [feature, variant] of Object.entries(flags)) {
|
|
105
|
-
|
|
105
|
+
if (variant !== false) {
|
|
106
|
+
featureVariantProperties[`$feature/${feature}`] = variant
|
|
107
|
+
}
|
|
106
108
|
}
|
|
107
109
|
}
|
|
110
|
+
const activeFlags = Object.keys(flags || {}).filter((flag) => flags?.[flag] !== false)
|
|
108
111
|
const flagProperties = {
|
|
109
|
-
$active_feature_flags:
|
|
112
|
+
$active_feature_flags: activeFlags || undefined,
|
|
110
113
|
...featureVariantProperties,
|
|
111
114
|
}
|
|
112
115
|
_capture({ ...properties, $groups: groups, ...flagProperties })
|
|
@@ -117,7 +120,12 @@ export class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
|
|
|
117
120
|
}
|
|
118
121
|
|
|
119
122
|
identify({ distinctId, properties }: IdentifyMessageV1): void {
|
|
120
|
-
|
|
123
|
+
// Catch properties passed as $set and move them to the top level
|
|
124
|
+
const personProperties = properties?.$set || properties
|
|
125
|
+
|
|
126
|
+
super.identifyStateless(distinctId, {
|
|
127
|
+
$set: personProperties,
|
|
128
|
+
})
|
|
121
129
|
}
|
|
122
130
|
|
|
123
131
|
alias(data: { distinctId: string; alias: string }): void {
|
|
@@ -327,8 +335,8 @@ export class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
|
|
|
327
335
|
return { featureFlags, featureFlagPayloads }
|
|
328
336
|
}
|
|
329
337
|
|
|
330
|
-
groupIdentify({ groupType, groupKey, properties }: GroupIdentifyMessage): void {
|
|
331
|
-
super.groupIdentifyStateless(groupType, groupKey, properties)
|
|
338
|
+
groupIdentify({ groupType, groupKey, properties, distinctId }: GroupIdentifyMessage): void {
|
|
339
|
+
super.groupIdentifyStateless(groupType, groupKey, properties, undefined, distinctId)
|
|
332
340
|
}
|
|
333
341
|
|
|
334
342
|
async reloadFeatureFlags(): Promise<void> {
|
package/src/types.ts
CHANGED
|
@@ -16,6 +16,7 @@ export interface GroupIdentifyMessage {
|
|
|
16
16
|
groupType: string
|
|
17
17
|
groupKey: string // Unique identifier for the group
|
|
18
18
|
properties?: Record<string | number, any>
|
|
19
|
+
distinctId?: string // optional distinctId to associate message with a person
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
export type FeatureFlagCondition = {
|
|
@@ -114,7 +114,27 @@ describe('PostHog Node.js', () => {
|
|
|
114
114
|
distinct_id: '123',
|
|
115
115
|
event: '$identify',
|
|
116
116
|
properties: {
|
|
117
|
-
|
|
117
|
+
$set: {
|
|
118
|
+
foo: 'bar',
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
])
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
it('should handle identify mistakenly using $set', async () => {
|
|
126
|
+
expect(mockedFetch).toHaveBeenCalledTimes(0)
|
|
127
|
+
posthog.identify({ distinctId: '123', properties: { foo: 'bar', $set: { foo: 'other' } } })
|
|
128
|
+
jest.runOnlyPendingTimers()
|
|
129
|
+
const batchEvents = getLastBatchEvents()
|
|
130
|
+
expect(batchEvents).toMatchObject([
|
|
131
|
+
{
|
|
132
|
+
distinct_id: '123',
|
|
133
|
+
event: '$identify',
|
|
134
|
+
properties: {
|
|
135
|
+
$set: {
|
|
136
|
+
foo: 'other',
|
|
137
|
+
},
|
|
118
138
|
},
|
|
119
139
|
},
|
|
120
140
|
])
|
|
@@ -214,7 +234,6 @@ describe('PostHog Node.js', () => {
|
|
|
214
234
|
posthog.groupIdentify({ groupType: 'posthog', groupKey: 'team-1', properties: { analytics: true } })
|
|
215
235
|
jest.runOnlyPendingTimers()
|
|
216
236
|
const batchEvents = getLastBatchEvents()
|
|
217
|
-
console.log(batchEvents)
|
|
218
237
|
expect(batchEvents).toMatchObject([
|
|
219
238
|
{
|
|
220
239
|
distinct_id: '$posthog_team-1',
|
|
@@ -228,6 +247,29 @@ describe('PostHog Node.js', () => {
|
|
|
228
247
|
},
|
|
229
248
|
])
|
|
230
249
|
})
|
|
250
|
+
|
|
251
|
+
it('should allow passing optional distinctID to identify group', () => {
|
|
252
|
+
posthog.groupIdentify({
|
|
253
|
+
groupType: 'posthog',
|
|
254
|
+
groupKey: 'team-1',
|
|
255
|
+
properties: { analytics: true },
|
|
256
|
+
distinctId: '123',
|
|
257
|
+
})
|
|
258
|
+
jest.runOnlyPendingTimers()
|
|
259
|
+
const batchEvents = getLastBatchEvents()
|
|
260
|
+
expect(batchEvents).toMatchObject([
|
|
261
|
+
{
|
|
262
|
+
distinct_id: '123',
|
|
263
|
+
event: '$groupidentify',
|
|
264
|
+
properties: {
|
|
265
|
+
$group_type: 'posthog',
|
|
266
|
+
$group_key: 'team-1',
|
|
267
|
+
$group_set: { analytics: true },
|
|
268
|
+
$lib: 'posthog-node',
|
|
269
|
+
},
|
|
270
|
+
},
|
|
271
|
+
])
|
|
272
|
+
})
|
|
231
273
|
})
|
|
232
274
|
|
|
233
275
|
describe('feature flags', () => {
|
|
@@ -236,6 +278,7 @@ describe('PostHog Node.js', () => {
|
|
|
236
278
|
'feature-1': true,
|
|
237
279
|
'feature-2': true,
|
|
238
280
|
'feature-variant': 'variant',
|
|
281
|
+
'disabled-flag': false,
|
|
239
282
|
}
|
|
240
283
|
|
|
241
284
|
const mockFeatureFlagPayloads = {
|