PostHog Node.js SDK
SDK Version: 5.7.0
PostHog
Initialization methods
PostHog
public
Initialize a new PostHog client instance.
Parameters
Name | Type |
---|---|
apiKey | string |
Your PostHog project API key | |
options? | PostHogOptions |
Configuration options for the client |
Examples
Returns
Type |
---|
any |
debug
public
Enable or disable debug logging.
Parameters
Name | Type |
---|---|
enabled? | boolean |
Whether to enable debug logging |
Examples
Returns
Type |
---|
void |
getLibraryVersion
public
Get the library version from package.json.
Examples
Returns
Type |
---|
string |
getPersistedProperty
public
Get a persisted property value from memory storage.
Parameters
Name | Type |
---|---|
key | PostHogPersistedProperty |
The property key to retrieve |
Examples
Returns
Type | |
---|---|
Union of | any |
undefined |
setPersistedProperty
public
Set a persisted property value in memory storage.
Parameters
Name | Type |
---|---|
key | PostHogPersistedProperty |
The property key to set | |
value | any | null |
The value to store (null to remove) |
Examples
Returns
Type |
---|
void |
Capture methods
Examples
Returns
Type |
---|
void |
captureImmediate
public
Capture an event immediately (synchronously).
Parameters
Name | Type |
---|---|
props | EventMessage |
The event properties |
Examples
Returns
Type |
---|
Promise<void> |
Error tracking methods
captureException
public
Capture an error exception as an event.
Parameters
Name | Type |
---|---|
error | unknown |
The error to capture | |
distinctId? | string |
Optional user distinct ID | |
additionalProperties? | Record<string | number, any> |
Optional additional properties to include |
Examples
Returns
Type |
---|
void |
captureExceptionImmediate
public
Capture an error exception as an event immediately (synchronously).
Parameters
Name | Type |
---|---|
error | unknown |
The error to capture | |
distinctId? | string |
Optional user distinct ID | |
additionalProperties? | Record<string | number, any> |
Optional additional properties to include |
Examples
Returns
Type |
---|
Promise<void> |
Feature flags methods
getAllFlags
public
Get all feature flag values for a specific user.
Parameters
Name | Type |
---|---|
distinctId | string |
The user's distinct ID | |
options? | {
groups?: Record<string, string>;
personProperties?: Record<string, string>;
groupProperties?: Record<string, Record<string, string>>;
onlyEvaluateLocally?: boolean;
disableGeoip?: boolean;
flagKeys?: string[];
} |
Optional configuration for flag evaluation |
Examples
Returns
Type |
---|
Promise<Record<string, FeatureFlagValue>> |
getAllFlagsAndPayloads
public
Get all feature flag values and payloads for a specific user.
Parameters
Name | Type |
---|---|
distinctId | string |
The user's distinct ID | |
options? | {
groups?: Record<string, string>;
personProperties?: Record<string, string>;
groupProperties?: Record<string, Record<string, string>>;
onlyEvaluateLocally?: boolean;
disableGeoip?: boolean;
flagKeys?: string[];
} |
Optional configuration for flag evaluation |
Examples
Returns
Type |
---|
Promise<PostHogFlagsAndPayloadsResponse> |
getFeatureFlag
public
Get the value of a feature flag for a specific user.
Parameters
Name | Type |
---|---|
key | string |
The feature flag key | |
distinctId | string |
The user's distinct ID | |
options? | {
groups?: Record<string, string>;
personProperties?: Record<string, string>;
groupProperties?: Record<string, Record<string, string>>;
onlyEvaluateLocally?: boolean;
sendFeatureFlagEvents?: boolean;
disableGeoip?: boolean;
} |
Optional configuration for flag evaluation |
Examples
Returns
Type | |
---|---|
Union of | Promise<FeatureFlagValue |
undefined> |
getFeatureFlagPayload
public
Get the payload for a feature flag.
Parameters
Name | Type |
---|---|
key | string |
The feature flag key | |
distinctId | string |
The user's distinct ID | |
matchValue? | FeatureFlagValue |
Optional match value to get payload for | |
options? | {
groups?: Record<string, string>;
personProperties?: Record<string, string>;
groupProperties?: Record<string, Record<string, string>>;
onlyEvaluateLocally?: boolean;
sendFeatureFlagEvents?: boolean;
disableGeoip?: boolean;
} |
Optional configuration for flag evaluation |
Examples
Returns
Type | |
---|---|
Union of | Promise<JsonType |
undefined> |
getRemoteConfigPayload
public
Get the remote config payload for a feature flag.
Parameters
Name | Type |
---|---|
flagKey | string |
The feature flag key |
Examples
Returns
Type | |
---|---|
Union of | Promise<JsonType |
undefined> |
isFeatureEnabled
public
Check if a feature flag is enabled for a specific user.
Parameters
Name | Type |
---|---|
key | string |
The feature flag key | |
distinctId | string |
The user's distinct ID | |
options? | {
groups?: Record<string, string>;
personProperties?: Record<string, string>;
groupProperties?: Record<string, Record<string, string>>;
onlyEvaluateLocally?: boolean;
sendFeatureFlagEvents?: boolean;
disableGeoip?: boolean;
} |
Optional configuration for flag evaluation |
Examples
Returns
Type | |
---|---|
Union of | Promise<boolean |
undefined> |
isLocalEvaluationReady
public
Check if local evaluation of feature flags is ready.
Examples
Returns
Type |
---|
boolean |
reloadFeatureFlags
public
Reload feature flag definitions from the server for local evaluation.
Examples
Returns
Type |
---|
Promise<void> |
waitForLocalEvaluationReady
public
Wait for local evaluation of feature flags to be ready.
Parameters
Name | Type |
---|---|
timeoutMs? | number |
Timeout in milliseconds (default: 30000) |
Examples
Returns
Type |
---|
Promise<boolean> |
Identification methods
alias
public
Create an alias to link two distinct IDs together.
Parameters
Name | Type |
---|---|
data | {
distinctId: string;
alias: string;
disableGeoip?: boolean;
} |
The alias data containing distinctId and alias |
Examples
Returns
Type |
---|
void |
aliasImmediate
public
Create an alias to link two distinct IDs together immediately (synchronously).
Parameters
Name | Type |
---|---|
data | {
distinctId: string;
alias: string;
disableGeoip?: boolean;
} |
The alias data containing distinctId and alias |
Examples
Returns
Type |
---|
Promise<void> |
getCustomUserAgent
public
Get the custom user agent string for this client.
Examples
Returns
Type |
---|
string |
groupIdentify
public
Create or update a group and its properties.
Parameters
Name | Type |
---|---|
{ groupType, groupKey, properties, distinctId, disableGeoip } | GroupIdentifyMessage |
Examples
Returns
Type |
---|
void |
identify
public
Identify a user and set their properties.
Parameters
Name | Type |
---|---|
{ distinctId, properties, disableGeoip } | IdentifyMessage |
Examples
Returns
Type |
---|
void |
identifyImmediate
public
Identify a user and set their properties immediately (synchronously).
Parameters
Name | Type |
---|---|
{ distinctId, properties, disableGeoip } | IdentifyMessage |
Examples
Returns
Type |
---|
Promise<void> |
Privacy methods
disable
public
Disable the PostHog client (opt-out).
Examples
Returns
Type |
---|
Promise<void> |
enable
public
Enable the PostHog client (opt-in).
Examples
Returns
Type |
---|
Promise<void> |
Other methods
getLibraryId
public
Examples
Returns
Type |
---|
string |
addPendingPromise
public
Parameters
Name | Type |
---|---|
promise | Promise<T> |
Examples
Returns
Type |
---|
Promise<T> |
aliasStateless
public
Parameters
Name | Type |
---|---|
alias | string |
distinctId | string |
properties? | PostHogEventProperties |
options? | PostHogCaptureOptions |
Examples
Returns
Type |
---|
void |
aliasStatelessImmediate
public
Parameters
Name | Type |
---|---|
alias | string |
distinctId | string |
properties? | PostHogEventProperties |
options? | PostHogCaptureOptions |
Examples
Returns
Type |
---|
Promise<void> |
captureStateless
public
Parameters
Name | Type |
---|---|
distinctId | string |
event | string |
properties? | PostHogEventProperties |
options? | PostHogCaptureOptions |
Examples
Returns
Type |
---|
void |
captureStatelessImmediate
public
Parameters
Name | Type |
---|---|
distinctId | string |
event | string |
properties? | PostHogEventProperties |
options? | PostHogCaptureOptions |
Examples
Returns
Type |
---|
Promise<void> |
enqueue
public
- ** QUEUEING AND FLUSHING *
Parameters
Name | Type |
---|---|
type | string |
_message | any |
options? | PostHogCaptureOptions |
Examples
Returns
Type |
---|
void |
fetch
public
Parameters
Name | Type |
---|---|
url | string |
options | PostHogFetchOptions |
Examples
Returns
Type |
---|
Promise<PostHogFetchResponse> |
flush
public
Flushes the queue
This function will return a promise that will resolve when the flush is complete, or reject if there was an error (for example if the server or network is down).
If there is already a flush in progress, this function will wait for that flush to complete.
It's recommended to do error handling in the callback of the promise.
Examples
Returns
Type |
---|
Promise<void> |
getCommonEventProperties
public
Examples
Returns
getCustomHeaders
public
Examples
Returns
Type |
---|
{
[key: string]: string;
} |
getFeatureFlagDetailsStateless
public
Parameters
Name | Type |
---|---|
distinctId | string |
groups? | Record<string, string | number> |
personProperties? | Record<string, string> |
groupProperties? | Record<string, Record<string, string>> |
disableGeoip? | boolean |
flagKeysToEvaluate? | string[] |
Examples
Returns
Type | |
---|---|
Union of | Promise<PostHogFeatureFlagDetails |
undefined> |
getFeatureFlagDetailStateless
public
Parameters
Name | Type |
---|---|
key | string |
distinctId | string |
groups? | Record<string, string> |
personProperties? | Record<string, string> |
groupProperties? | Record<string, Record<string, string>> |
disableGeoip? | boolean |
Examples
Returns
Type | |
---|---|
Union of | Promise<{
response: FeatureFlagDetail |
undefined;
requestId: string | |
undefined;
} | |
undefined> |
getFeatureFlagPayloadsStateless
public
Parameters
Name | Type |
---|---|
distinctId | string |
groups? | Record<string, string> |
personProperties? | Record<string, string> |
groupProperties? | Record<string, Record<string, string>> |
disableGeoip? | boolean |
flagKeysToEvaluate? | string[] |
Examples
Returns
Type | |
---|---|
Union of | Promise<PostHogFlagsResponse['featureFlagPayloads'] |
undefined> |
getFeatureFlagPayloadStateless
public
Parameters
Name | Type |
---|---|
key | string |
distinctId | string |
groups? | Record<string, string> |
personProperties? | Record<string, string> |
groupProperties? | Record<string, Record<string, string>> |
disableGeoip? | boolean |
Examples
Returns
Type | |
---|---|
Union of | Promise<JsonType |
undefined> |
getFeatureFlagsAndPayloadsStateless
public
Parameters
Name | Type |
---|---|
distinctId | string |
groups? | Record<string, string | number> |
personProperties? | Record<string, string> |
groupProperties? | Record<string, Record<string, string>> |
disableGeoip? | boolean |
flagKeysToEvaluate? | string[] |
Examples
Returns
Type | |
---|---|
Union of | Promise<{
flags: PostHogFlagsResponse['featureFlags'] |
undefined;
payloads: PostHogFlagsResponse['featureFlagPayloads'] | |
undefined;
requestId: PostHogFlagsResponse['requestId'] | |
undefined;
}> |
getFeatureFlagsStateless
public
Parameters
Name | Type |
---|---|
distinctId | string |
groups? | Record<string, string | number> |
personProperties? | Record<string, string> |
groupProperties? | Record<string, Record<string, string>> |
disableGeoip? | boolean |
flagKeysToEvaluate? | string[] |
Examples
Returns
Type | |
---|---|
Union of | Promise<{
flags: PostHogFlagsResponse['featureFlags'] |
undefined;
payloads: PostHogFlagsResponse['featureFlagPayloads'] | |
undefined;
requestId: PostHogFlagsResponse['requestId'] | |
undefined;
}> |
getFeatureFlagStateless
public
Parameters
Name | Type |
---|---|
key | string |
distinctId | string |
groups? | Record<string, string> |
personProperties? | Record<string, string> |
groupProperties? | Record<string, Record<string, string>> |
disableGeoip? | boolean |
Examples
Returns
Type | |
---|---|
Union of | Promise<{
response: FeatureFlagValue |
undefined;
requestId: string | |
undefined;
}> |
getFlags
public
- ** FEATURE FLAGS *
Parameters
Name | Type |
---|---|
distinctId | string |
groups? | Record<string, string | number> |
personProperties? | Record<string, string> |
groupProperties? | Record<string, Record<string, string>> |
extraPayload? | Record<string, any> |
Examples
Returns
Type | |
---|---|
Union of | Promise<PostHogFlagsResponse |
undefined> |
getRemoteConfig
public
Examples
Returns
Type | |
---|---|
Union of | Promise<PostHogRemoteConfig |
undefined> |
getSurveysStateless
public
- ** SURVEYS *
Examples
Returns
Type |
---|
Promise<SurveyResponse['surveys']> |
groupIdentifyStateless
public
- ** GROUPS *
Parameters
Name | Type |
---|---|
groupType | string |
groupKey | string | number |
groupProperties? | PostHogEventProperties |
options? | PostHogCaptureOptions |
distinctId? | string |
eventProperties? | PostHogEventProperties |
Examples
Returns
Type |
---|
void |
identifyStateless
public
- ** TRACKING *
Parameters
Name | Type |
---|---|
distinctId | string |
properties? | PostHogEventProperties |
options? | PostHogCaptureOptions |
Examples
Returns
Type |
---|
void |
identifyStatelessImmediate
public
Parameters
Name | Type |
---|---|
distinctId | string |
properties? | PostHogEventProperties |
options? | PostHogCaptureOptions |
Examples
Returns
Type |
---|
Promise<void> |
logMsgIfDebug
public
Parameters
Name | Type |
---|---|
fn | () => void |
Examples
Returns
Type |
---|
void |
on
public
Parameters
Name | Type |
---|---|
event | string |
cb | (...args: any[]) => void |
Examples
Returns
Type |
---|
() => void |
optIn
public
Examples
Returns
Type |
---|
Promise<void> |
optOut
public
Examples
Returns
Type |
---|
Promise<void> |
register
public
Parameters
Name | Type |
---|---|
properties | PostHogEventProperties |
Examples
Returns
Type |
---|
Promise<void> |
sendImmediate
public
Parameters
Name | Type |
---|---|
type | string |
_message | any |
options? | PostHogCaptureOptions |
Examples
Returns
Type |
---|
Promise<void> |
shutdown
public
Call shutdown() once before the node process exits, so ensure that all events have been sent and all promises have resolved. Do not use this function if you intend to keep using this PostHog instance after calling it.
Parameters
Name | Type |
---|---|
shutdownTimeoutMs? | number |
Examples
Returns
Type |
---|
Promise<void> |
unregister
public
Parameters
Name | Type |
---|---|
property | string |
Examples
Returns
Type |
---|
Promise<void> |
wrap
public
Parameters
Name | Type |
---|---|
fn | () => void |
Examples
Returns
Type |
---|
void |