> ## Documentation Index
> Fetch the complete documentation index at: https://docs.waniwani.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# PostHog

> Send each Waniwani conversation to your PostHog project, on the same person as your website events, with the end-of-conversation labels you can group in funnels.

The PostHog drain puts your agent's conversations in your PostHog project. Chat messages show between the page views and clicks of the same person, at the time they happened, and the end-of-conversation labels arrive as one more event you can break funnels down by. Read [Drains](https://docs.waniwani.ai/platform/drains/overview.md) first for how a conversation goes out and what each event holds.

## Before you start

* An admin role in your Waniwani organization.
* Your PostHog **project API key** (it starts with `phc_`). In PostHog, open **Settings → Project** and copy **Project API key**. This key can only send events.
* Your PostHog region: **US Cloud**, **EU Cloud**, or the address of your own PostHog host (it must start with `https://`).

## Connect PostHog

**1. Paste the key**

In Waniwani, open **Settings → Connectors → PostHog**. Paste the project API key, pick the region and click **Connect**. Waniwani checks the key with PostHog and sends one `waniwani connected` event, which creates no person.

**2. Link the chat to your PostHog visitors**

Give the chat each visitor's PostHog distinct ID, so chat events land on the same person as their page views. Add this where the chat is installed:

```tsx React theme={null}
<WaniwaniChat
  visitorId={() => posthog.get_distinct_id()}
/>
```

```js Script tag theme={null}
WaniWani.chat.setVisitorId(posthog.get_distinct_id())
```

Run the script-tag line once PostHog has loaded on the page. Then open your site and send one message: the step shows when a chat session with a PostHog ID came in.

**3. Send a test conversation**

Click **Send test conversation**. Your newest conversation goes to PostHog right away, and Waniwani lists the events sent and whether they landed on a person. **Open in PostHog** takes you to that person.

## What PostHog receives

| PostHog field | Value |
| - | - |
| `event` | `waniwani ` + the event name, for example `waniwani chat.user_message` |
| `distinct_id` | The visitor's PostHog distinct ID, when the chat has one |
| `timestamp` | When the event happened in the conversation, so it sits in the right place in the person's timeline |
| `uuid` | A fixed ID for each event, so a resend never adds a duplicate |
| `$current_url` | The page the visitor was on, also in `page_url` |
| Other properties | The ones listed in [What your tool receives](https://docs.waniwani.ai/platform/drains/overview.md#what-your-tool-receives) |

When the chat has no PostHog ID, the events still go out, on an ID made from the conversation, with `$process_person_profile: false`. They count in event totals and create no person, so they can't join a funnel with your page views. This happens when the chat isn't linked yet, for visitors PostHog tracks without cookies, and for conversations that never had a browser (voice, ChatGPT, Claude).

Waniwani takes the visitor ID as a PostHog distinct ID unless it is an ID the chat made itself or a PostHog cookieless ID. If you identify signed-in users in PostHog, `posthog.get_distinct_id()` returns their identified ID and the chat events follow them.

## Use it in PostHog

* **Do chatters convert better?** Make a cohort of persons who did `waniwani chat.user_message`, and compare its conversion with everyone else.
* **Which topics come before a conversion?** Build a funnel that starts with `waniwani conversation analyzed`, ends with your conversion event, and is broken down by `topics`.
* **Where do conversations stall?** Break `waniwani conversation analyzed` down by `drop_off_reason`, or filter on `frustrated = true` and open `waniwani_session_url` to read the conversation in Waniwani.

## Troubleshooting

| What you see | What to do |
| - | - |
| "PostHog refused this key" | Copy the project API key (it starts with `phc_`) again from **Settings → Project** of the right project. |
| "This PostHog project is over its event quota" | Raise the quota in PostHog, then connect again. |
| Events arrive but on no person | Link the chat (step 2). Check that `posthog.get_distinct_id()` returns an ID on the page that shows the chat. |
| The drain page says PostHog refused the project key | The key was revoked or rotated. Paste the new key, then click **Send again** for the conversations that did not go out. |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.