PostHog React Native SDK
SDK Version:
PostHog
Initialization methods
PostHogProviderpublic
PostHogProvider is a React component that provides PostHog functionality to your React Native app. You can find all configuration options in the React Native SDK docs.
Autocapturing navigation requires further configuration. See the React Native SDK navigation docs for more information about autocapturing navigation.
This is the recommended way to set up PostHog for React Native. This utilizes the Context API to pass the PostHog client around, enable autocapture.
Parameters
| Name | Type |
|---|---|
{ children, client, options, apiKey, autocapture, style, debug, } | PostHogProviderProps |
Examples
Returns
| Type | |
|---|---|
| Union of | JSX.Element |
null |
PostHogpublic
Creates a new PostHog instance for React Native. You can find all configuration options in the React Native SDK docs.
If you prefer not to use the PostHogProvider, you can initialize PostHog in its own file and import the instance from there.
Parameters
| Name | Type |
|---|---|
apiKey | string |
Your PostHog API key | |
options? | PostHogOptions |
PostHog configuration options | |
Examples
Returns
| Type |
|---|
any |
debugpublic
Enables or disables debug mode for detailed logging.
Notes:
Debug mode logs all PostHog calls to the console for troubleshooting. This is useful during development to understand what data is being sent.
Parameters
| Name | Type |
|---|---|
enabled? | boolean |
Examples
Returns
| Type |
|---|
void |
shutdownpublic
Shuts down the PostHog instance and ensures all events are sent.
Call shutdown() once before the process exits to 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. Use flush() for per-request cleanup instead.
Parameters
| Name | Type |
|---|---|
shutdownTimeoutMs? | number |
Maximum time to wait for shutdown in milliseconds | |
Examples
Returns
| Type |
|---|
Promise<void> |
Capture methods
flushpublic
Manually flushes the event queue.
You can set the number of events in the configuration that should queue before flushing. Setting this to 1 will send events immediately and will use more battery. This is set to 20 by default. You can also manually flush the queue. If a flush is already in progress it returns a promise for the existing flush.
Examples
Returns
| Type |
|---|
Promise<void> |
registerpublic
Registers super properties that are sent with every event.
Super properties are properties associated with events that are set once and then sent with every capture call. They persist across sessions and are stored locally.
Parameters
| Name | Type |
|---|---|
properties | PostHogEventProperties |
An associative array of properties to store about the user | |
Examples
Returns
| Type |
|---|
Promise<void> |
screenpublic
Captures a screen view event.
Notes:
This function requires a name. You may also pass in an optional properties object. Screen name is automatically registered for the session and will be included in subsequent events.
Parameters
| Name | Type |
|---|---|
name | string |
The name of the screen | |
properties? | PostHogEventProperties |
Optional properties to include with the screen event | |
options? | PostHogCaptureOptions |
Optional capture options | |
Examples
Returns
| Type |
|---|
Promise<void> |
unregisterpublic
Removes a super property so it won't be sent with future events.
Super Properties are persisted across sessions so you have to explicitly remove them if they are no longer relevant.
Parameters
| Name | Type |
|---|---|
property | string |
The name of the super property to remove | |
Examples
Returns
| Type |
|---|
Promise<void> |
Error tracking methods
captureExceptionpublic
Capture a caught exception manually
Parameters
| Name | Type |
|---|---|
error | Error | unknown |
The error to capture | |
additionalProperties? | PostHogEventProperties |
Any additional properties to add to the error event | |
Examples
Returns
| Type |
|---|
void |
Feature flags methods
getFeatureFlagpublic
Gets the value of a feature flag for the current user.
Defaults to undefined if not loaded yet or if there was a problem loading. Multivariant feature flags are returned as a string.
Parameters
| Name | Type |
|---|---|
key | string |
The feature flag key | |
Examples
Returns
| Type | |
|---|---|
| Union of | boolean |
string | |
undefined |
getFeatureFlagPayloadpublic
Gets the payload of a feature flag for the current user.
Returns JsonType or undefined if not loaded yet or if there was a problem loading.
Parameters
| Name | Type |
|---|---|
key | string |
The feature flag key | |
Examples
Returns
| Type | |
|---|---|
| Union of | JsonType |
undefined |
isFeatureEnabledpublic
Checks if a feature flag is enabled for the current user.
Defaults to undefined if not loaded yet or if there was a problem loading.
Parameters
| Name | Type |
|---|---|
key | string |
The feature flag key | |
Examples
Returns
| Type | |
|---|---|
| Union of | boolean |
undefined |
reloadFeatureFlagspublic
Reloads feature flags from the server.
PostHog loads feature flags when instantiated and refreshes whenever methods are called that affect the flag. If you want to manually trigger a refresh, you can call this method.
Examples
Returns
| Type |
|---|
void |
reloadFeatureFlagsAsyncpublic
Reloads feature flags from the server asynchronously.
PostHog loads feature flags when instantiated and refreshes whenever methods are called that affect the flag. If you want to manually trigger a refresh and get the result, you can call this method.
Examples
Returns
| Type | |
|---|---|
| Union of | Promise<Record<string, boolean |
string> | |
undefined> |
resetGroupPropertiesForFlagspublic
Resets group properties for feature flag evaluation.
Examples
Returns
| Type |
|---|
void |
resetPersonPropertiesForFlagspublic
Resets person properties for feature flag evaluation.
Examples
Returns
| Type |
|---|
void |
setGroupPropertiesForFlagspublic
Sets group properties for feature flag evaluation.
These properties are automatically attached to the current group (set via posthog.group()). When you change the group, these properties are reset.
Parameters
| Name | Type |
|---|---|
properties | Record<string, Record<string, string>> |
The group properties to set for flag evaluation | |
Examples
Returns
| Type |
|---|
void |
setPersonPropertiesForFlagspublic
Sets person properties for feature flag evaluation.
Sometimes, you might want to evaluate feature flags using properties that haven't been ingested yet, or were set incorrectly earlier. You can do so by setting properties the flag depends on with this call. These are set for the entire session. Successive calls are additive: all properties you set are combined together and sent for flag evaluation.
Parameters
| Name | Type |
|---|---|
properties | Record<string, string> |
The person properties to set for flag evaluation | |
Examples
Returns
| Type |
|---|
void |
Group analytics methods
grouppublic
Associates the current user with a group.
Group analytics allows you to associate the events for that person's session with a group (e.g. teams, organizations, etc.). This is a paid feature and is not available on the open-source or free cloud plan.
Parameters
| Name | Type |
|---|---|
groupType | string |
The type of group (e.g. 'company', 'team') | |
groupKey | string |
The unique identifier for the group | |
properties? | PostHogEventProperties |
Optional properties to set for the group | |
Examples
Returns
| Type |
|---|
void |
Identification methods
aliaspublic
Assigns an alias to the current user.
Sometimes, you want to assign multiple distinct IDs to a single user. This is helpful when your primary distinct ID is inaccessible. For example, if a distinct ID used on the frontend is not available in your backend.
Parameters
| Name | Type |
|---|---|
alias | string |
The alias to assign to the current user | |
Examples
Returns
| Type |
|---|
void |
getDistinctIdpublic
Gets the current user's distinct ID.
You may find it helpful to get the current user's distinct ID. For example, to check whether you've already called identify for a user or not. This returns either the ID automatically generated by PostHog or the ID that has been passed by a call to identify().
Examples
Returns
| Type |
|---|
string |
identifypublic
Associates events with a specific user. Learn more about identifying users
Parameters
| Name | Type |
|---|---|
distinctId? | string |
A unique identifier for your user. Typically either their email or database ID. | |
properties? | PostHogEventProperties |
Optional dictionary with key:value pairs to set the person properties | |
options? | PostHogCaptureOptions |
Optional capture options | |
Examples
Returns
| Type |
|---|
void |
resetpublic
Resets the user's ID and anonymous ID after logout.
To reset the user's ID and anonymous ID, call reset. Usually you would do this right after the user logs out. This also clears all stored super properties and more.
Examples
Returns
| Type |
|---|
void |
getAnonymousIdpublic
Returns the current anonymous ID.
This is the ID assigned to users before they are identified. It's used to track anonymous users and link them to identified users when they sign up.
Examples
Returns
| Type |
|---|
string |
LLM analytics methods
captureTraceFeedbackpublic
Capture written user feedback for a LLM trace. Numeric values are converted to strings.
Parameters
| Name | Type |
|---|---|
traceId | string | number |
The trace ID to capture feedback for. | |
userFeedback | string |
The feedback to capture. | |
Examples
Returns
| Type |
|---|
void |
captureTraceMetricpublic
Capture a metric for a LLM trace. Numeric values are converted to strings.
Parameters
| Name | Type |
|---|---|
traceId | string | number |
The trace ID to capture the metric for. | |
metricName | string |
The name of the metric to capture. | |
metricValue | string | number | boolean |
The value of the metric to capture. | |
Examples
Returns
| Type |
|---|
void |
Privacy methods
optInpublic
Opts the user in to data capture.
By default, PostHog has tracking enabled unless it is forcefully disabled by default using the option defaultOptIn: false . Once this has been called it is persisted and will be respected until optOut is called again or the reset function is called.
Examples
Returns
| Type |
|---|
Promise<void> |
optOutpublic
Opts the user out of data capture.
You can completely opt-out users from data capture. Once this has been called it is persisted and will be respected until optIn is called again or the reset function is called.
Examples
Returns
| Type |
|---|
Promise<void> |
Other methods
fetchpublic
Parameters
| Name | Type |
|---|---|
url | string |
options | PostHogFetchOptions |
Examples
Returns
| Type |
|---|
Promise<PostHogFetchResponse> |
getCommonEventPropertiespublic
Examples
Returns
getCustomUserAgentpublic
Examples
Returns
| Type |
|---|
string |
getLibraryIdpublic
Examples
Returns
| Type |
|---|
string |
getLibraryVersionpublic
Examples
Returns
| Type |
|---|
string |
getPersistedPropertypublic
Parameters
| Name | Type |
|---|---|
key | PostHogPersistedProperty |
Examples
Returns
| Type | |
|---|---|
| Union of | T |
undefined |
getSessionIdpublic
Examples
Returns
| Type |
|---|
string |
getSurveyspublic
Examples
Returns
| Type |
|---|
Promise<SurveyResponse['surveys']> |
initReactNativeNavigationpublic
Parameters
| Name | Type |
|---|---|
options | PostHogAutocaptureOptions |
Examples
Returns
| Type |
|---|
boolean |
resetSessionIdpublic
Examples
Returns
| Type |
|---|
void |
setPersistedPropertypublic
Parameters
| Name | Type |
|---|---|
key | PostHogPersistedProperty |
value | T | null |
Examples
Returns
| Type |
|---|
void |
autocapturepublic
Parameters
| Name | Type |
|---|---|
eventType | string |
elements | PostHogAutocaptureElement[] |
properties? | PostHogEventProperties |
options? | PostHogCaptureOptions |
Examples
Returns
| Type |
|---|
void |
capturepublic
Parameters
| Name | Type |
|---|---|
event | string |
properties? | PostHogEventProperties |
options? | PostHogCaptureOptions |
Examples
Returns
| Type |
|---|
void |
flagsAsyncpublic
- ** FEATURE FLAGS *
Parameters
| Name | Type |
|---|---|
sendAnonDistinctId? | boolean |
fetchConfig? | boolean |
Examples
Returns
| Type | |
|---|---|
| Union of | Promise<PostHogFlagsResponse |
undefined> |
getFeatureFlagDetailspublic
Examples
Returns
| Type | |
|---|---|
| Union of | PostHogFeatureFlagDetails |
undefined |
getFeatureFlagPayloadspublic
Examples
Returns
| Type | |
|---|---|
| Union of | PostHogFlagsResponse['featureFlagPayloads'] |
undefined |
getFeatureFlagspublic
Examples
Returns
| Type | |
|---|---|
| Union of | PostHogFlagsResponse['featureFlags'] |
undefined |
getFeatureFlagsAndPayloadspublic
Examples
Returns
| Type | |
|---|---|
| Union of | {
flags: PostHogFlagsResponse['featureFlags'] |
undefined;
payloads: PostHogFlagsResponse['featureFlagPayloads'] | |
undefined;
} |
getKnownFeatureFlagspublic
Examples
Returns
| Type | |
|---|---|
| Union of | PostHogFlagsResponse['featureFlags'] |
undefined |
groupIdentifypublic
Parameters
| Name | Type |
|---|---|
groupType | string |
groupKey | string | number |
groupProperties? | PostHogEventProperties |
options? | PostHogCaptureOptions |
Examples
Returns
| Type |
|---|
void |
Examples
Returns
| Type |
|---|
void |
onpublic
Parameters
| Name | Type |
|---|---|
event | string |
cb | (...args: any[]) => void |
Examples
Returns
| Type |
|---|
() => void |
onFeatureFlagpublic
Parameters
| Name | Type |
|---|---|
key | string |
cb | (value: FeatureFlagValue) => void |
Examples
Returns
| Type |
|---|
() => void |
onFeatureFlagspublic
Parameters
| Name | Type |
|---|---|
cb | (flags: PostHogFlagsResponse['featureFlags']) => void |
Examples
Returns
| Type |
|---|
() => void |
overrideFeatureFlagpublic
Parameters
| Name | Type |
|---|---|
flags | PostHogFlagsResponse['featureFlags'] | null |
Examples
Returns
| Type |
|---|
Promise<void> |
registerForSessionpublic
Parameters
| Name | Type |
|---|---|
properties | PostHogEventProperties |
Examples
Returns
| Type |
|---|
void |
reloadRemoteConfigAsyncpublic
Examples
Returns
| Type | |
|---|---|
| Union of | Promise<PostHogRemoteConfig |
undefined> |
setupBootstrappublic
Parameters
| Name | Type |
|---|---|
options? | Partial<PostHogCoreOptions> |
Examples
Returns
| Type |
|---|
void |
unregisterForSessionpublic
Parameters
| Name | Type |
|---|---|
property | string |
Examples
Returns
| Type |
|---|
void |
addPendingPromisepublic
Parameters
| Name | Type |
|---|---|
promise | Promise<T> |
Examples
Returns
| Type |
|---|
Promise<T> |
aliasStatelesspublic
Parameters
| Name | Type |
|---|---|
alias | string |
distinctId | string |
properties? | PostHogEventProperties |
options? | PostHogCaptureOptions |
Examples
Returns
| Type |
|---|
void |
aliasStatelessImmediatepublic
Parameters
| Name | Type |
|---|---|
alias | string |
distinctId | string |
properties? | PostHogEventProperties |
options? | PostHogCaptureOptions |
Examples
Returns
| Type |
|---|
Promise<void> |
captureStatelesspublic
Parameters
| Name | Type |
|---|---|
distinctId | string |
event | string |
properties? | PostHogEventProperties |
options? | PostHogCaptureOptions |
Examples
Returns
| Type |
|---|
void |
captureStatelessImmediatepublic
Parameters
| Name | Type |
|---|---|
distinctId | string |
event | string |
properties? | PostHogEventProperties |
options? | PostHogCaptureOptions |
Examples
Returns
| Type |
|---|
Promise<void> |
enqueuepublic
- ** QUEUEING AND FLUSHING *
Parameters
| Name | Type |
|---|---|
type | string |
_message | any |
options? | PostHogCaptureOptions |
Examples
Returns
| Type |
|---|
void |
getCustomHeaderspublic
Examples
Returns
| Type |
|---|
{
[key: string]: string;
} |
getFeatureFlagDetailsStatelesspublic
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> |
getFeatureFlagDetailStatelesspublic
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> |
getFeatureFlagPayloadsStatelesspublic
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> |
getFeatureFlagPayloadStatelesspublic
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> |
getFeatureFlagsAndPayloadsStatelesspublic
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;
}> |
getFeatureFlagsStatelesspublic
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;
}> |
getFeatureFlagStatelesspublic
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;
}> |
getFlagspublic
- ** 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> |
fetchConfig? | boolean |
Examples
Returns
| Type | |
|---|---|
| Union of | Promise<PostHogFlagsResponse |
undefined> |
getRemoteConfigpublic
Examples
Returns
| Type | |
|---|---|
| Union of | Promise<PostHogRemoteConfig |
undefined> |
getSurveysStatelesspublic
- ** SURVEYS *
Examples
Returns
| Type |
|---|
Promise<SurveyResponse['surveys']> |
groupIdentifyStatelesspublic
- ** GROUPS *
Parameters
| Name | Type |
|---|---|
groupType | string |
groupKey | string | number |
groupProperties? | PostHogEventProperties |
options? | PostHogCaptureOptions |
distinctId? | string |
eventProperties? | PostHogEventProperties |
Examples
Returns
| Type |
|---|
void |
identifyStatelesspublic
- ** TRACKING *
Parameters
| Name | Type |
|---|---|
distinctId | string |
properties? | PostHogEventProperties |
options? | PostHogCaptureOptions |
Examples
Returns
| Type |
|---|
void |
identifyStatelessImmediatepublic
Parameters
| Name | Type |
|---|---|
distinctId | string |
properties? | PostHogEventProperties |
options? | PostHogCaptureOptions |
Examples
Returns
| Type |
|---|
Promise<void> |
logMsgIfDebugpublic
Parameters
| Name | Type |
|---|---|
fn | () => void |
Examples
Returns
| Type |
|---|
void |
sendImmediatepublic
Parameters
| Name | Type |
|---|---|
type | string |
_message | any |
options? | PostHogCaptureOptions |
Examples
Returns
| Type |
|---|
Promise<void> |
wrappublic
Parameters
| Name | Type |
|---|---|
fn | () => void |
Examples
Returns
| Type |
|---|
void |