posthog-node 2.5.0 → 2.5.2

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.
@@ -11,6 +11,7 @@ export declare abstract class PostHogCoreStateless {
11
11
  private requestTimeout;
12
12
  private captureMode;
13
13
  private removeDebugCallback?;
14
+ private pendingPromises;
14
15
  private _optoutOverride;
15
16
  protected _events: SimpleEventEmitter;
16
17
  protected _flushTimer?: any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "posthog-node",
3
- "version": "2.5.0",
3
+ "version": "2.5.2",
4
4
  "description": "PostHog Node.js integration",
5
5
  "repository": "PostHog/posthog-node",
6
6
  "scripts": {
@@ -117,7 +117,12 @@ export class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
117
117
  }
118
118
 
119
119
  identify({ distinctId, properties }: IdentifyMessageV1): void {
120
- super.identifyStateless(distinctId, properties)
120
+ // Catch properties passed as $set and move them to the top level
121
+ const personProperties = properties?.$set || properties
122
+
123
+ super.identifyStateless(distinctId, {
124
+ $set: personProperties,
125
+ })
121
126
  }
122
127
 
123
128
  alias(data: { distinctId: string; alias: string }): void {
@@ -3,7 +3,7 @@ import { PostHog as PostHog } from '../src/posthog-node'
3
3
  jest.mock('../src/fetch')
4
4
  import { fetch } from '../src/fetch'
5
5
  import { anyDecideCall, anyLocalEvalCall, apiImplementation } from './feature-flags.spec'
6
- import { waitForPromises } from '../../posthog-core/test/test-utils/test-utils'
6
+ import { waitForPromises, wait } from '../../posthog-core/test/test-utils/test-utils'
7
7
 
8
8
  jest.mock('../package.json', () => ({ version: '1.2.3' }))
9
9
 
@@ -114,7 +114,27 @@ describe('PostHog Node.js', () => {
114
114
  distinct_id: '123',
115
115
  event: '$identify',
116
116
  properties: {
117
- foo: 'bar',
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
  ])
@@ -152,6 +172,63 @@ describe('PostHog Node.js', () => {
152
172
  })
153
173
  })
154
174
 
175
+ describe('shutdown', () => {
176
+ beforeEach(() => {
177
+ // a serverless posthog configuration
178
+ posthog = new PostHog('TEST_API_KEY', {
179
+ host: 'http://example.com',
180
+ flushAt: 1,
181
+ flushInterval: 0,
182
+ })
183
+
184
+ mockedFetch.mockImplementation(async () => {
185
+ // simulate network delay
186
+ await wait(500)
187
+
188
+ return Promise.resolve({
189
+ status: 200,
190
+ text: () => Promise.resolve('ok'),
191
+ json: () =>
192
+ Promise.resolve({
193
+ status: 'ok',
194
+ }),
195
+ } as any)
196
+ })
197
+ })
198
+
199
+ afterEach(() => {
200
+ posthog.debug(false)
201
+ })
202
+
203
+ it('should shutdown cleanly', async () => {
204
+ const logSpy = jest.spyOn(global.console, 'log')
205
+ jest.useRealTimers()
206
+ // using debug mode to check console.log output
207
+ // which tells us when the flush is complete
208
+ posthog.debug(true)
209
+ for (let i = 0; i < 10; i++) {
210
+ posthog.capture({ event: 'test-event', distinctId: '123' })
211
+ // requests come 100ms apart
212
+ await wait(100)
213
+ }
214
+
215
+ // 10 capture calls to debug log
216
+ // 6 flush calls to debug log
217
+ expect(logSpy).toHaveBeenCalledTimes(16)
218
+ expect(10).toEqual(logSpy.mock.calls.filter((call) => call[1].includes('capture')).length)
219
+ expect(6).toEqual(logSpy.mock.calls.filter((call) => call[1].includes('flush')).length)
220
+
221
+ logSpy.mockClear()
222
+
223
+ await posthog.shutdownAsync()
224
+ // remaining 4 flush calls to debug log
225
+ // happen during shutdown
226
+ expect(4).toEqual(logSpy.mock.calls.filter((call) => call[1].includes('flush')).length)
227
+ jest.useFakeTimers()
228
+ logSpy.mockRestore()
229
+ })
230
+ })
231
+
155
232
  describe('groupIdentify', () => {
156
233
  it('should identify group with unique id', () => {
157
234
  posthog.groupIdentify({ groupType: 'posthog', groupKey: 'team-1', properties: { analytics: true } })