posthog-node 3.5.0 → 3.6.0
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 +4 -0
- package/lib/index.cjs.js +6 -4
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.d.ts +16 -12
- package/lib/index.esm.js +6 -4
- package/lib/index.esm.js.map +1 -1
- package/lib/posthog-core/src/index.d.ts +11 -11
- package/lib/posthog-core/src/types.d.ts +4 -1
- package/lib/posthog-node/src/posthog-node.d.ts +3 -3
- package/lib/posthog-node/src/types.d.ts +5 -4
- package/package.json +1 -1
- package/src/posthog-node.ts +14 -5
- package/src/types.ts +5 -4
- package/test/extensions/sentry-integration.spec.ts +1 -0
- package/test/posthog-node.spec.ts +20 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PostHogFetchOptions, PostHogFetchResponse, PostHogAutocaptureElement, PostHogDecideResponse, PosthogCoreOptions, PostHogEventProperties, PostHogPersistedProperty,
|
|
1
|
+
import { PostHogFetchOptions, PostHogFetchResponse, PostHogAutocaptureElement, PostHogDecideResponse, PosthogCoreOptions, PostHogEventProperties, PostHogPersistedProperty, PostHogCaptureOptions, JsonType } from './types';
|
|
2
2
|
import { RetriableOptions } from './utils';
|
|
3
3
|
export * as utils from './utils';
|
|
4
4
|
import { LZString } from './lz-string';
|
|
@@ -36,17 +36,17 @@ export declare abstract class PostHogCoreStateless {
|
|
|
36
36
|
/***
|
|
37
37
|
*** TRACKING
|
|
38
38
|
***/
|
|
39
|
-
protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?:
|
|
39
|
+
protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
|
|
40
40
|
protected captureStateless(distinctId: string, event: string, properties?: {
|
|
41
41
|
[key: string]: any;
|
|
42
|
-
}, options?:
|
|
42
|
+
}, options?: PostHogCaptureOptions): this;
|
|
43
43
|
protected aliasStateless(alias: string, distinctId: string, properties?: {
|
|
44
44
|
[key: string]: any;
|
|
45
|
-
}, options?:
|
|
45
|
+
}, options?: PostHogCaptureOptions): this;
|
|
46
46
|
/***
|
|
47
47
|
*** GROUPS
|
|
48
48
|
***/
|
|
49
|
-
protected groupIdentifyStateless(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?:
|
|
49
|
+
protected groupIdentifyStateless(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions, distinctId?: string, eventProperties?: PostHogEventProperties): this;
|
|
50
50
|
/***
|
|
51
51
|
*** FEATURE FLAGS
|
|
52
52
|
***/
|
|
@@ -63,7 +63,7 @@ export declare abstract class PostHogCoreStateless {
|
|
|
63
63
|
/***
|
|
64
64
|
*** QUEUEING AND FLUSHING
|
|
65
65
|
***/
|
|
66
|
-
protected enqueue(type: string, _message: any, options?:
|
|
66
|
+
protected enqueue(type: string, _message: any, options?: PostHogCaptureOptions): void;
|
|
67
67
|
flushAsync(): Promise<any>;
|
|
68
68
|
flush(callback?: (err?: any, data?: any) => void): void;
|
|
69
69
|
private fetchWithRetry;
|
|
@@ -101,20 +101,20 @@ export declare abstract class PostHogCore extends PostHogCoreStateless {
|
|
|
101
101
|
/***
|
|
102
102
|
*** TRACKING
|
|
103
103
|
***/
|
|
104
|
-
identify(distinctId?: string, properties?: PostHogEventProperties, options?:
|
|
104
|
+
identify(distinctId?: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
|
|
105
105
|
capture(event: string, properties?: {
|
|
106
106
|
[key: string]: any;
|
|
107
|
-
}, options?:
|
|
107
|
+
}, options?: PostHogCaptureOptions): this;
|
|
108
108
|
alias(alias: string): this;
|
|
109
|
-
autocapture(eventType: string, elements: PostHogAutocaptureElement[], properties?: PostHogEventProperties, options?:
|
|
109
|
+
autocapture(eventType: string, elements: PostHogAutocaptureElement[], properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
|
|
110
110
|
/***
|
|
111
111
|
*** GROUPS
|
|
112
112
|
***/
|
|
113
113
|
groups(groups: {
|
|
114
114
|
[type: string]: string | number;
|
|
115
115
|
}): this;
|
|
116
|
-
group(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?:
|
|
117
|
-
groupIdentify(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?:
|
|
116
|
+
group(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
|
|
117
|
+
groupIdentify(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
|
|
118
118
|
/***
|
|
119
119
|
* PROPERTIES
|
|
120
120
|
***/
|
|
@@ -45,7 +45,10 @@ export declare type PostHogFetchOptions = {
|
|
|
45
45
|
body?: string;
|
|
46
46
|
signal?: AbortSignal;
|
|
47
47
|
};
|
|
48
|
-
export declare type
|
|
48
|
+
export declare type PostHogCaptureOptions = {
|
|
49
|
+
/** If provided overrides the auto-generated event ID */
|
|
50
|
+
uuid?: string;
|
|
51
|
+
/** If provided overrides the auto-generated timestamp */
|
|
49
52
|
timestamp?: Date;
|
|
50
53
|
disableGeoip?: boolean;
|
|
51
54
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { JsonType, PosthogCoreOptions, PostHogCoreStateless, PostHogFetchOptions, PostHogFetchResponse, PosthogFlagsAndPayloadsResponse, PostHogPersistedProperty } from '../../posthog-core/src';
|
|
2
|
-
import {
|
|
2
|
+
import { EventMessage, GroupIdentifyMessage, IdentifyMessage, PostHogNodeV1 } from './types';
|
|
3
3
|
export declare type PostHogOptions = PosthogCoreOptions & {
|
|
4
4
|
persistence?: 'memory';
|
|
5
5
|
personalApiKey?: string;
|
|
@@ -24,8 +24,8 @@ export declare class PostHog extends PostHogCoreStateless implements PostHogNode
|
|
|
24
24
|
enable(): void;
|
|
25
25
|
disable(): void;
|
|
26
26
|
debug(enabled?: boolean): void;
|
|
27
|
-
capture({ distinctId, event, properties, groups, sendFeatureFlags, timestamp, disableGeoip }:
|
|
28
|
-
identify({ distinctId, properties, disableGeoip }:
|
|
27
|
+
capture({ distinctId, event, properties, groups, sendFeatureFlags, timestamp, disableGeoip, uuid, }: EventMessage): void;
|
|
28
|
+
identify({ distinctId, properties, disableGeoip }: IdentifyMessage): void;
|
|
29
29
|
alias(data: {
|
|
30
30
|
distinctId: string;
|
|
31
31
|
alias: string;
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { JsonType } from '../../posthog-core/src';
|
|
2
|
-
export interface
|
|
2
|
+
export interface IdentifyMessage {
|
|
3
3
|
distinctId: string;
|
|
4
4
|
properties?: Record<string | number, any>;
|
|
5
5
|
disableGeoip?: boolean;
|
|
6
6
|
}
|
|
7
|
-
export interface
|
|
7
|
+
export interface EventMessage extends IdentifyMessage {
|
|
8
8
|
event: string;
|
|
9
9
|
groups?: Record<string, string | number>;
|
|
10
10
|
sendFeatureFlags?: boolean;
|
|
11
11
|
timestamp?: Date;
|
|
12
|
+
uuid?: string;
|
|
12
13
|
}
|
|
13
14
|
export interface GroupIdentifyMessage {
|
|
14
15
|
groupType: string;
|
|
@@ -67,7 +68,7 @@ export declare type PostHogNodeV1 = {
|
|
|
67
68
|
* @param groups OPTIONAL | object of what groups are related to this event, example: { company: 'id:5' }. Can be used to analyze companies instead of users.
|
|
68
69
|
* @param sendFeatureFlags OPTIONAL | Used with experiments. Determines whether to send feature flag values with the event.
|
|
69
70
|
*/
|
|
70
|
-
capture({ distinctId, event, properties, groups, sendFeatureFlags }:
|
|
71
|
+
capture({ distinctId, event, properties, groups, sendFeatureFlags }: EventMessage): void;
|
|
71
72
|
/**
|
|
72
73
|
* @description Identify lets you add metadata on your users so you can more easily identify who they are in PostHog,
|
|
73
74
|
* and even do things like segment users by these properties.
|
|
@@ -75,7 +76,7 @@ export declare type PostHogNodeV1 = {
|
|
|
75
76
|
* @param distinctId which uniquely identifies your user
|
|
76
77
|
* @param properties with a dict with any key: value pairs
|
|
77
78
|
*/
|
|
78
|
-
identify({ distinctId, properties }:
|
|
79
|
+
identify({ distinctId, properties }: IdentifyMessage): void;
|
|
79
80
|
/**
|
|
80
81
|
* @description To marry up whatever a user does before they sign up or log in with what they do after you need to make an alias call.
|
|
81
82
|
* This will allow you to answer questions like "Which marketing channels leads to users churning after a month?"
|
package/package.json
CHANGED
package/src/posthog-node.ts
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
PostHogPersistedProperty,
|
|
11
11
|
} from '../../posthog-core/src'
|
|
12
12
|
import { PostHogMemoryStorage } from '../../posthog-core/src/storage-memory'
|
|
13
|
-
import {
|
|
13
|
+
import { EventMessage, GroupIdentifyMessage, IdentifyMessage, PostHogNodeV1 } from './types'
|
|
14
14
|
import { FeatureFlagsPoller } from './feature-flags'
|
|
15
15
|
import fetch from './fetch'
|
|
16
16
|
|
|
@@ -100,9 +100,18 @@ export class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
|
|
|
100
100
|
this.featureFlagsPoller?.debug(enabled)
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
-
capture({
|
|
104
|
-
|
|
105
|
-
|
|
103
|
+
capture({
|
|
104
|
+
distinctId,
|
|
105
|
+
event,
|
|
106
|
+
properties,
|
|
107
|
+
groups,
|
|
108
|
+
sendFeatureFlags,
|
|
109
|
+
timestamp,
|
|
110
|
+
disableGeoip,
|
|
111
|
+
uuid,
|
|
112
|
+
}: EventMessage): void {
|
|
113
|
+
const _capture = (props: EventMessage['properties']): void => {
|
|
114
|
+
super.captureStateless(distinctId, event, props, { timestamp, disableGeoip, uuid })
|
|
106
115
|
}
|
|
107
116
|
|
|
108
117
|
// :TRICKY: If we flush, or need to shut down, to not lose events we want this promise to resolve before we flush
|
|
@@ -155,7 +164,7 @@ export class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
|
|
|
155
164
|
this.addPendingPromise(capturePromise)
|
|
156
165
|
}
|
|
157
166
|
|
|
158
|
-
identify({ distinctId, properties, disableGeoip }:
|
|
167
|
+
identify({ distinctId, properties, disableGeoip }: IdentifyMessage): void {
|
|
159
168
|
// Catch properties passed as $set and move them to the top level
|
|
160
169
|
const personProperties = properties?.$set || properties
|
|
161
170
|
|
package/src/types.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { JsonType } from '../../posthog-core/src'
|
|
2
2
|
|
|
3
|
-
export interface
|
|
3
|
+
export interface IdentifyMessage {
|
|
4
4
|
distinctId: string
|
|
5
5
|
properties?: Record<string | number, any>
|
|
6
6
|
disableGeoip?: boolean
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
export interface
|
|
9
|
+
export interface EventMessage extends IdentifyMessage {
|
|
10
10
|
event: string
|
|
11
11
|
groups?: Record<string, string | number> // Mapping of group type to group id
|
|
12
12
|
sendFeatureFlags?: boolean
|
|
13
13
|
timestamp?: Date
|
|
14
|
+
uuid?: string
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
export interface GroupIdentifyMessage {
|
|
@@ -75,7 +76,7 @@ export type PostHogNodeV1 = {
|
|
|
75
76
|
* @param groups OPTIONAL | object of what groups are related to this event, example: { company: 'id:5' }. Can be used to analyze companies instead of users.
|
|
76
77
|
* @param sendFeatureFlags OPTIONAL | Used with experiments. Determines whether to send feature flag values with the event.
|
|
77
78
|
*/
|
|
78
|
-
capture({ distinctId, event, properties, groups, sendFeatureFlags }:
|
|
79
|
+
capture({ distinctId, event, properties, groups, sendFeatureFlags }: EventMessage): void
|
|
79
80
|
|
|
80
81
|
/**
|
|
81
82
|
* @description Identify lets you add metadata on your users so you can more easily identify who they are in PostHog,
|
|
@@ -84,7 +85,7 @@ export type PostHogNodeV1 = {
|
|
|
84
85
|
* @param distinctId which uniquely identifies your user
|
|
85
86
|
* @param properties with a dict with any key: value pairs
|
|
86
87
|
*/
|
|
87
|
-
identify({ distinctId, properties }:
|
|
88
|
+
identify({ distinctId, properties }: IdentifyMessage): void
|
|
88
89
|
|
|
89
90
|
/**
|
|
90
91
|
* @description To marry up whatever a user does before they sign up or log in with what they do after you need to make an alias call.
|
|
@@ -4,6 +4,7 @@ jest.mock('../src/fetch')
|
|
|
4
4
|
import fetch from '../src/fetch'
|
|
5
5
|
import { anyDecideCall, anyLocalEvalCall, apiImplementation } from './feature-flags.spec'
|
|
6
6
|
import { waitForPromises, wait } from '../../posthog-core/test/test-utils/test-utils'
|
|
7
|
+
import { generateUUID } from 'posthog-core/src/utils'
|
|
7
8
|
|
|
8
9
|
jest.mock('../package.json', () => ({ version: '1.2.3' }))
|
|
9
10
|
|
|
@@ -66,6 +67,7 @@ describe('PostHog Node.js', () => {
|
|
|
66
67
|
$lib: 'posthog-node',
|
|
67
68
|
$lib_version: '1.2.3',
|
|
68
69
|
},
|
|
70
|
+
uuid: expect.any(String),
|
|
69
71
|
timestamp: expect.any(String),
|
|
70
72
|
type: 'capture',
|
|
71
73
|
library: 'posthog-node',
|
|
@@ -185,6 +187,24 @@ describe('PostHog Node.js', () => {
|
|
|
185
187
|
distinct_id: '123',
|
|
186
188
|
timestamp: '2021-02-03T00:00:00.000Z',
|
|
187
189
|
event: 'custom-time',
|
|
190
|
+
uuid: expect.any(String),
|
|
191
|
+
},
|
|
192
|
+
])
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
it('should allow overriding uuid', async () => {
|
|
196
|
+
expect(mockedFetch).toHaveBeenCalledTimes(0)
|
|
197
|
+
const uuid = generateUUID()
|
|
198
|
+
posthog.capture({ event: 'custom-time', distinctId: '123', uuid })
|
|
199
|
+
await waitForPromises()
|
|
200
|
+
jest.runOnlyPendingTimers()
|
|
201
|
+
const batchEvents = getLastBatchEvents()
|
|
202
|
+
expect(batchEvents).toMatchObject([
|
|
203
|
+
{
|
|
204
|
+
distinct_id: '123',
|
|
205
|
+
timestamp: expect.any(String),
|
|
206
|
+
event: 'custom-time',
|
|
207
|
+
uuid: uuid,
|
|
188
208
|
},
|
|
189
209
|
])
|
|
190
210
|
})
|