# PostHog integration

<Aside type="caution" icon="setting" title="Developer assistance needed">
 You'll need help from your development team to set up the integration. Share this guide with them.
</Aside>

[PostHog](https://posthog.com/) is a product analytics platform that collects events from your apps and websites. With the Pushwoosh destination in PostHog, the events you already track there reach Pushwoosh as [events](/product/audience-data-and-segmentation/events/). Use them to build segments and to start or branch [customer journeys](/product/customer-journey/pushwoosh-journey-overview).

The destination works only if two fields in its mapping carry Pushwoosh values: the platform field must hold `ios`, `android`, or `web`, and the user field must hold the User ID that Pushwoosh knows the user by. PostHog's own identifiers don't match anything in Pushwoosh.

## Before you start

Check that you have the following before you create the destination:

* The [Pushwoosh SDK](/developer/first-steps/integrate-pushwoosh-sdk) is integrated in your app.
* You know which value identifies a user in Pushwoosh. It's the value your app passes to [`setUserId`](/developer/first-steps/collecting-user-ids/), or the device's Hardware ID (HWID) for users without a custom User ID.
* PostHog events carry this value. For example, your app calls `posthog.identify()` with the same ID it passes to `setUserId`, so the PostHog `distinct_id` equals the Pushwoosh User ID.
* You have the application code and an API token from **Settings** → **API Access** in Pushwoosh. A [Device API token](/developer/api-reference/api-access-token/#device-api-token) is enough.
* The token belongs to the account that owns the application.

## Create the Pushwoosh destination in PostHog

Create the destination in PostHog, enter your Pushwoosh credentials, and set the mapping fields.

1. In PostHog, click **New destination**.
2. Search for **Pushwoosh** and click **Create** next to it.

   <img src="/posthog-integration-1.webp" alt="Pushwoosh destination in the PostHog destination catalog"/>

3. In **Pushwoosh API Key**, paste the API token.
4. In **Pushwoosh Application Code**, paste the application code. It looks like `XXXXX-XXXXX`.

   <img src="/posthog-integration-2.webp" alt="Pushwoosh API Key and Pushwoosh Application Code fields in the destination settings"/>

5. Under **Mappings**, click **Track Events** to open its fields.

   <img src="/posthog-integration-3.webp" alt="Track Events mapping in the destination's Mappings list"/>

6. Set the fields as described in [Map the Track Events fields](#map-the-track-events-fields).
7. Optionally, click **Create Or Update User Profile** in the same list and set its fields as described in [Map the user profile fields](#map-the-user-profile-fields).
8. Click **Create & enable**.

## Map the Track Events fields

Three fields decide which user an event belongs to. Fill each of them with a PostHog field that carries the Pushwoosh value, not just any identifier.

<img src="/posthog-integration-4.webp" alt="External User ID, Device ID, and Device Platform fields in the Track Events mapping"/>

| Field | Map it to | Don't map it to |
| --- | --- | --- |
| **External User ID** | A field that holds the Pushwoosh User ID or HWID, usually `{event.distinct_id}` | `{person.id}`, because it's PostHog's internal person ID and no Pushwoosh user has it |
| **Device Platform** | A field that holds `ios`, `android`, or `web`, usually `{event.$os}` | `$device_type`, because it holds `Mobile` or `Desktop`, not a platform |
| **Device ID** | A field that holds the Pushwoosh HWID of the device, if your PostHog events carry it. Otherwise clear the field | PostHog's own `{event.properties.$device_id}`, unless your app writes the Pushwoosh HWID into it |

### External User ID

Pushwoosh links the event to the user with this ID. `distinct_id` works only when it equals the Pushwoosh User ID, as described in [Before you start](#before-you-start).

### Device Platform

Values are case-insensitive, so `iOS` and `Android` from `$os` work as they are.

On websites, `$os` holds the computer's operating system, such as `Mac OS X`. In that case Pushwoosh takes the platform from the `$lib` event property. Keep **Event Properties** set to include the entire `{event.properties}` object so that `$lib` reaches Pushwoosh.

### Device ID

With the field empty, Pushwoosh records the event for the user as a whole, and it counts for all the user's devices. A value that isn't a Pushwoosh HWID matches no device in Pushwoosh.

If the field holds `{event.properties.$device_id}`, clear it, unless your app writes the Pushwoosh HWID into `$device_id`.

### Other fields

Leave **Name**, **Timestamp**, and **Event Properties** as PostHog fills them. Each PostHog event becomes a Pushwoosh event with the same name, and Pushwoosh creates missing events and event properties automatically.

## Map the user profile fields

The **Create Or Update User Profile** mapping sends PostHog person properties to Pushwoosh as [tags](/product/audience-data-and-segmentation/user-data-tags/tags/). It's optional: set it up if you want to use person properties for targeting and personalization.

1. Set **External User ID** the same way as for Track Events.
2. Leave the device token, device ID, and platform fields empty, unless your PostHog data holds Pushwoosh push tokens.

With only a User ID, Pushwoosh saves the properties as user-specific tags of an existing user and creates no device.

If you map the device token and device ID, map the platform too, to a field that holds `ios`, `android`, or `web`. With any other platform, Pushwoosh rejects the request and saves nothing from it.

## Check that the integration works

Send a test event from the destination, then check a real event in Pushwoosh.

1. In the destination, click **Start testing**.
2. Click **Test function** to send an example event.
3. Check that the test shows **Success**.

Then send a real event for a user who has a device in Pushwoosh. In Pushwoosh, go to **Audience** → **Events**, open the event's three-dot menu, choose **Event statistics**, and check that the event is counted.

Pushwoosh answers each request with a status and, for rejected events, the reason. The full list of responses is in [What Pushwoosh does with data from Segment](/product/integrations/segment-integration/#what-pushwoosh-does-with-data-from-segment), as the PostHog destination uses the same endpoints.

## Troubleshooting

If the test or the destination's logs show an error instead of **Success**, find the reason below.

### The response says token doesn't match appcode

The API token belongs to a different account than the application. Create the token in the account that owns the application code.

### The response says application not found

The application code is wrong. Copy it again from the Pushwoosh Control Panel.

### The response says invalid argument

Pushwoosh rejected the event because of its content. The most common cause is that **External User ID** and **Device ID** are both empty. Map **External User ID** to a field that holds the Pushwoosh User ID, as described in [Map the Track Events fields](#map-the-track-events-fields). Otherwise, check that the event name and event properties aren't unusually long.

### The test succeeds, but the event isn't counted for the user

Check these in order:

1. The value in **External User ID** equals the Pushwoosh User ID. With `{event.distinct_id}`, your app must call `posthog.identify()` with the same ID it passes to `setUserId`.
2. A user with this User ID exists in Pushwoosh. Search for it in **Audience** → **User Explorer**.
3. **Device ID** is empty or holds a Pushwoosh HWID, not PostHog's own device ID.