> ## 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.

# Amplitude

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

The Amplitude drain puts your agent's conversations in your Amplitude project. Chat messages show in the same user's activity as their page views, at the time they happened, and the end-of-conversation labels arrive as one more event you can group 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 Amplitude project's **API key**. In Amplitude, open **Settings → Organization settings → Projects** and pick the project. Click **Manage** next to **API Keys** and copy the **API Key** (it can show as **Legacy API Key**). Never paste the **Secret Key**: Waniwani only sends events and never needs it.
* Your project's data center: **US**, or **EU** if you sign in at `app.eu.amplitude.com`.
* The Amplitude Browser SDK on the pages that show the chat.

## Connect Amplitude

**1. Paste the key**

In Waniwani, open **Settings → Connectors → Amplitude**. Paste the API key, pick the data center and click **Connect**. Waniwani checks the key by sending one `waniwani connected` event, on a test user named `waniwani-connection-test`. That one user stays in your project.

**2. Link the chat to your Amplitude users**

Give the chat each visitor's Amplitude device ID. Add this where the chat is installed:

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

```js Script tag theme={null}
WaniWani.chat.setVisitorId(amplitude.getDeviceId())
```

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

**3. Send a test conversation**

Click **Send test conversation**. Your newest conversation goes to Amplitude right away, and Waniwani lists the events sent. In Amplitude, open **User Look-Up**, search the device ID, and the events show in that user's activity within a few minutes.

> [!NOTE]
>
> The chat tells Waniwani whether your page set the visitor ID. Amplitude device IDs and the chat's own visitor IDs are both random, so this is how Waniwani knows which one it got. The script tag loads the latest chat on its own. A React site needs `@waniwani/chat` 1.5.1 or later.

## Conversations with no device ID are skipped

Every new ID you send to Amplitude becomes a new user, and Amplitude bills by user. So Waniwani sends a conversation only when the chat has the visitor's Amplitude device ID. The others show as **Skipped** on the drain page, with a count under **Last 7 days**, and nothing reaches Amplitude for them.

A conversation is skipped when the chat isn't linked yet, when `amplitude.getDeviceId()` returned nothing on that page, and for conversations that never had a browser (voice, ChatGPT, Claude).

## What Amplitude receives

| Amplitude field | Value |
| - | - |
| `event_type` | `waniwani ` + the event name, for example `waniwani chat.user_message` |
| `device_id` | The visitor's Amplitude device ID |
| `time` | When the event happened in the conversation |
| `insert_id` | A fixed ID for each event. Amplitude drops a second copy with the same `insert_id` for 7 days, so a resend never adds a duplicate |
| `session_id` | `-1`, so chat events don't change your Amplitude sessions |
| `event_properties` | The ones listed in [What your tool receives](https://docs.waniwani.ai/platform/drains/overview.md#what-your-tool-receives) |

Text values longer than 1,024 characters are cut at 1,024, the Amplitude limit. If your site calls `amplitude.setUserId` for signed-in visitors, Amplitude joins the device to that user, and the chat events follow.

## Use it in Amplitude

* **Do chatters convert better?** In a funnel, start with `waniwani chat.user_message` and end with your conversion event, then compare with the same funnel for users who never chatted.
* **Which topics come before a conversion?** Group `waniwani conversation analyzed` by `topics`, then add your conversion event as the next funnel step.
* **Where do conversations stall?** Group `waniwani conversation analyzed` 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 |
| - | - |
| "Amplitude refused this key" | Copy the API Key again from the project's API keys page. The Secret Key does not work. |
| Every conversation shows as Skipped | Link the chat (step 2). Check that `amplitude.getDeviceId()` returns an ID on the page that shows the chat, and on React update `@waniwani/chat`. |
| The test result says the conversation has no Amplitude device ID | The newest conversation came from a page that wasn't linked. Send a message from a linked page and test again. |
| Events don't show in User Look-Up | Wait a few minutes. Then check that the data center picked in Waniwani is the one of your project, and that you search in the same project. |
| The drain page says Amplitude 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.