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

# Mixpanel

> Export router data to Mixpanel and import your product events

Connect your [Mixpanel](https://mixpanel.com) project to export router exposure data for analysis and import your product events to define custom metrics in Portal.

## Prerequisites

<Steps titleSize="h3">
  <Step title="Find your Mixpanel Project ID">
    <img src="https://mintcdn.com/inworldaidev/Xfq08e_91GsQXodV/img/router/mixpanel-project-overview.png?fit=max&auto=format&n=Xfq08e_91GsQXodV&q=85&s=1344af919ca209c08d505370bceac57e" alt="" width="2266" height="1678" data-path="img/router/mixpanel-project-overview.png" />

    In Mixpanel, navigate to **Settings > Project Settings**. In the **Overview** tab, find and copy your **Project ID**.
  </Step>

  <Step title="Create a service account">
    <img src="https://mintcdn.com/inworldaidev/Xfq08e_91GsQXodV/img/router/mixpanel-service-account.png?fit=max&auto=format&n=Xfq08e_91GsQXodV&q=85&s=3208dd11eaf21895b9b13639e5940155" alt="" width="1656" height="1080" data-path="img/router/mixpanel-service-account.png" />

    In Project Settings, go to the **Service Accounts** tab. Click **Add Service Account**.

    Type in the name of your new service account. Click the `+` button to create your new service account. Set the **Project Role** to **Admin** or **Owner**. Set **Expires** to **Never**.

    Click **Add** and copy the username and secret — you will not be able to view the secret again.
  </Step>

  <Step title="Configure the integration in Portal">
    <img src="https://mintcdn.com/inworldaidev/Xfq08e_91GsQXodV/img/router/connect-mixpanel.png?fit=max&auto=format&n=Xfq08e_91GsQXodV&q=85&s=bfdd45888ce0598b78f71ea87c382b2e" alt="" width="1502" height="1452" data-path="img/router/connect-mixpanel.png" />

    In [Portal](https://portal.inworld.ai), go to **Routers** and click **Set Up Metrics** to open the **Data Integrations** page, then click **Add Integration** and select **Mixpanel**. Enter the following:

    | Field                        | Value                                    |
    | :--------------------------- | :--------------------------------------- |
    | **Project ID**               | Your Mixpanel project ID from Step 1     |
    | **Service Account Username** | The service account username from Step 2 |
    | **Service Account Secret**   | The service account secret from Step 2   |

    Click **Enable** to save your credentials. You can then toggle export and import independently in the **Data Sync** section.
  </Step>
</Steps>

## Exporting to Mixpanel

Toggle **Export to Mixpanel** on in the Data Sync section of your integration settings. When enabled, router exposure data is automatically sent to your Mixpanel project.

### Make a request to your router

Call the [Chat Completions API](/api-reference/routerAPI/chat-completions) with your router, populating the Mixpanel Distinct ID that corresponds to the user in the `user` field:

```shell theme={"system"}
curl --request POST \
  --url https://api.inworld.ai/v1/chat/completions \
  --header 'Authorization: Basic <your-api-key>' \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "inworld/my-router",
    "messages": [{"role": "user", "content": "Hello"}],
    "user": "<mixpanel-distinct-id>"
  }'
```

The router exposure data will match the `user` to the Mixpanel Distinct ID when creating events in Mixpanel, so you can tie the specific request to the right user.

### View events in Mixpanel

The router exposure data will flow to your Mixpanel project automatically (data syncs every \~8 hours). You will see an event for each router, route, and variant that a user was exposed to every hour.

Each event will include:

* `alias_id` — Your router's ID
* `route_id` — The route that the user was routed to
* `variant_id` — The variant that the user was exposed to
* `Time` — The time of the most recent event in the hourly bucket in which the user was exposed to this router, route, and variant

The Distinct ID of the event will be what was passed in the `user` field of the request.

<img src="https://mintcdn.com/inworldaidev/Xfq08e_91GsQXodV/img/router/mixpanel-router-events.png?fit=max&auto=format&n=Xfq08e_91GsQXodV&q=85&s=7f5db2d2f9e09680ebe07ae816299232" alt="" width="2662" height="1782" data-path="img/router/mixpanel-router-events.png" />

### Analyze your data

We recommend creating a [Borrowed Event Property](https://docs.mixpanel.com/docs/features/custom-properties#borrowed-properties). To do so, create a Custom Event Property and select Computed > Borrow Property. Select the `Inworld Router Exposure` event and `alias_id`, `route_id`, or `variant_id` based on what property you want to analyze your data by.

<img src="https://mintcdn.com/inworldaidev/Xfq08e_91GsQXodV/img/router/mixpanel-borrow-event-property.gif?s=2eebdd52db652a68aa2fba579f6a2b54" alt="borrow-event.gif" width="960" height="727" data-path="img/router/mixpanel-borrow-event-property.gif" />

Now you can use the property in any Breakdown of a report to see how your metrics trend based on the router, route, or variant that the user was exposed to.

## Importing from Mixpanel

Toggle **Import from Mixpanel** on in the Data Sync section to start pulling your product events into Portal.

### Define events to import

In the **Events to import** section that appears after enabling import, click **Add Event** and configure:

| Field           | Description                                                                                    | Example                                  |
| :-------------- | :--------------------------------------------------------------------------------------------- | :--------------------------------------- |
| **Event ID**    | The Mixpanel event name to import. Must exactly match the event name in your Mixpanel project. | `purchase`                               |
| **Description** | Optional description of when this event is emitted.                                            | `Fires when a user completes a purchase` |

<Note>
  Event IDs can only contain letters, numbers, underscores, and hyphens.
</Note>

You can add multiple events — each one will be imported separately and will be available as a distinct event when creating [custom metrics](/router/data-integrations/custom-metrics).

### How import works

* Inworld uses the same service account credentials configured in [Prerequisites](#prerequisites) to read events from your Mixpanel project.
* Events are synced approximately every **8 hours**.
* User identity is matched using the Mixpanel **Distinct ID**. To correlate imported events with router exposures, ensure the same user identifier is passed as the `user` field in your Chat Completions API requests and as the Distinct ID in your Mixpanel events.
* Once events are imported, their properties are automatically discovered and become available for metric configuration.

### Next steps

Once your events are importing, [create custom metrics](/router/data-integrations/custom-metrics) to track how routing decisions impact your KPIs.
