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

# Mixpanel Cohorts

> Import Mixpanel cohort data into Kameleoon to target experiments and personalizations at specific user segments defined by behavioral data in Mixpanel.

[Mixpanel](https://mixpanel.com/) is a leading user-behavior analysis solution for product teams.

The Kameleoon Mixpanel Cohorts integration automatically exports Mixpanel cohort data into Kameleoon, where you can use it to deliver personalized experiences and run highly targeted experiments, driving more impactful and data-driven optimizations.

![](https://storage.googleapis.com/kameleoon-storage-documentation/user-manual/images/integrations/as-a-destination/analytics/mixpanel/mixpanel-cohorts/data-flow.png)

## Key benefits

* **Seamless cohort sync:** Import user segments from Mixpanel into Kameleoon effortlessly.
* **Personalized targeting:** Deliver tailored experiences based on user behavior.
* **Enhanced experimentation:** Run precise A/B tests on specific audience groups.
* **Real-time updates:** Keep user segments up-to-date automatically.

## Use cases

### Optimize experiments with behavioral targeting

Sync Mixpanel cohorts with Kameleoon to test variations on users based on their real-time behavior.

For example, run an A/B test on users who have viewed a product page multiple times, but haven't added anything to their cart. Test different incentives (for example, a limited-time offer versus social proof messaging) to determine which strategy drives the most conversions.

### Analyze results with behavioral segmentation

Break down Kameleoon campaigns using Mixpanel cohorts for deeper insights.

For example, if a test or personalization campaign shows a drop in conversions, analyze key Mixpanel segments (for example, high-value customers, churn-prone users) to identify patterns. This analysis helps you understand which experiences resonate with different user groups and refine your strategy accordingly.

### Personalize experiences using Mixpanel's rich user insights

Leverage Mixpanel's deep user data (such as behavior, lifecycle stage, or purchase history) to create highly relevant experiences in Kameleoon.

For example, identify frequent buyers, users who recently downgraded their subscription, or those who engage with specific content. Then, tailor messaging, promotions, or feature recommendations in Kameleoon to maximize engagement and conversions.

## Activate the Kameleoon integration in Mixpanel

You must activate the Kameleoon integration in your Mixpanel account to enable cohort synchronization. You can refer to [this](https://help.mixpanel.com/hc/en-us/articles/360055834811-Kameleoon-Integration) Mixpanel article.

> [!NOTE]
>
> You must add a Kameleoon API key. Ask your Kameleoon Customer Success Manager for it.

Only identified user profiles get exported to Kameleoon (no anonymous users). If you're using Mixpanel's identity merge feature and, as Mixpanel's `distinct_id` for a given user may change, you should set `$kameleoon_mapping_id` as an additional user property that contains the [Kameleoon visitor code](https://docs.kameleoon.com/developer-docs/apis/activation-api-js/api-reference/objects/configuration-and-visitor.md#visitor) or your internal user ID if you use Kameleoon's [SDKs](https://docs.kameleoon.com/developer-docs/feature-experimentation/get-started/overview.md). Mixpanel will automatically add the `kameleoon_mapping_id` in each cohort export it sends to Kameleoon.

You can set the `kameleoon_mapping_id` using the [Mixpanel `people.set` method](https://docs.mixpanel.com/docs/tracking-methods/sdks/javascript#setting-profile-properties), right after the identify method runs. Here is an example of code you can use with the Mixpanel JavaScript API:

```javascript theme={null}
mixpanel.people.set({
     '$kameleoon_mapping_id': Kameleoon.API.Visitor.code
 });
```

Underneath, Mixpanel uses Kameleoon's [Data API map endpoint](https://docs.kameleoon.com/api-reference/map/update-data-for-several-keys.md) to send the cohorts to Kameleoon.

## Use Mixpanel cohorts in your Kameleoon campaigns

### Create a custom data to target Mixpanel cohorts in your experiments

To target Mixpanel cohorts, you must set up a custom data within Kameleoon to retrieve the cohort names for each user. For more information on creating custom data, refer to [Create custom data](https://docs.kameleoon.com/user-manual/assets/custom-data/create-custom-data.md).

Name your custom data **Mixpanel cohorts** and choose the **Custom JavaScript code** retrieval method if you're running a web experiment or the **Kameleoon SDK** retrieval method if you're running a server-side experiment or using a feature flag.

Set the custom data to the **list of** and **string** types. You can set the scope to **Page**, depending on the scope you defined in Mixpanel. With the **Page** scope, Kameleoon reevaluates whether a user belongs to a certain Mixpanel cohort on every page load.

### External segment sync for web experiments

If you use the `$kameleoon_mapping_id` property, use the code below in the custom data retrieval method:

```javascript theme={null}
Kameleoon.API.Data.retrieveDataFromRemoteSource(Kameleoon.API.Visitor.code, function (data) {
 if (data?.mixpanel_cohorts) {
 data.mixpanel_cohorts.map(function (segment) {
 if (segment?.mixpanel_cohort_id) {
 Kameleoon.API.Data.setCustomData("Mixpanel cohorts", segment.mixpanel_cohort_id);
 }
 });
 }
});
return { "value": null}
```

If you'd rather use the Mixpanel default `distinct_id` method to set your own user ID, use this code:

```javascript theme={null}
if (!window.mixpanel?.get_distinct_id) return null;
let id = mixpanel.get_distinct_id();
Kameleoon.API.Data.retrieveDataFromRemoteSource(id, function (data) {
 if (data?.mixpanel_cohorts) {
 data.mixpanel_cohorts.map(function (segment) {
 if (segment?.mixpanel_cohort_id) {
 Kameleoon.API.Data.setCustomData("Mixpanel cohorts", segment.mixpanel_cohort_id);
 }
 });
 }
});
return { "value": null}
```

### External segment sync for server-side

To use Mixpanel cohorts in server-side experiments or feature flags, consult the guidelines outlined in the [documentation for each respective SDK](https://docs.kameleoon.com/developer-docs/feature-experimentation/get-started/overview.md).

Kameleoon's SDKs use the `getRemoteData()` method, which is accessible across all Kameleoon's SDKs. This method fetches data stored by Mixpanel on Kameleoon's servers and makes it accessible within your backend code.

### Use Mixpanel cohort names in a Kameleoon segment

This section explains how to display Mixpanel cohort names in the Kameleoon segment builder. When you select the corresponding custom data and use the **is among the values** condition, Kameleoon shows a list of all available Mixpanel cohorts to choose from.

This setup requires your [Kameleoon project sitecode](https://docs.kameleoon.com/user-manual/project-management/manage-your-projects.md#how-do-i-find-my-sitecode).

When you create the custom data, enable **Save the values for the targeting condition associated with the custom data point** in the options step. This option displays an additional code field.

Copy the following code into the custom data code field and replace `ADD_YOUR_PROJECT_ID_HERE` with your Kameleoon project sitecode.

```javascript theme={null}
const response = await fetch('https://customers.kameleoon.com/mixpanel/cohorts/ADD_YOUR_PROJECT_ID_HERE');

if (!response.ok) {
  throw new Error(`HTTP error: ${response.status}`);
}

const mixpanel = await response.json();

const mixPanelSegments = mixpanel
  .filter((segment) => segment.id && segment.name)
  .map((segment) => ({
    value: segment.id,
    label: segment.name,
  }));

return mixPanelSegments;
```

Selecting a cohort enables targeting in personalizations and experiments.
