# Mixpanel

## Why combine Mixpanel and Purchasely?

### Pre-requisites

The minimal version of the Purchasely SDK supporting this integration is v3.2.0. If the Purchasely SDK integrated in your app is under the minimal version, please update it.

The Mixpanel SDK also needs to be integrated inside the app.

### Subscription events

### General overview

[Mixpanel](https://www.mixpanel.com) is a leading product analytics software company, it is an event-based tracking solution gives product teams the ability to gain insights into how to best acquire, convert, and retain their users across web and mobile platforms.

This integration will allow you to get all the available Purchasely events to Mixpanel and get a better and deeper understanding of your subscription business and customer behavior.

Purchasely provides a unified dataset to track the subscription events for all stores. These events are generated by the Purchasely Backend and can be sent to Mixpanel.

![](https://2145676854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKuAJGBnHJWZbqzA4g8yO%2Fuploads%2Fgit-blob-45df9d75ce2a49b2f164156124fd5c2800074df7%2Fimage%20\(143\).png?alt=media)

Subscription events generated by Purchasely will be sent to Mixpanel using a server-to-server integration. As a result, you will be able to see all the subscription events in your Mixpanel Dashboard and map them with your acquisition campaigns.

#### Events

The following events are the main ones that can be sent to Mixpanel by Purchasely. See the full list [here](https://purchasely.gitbook.io/purchasely/analytics/events/webhook-events/subscription-events).

| Event                           | Description                                                                                                              |
| ------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
| SUBSCRIPTION\_STARTED           | Sent when the user purchased a product wether it is the start of a trial or a regular purchase of a consumable product.  |
| SUBSCRIPTION\_RENEWED           | Sent when a subscription renews                                                                                          |
| SUBSCRIPTION\_EXPIRED           | Sent when the subscription actually ends                                                                                 |
| SUBSCRIPTION\_REACTIVATED       | Sent when an expired subscription is reactivated. This event is particularly useful for win-back & retargeting campaigns |
| SUBSCRIPTION\_REFUNDED\_REVOKED | Sent when the subscription actually ends                                                                                 |
| RENEWAL\_DISABLED               | Sent when the user deactivates the renewal of a subscription wether it is in trial period or not.                        |
| RENEWAL\_ENABLED                | Sent when the user reactivates                                                                                           |
| TRIAL\_STARTED                  | Sent when a trial starts                                                                                                 |
| TRIAL\_CONVERTED                | Sent when a user converts from a free trial to a normal paid-period                                                      |
| TRIAL\_NOT\_CONVERTED           | Sent when a user finishes it's trial period without renewing to a paid-period                                            |

The names of events sent to Mixpanel can be overriden when setting up the integration.

## **Integrating Purchasely with Mixpanel**

The integration requires 2 steps:

1. Associate the user to events by providing the `Mixpanel Distinct ID` to the Purchasely SDK
2. Activate the Mixpanel integration in the Purchasely Console

### 1. Associating users to events

See the [Mixpanel Documentation](https://help.mixpanel.com/hc/en-us/articles/115004509406-Distinct-IDs-) for more information

{% tabs %}
{% tab title="Swift" %}

```swift
Mixpanel.initialize(token: "MY_TOKEN")
Purchasely.setAttribute(.mixpanelDistinctId, value: Mixpanel.mainInstance().distinctId)
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val mixpanel = MixpanelAPI.getInstance(applicationContext, "MY_TOKEN")
mixpanel.distinctId?.let {
  Purchasely.setAttribute(Attribute.MIXPANEL_DISTINCT_ID, it)
}
```

{% endtab %}

{% tab title="Java" %}

```java
MixpanelAPI mixpanel = MixpanelAPI.getInstance(getApplicationContext(), "MY_TOKEN");
if(mixpanel.getDistinctId() != null) {
  Purchasely.setAttribute(Attribute.MIXPANEL_DISTINCT_ID, mixpanel.getDistinctId());
}
```

{% endtab %}

{% tab title="React Native" %}

```jsx
import { Mixpanel } from 'mixpanel-react-native';

const mixpanel = new Mixpanel("Your Project Token");
mixpanel.init();
Purchasely.setAttribute(Attributes.MIXPANEL_DISTINCT_ID, (await mixpanel.getDistinctId(););
```

{% endtab %}
{% endtabs %}

### 2. Activating the Mixpanel integration

The activation requires 2 steps:

1. Setting up a new service account in the Mixpanel Dashboard
2. Retrieving your service account's project ID from the Mixpanel Dashboard
3. Enabling the Mixpanel integration in the Purchasely Console

#### a. Setting up a new service account in the Mixpanel Dashboard

1. Follow [the Mixpanel documentation](https://developer.mixpanel.com/reference/service-accounts#managing-service-accounts)
2. Write down your service account

{% hint style="info" %}
Because it relies on the [`Import Event` endpoint of Mixpanel's `Ingestion API`](https://developer.mixpanel.com/reference/import-events), the Mixpanel integration requires a service account with the `Owner` or `Admin` *Project Role* to be able to forward subscription events to Mixpanel. We strongly recommend using `Member` *Organization Role* and the `Admin` *Project role*.
{% endhint %}

#### b. Retrieving your service account's project ID from the Mixpanel Dashboard

1. Follow [the Mixpanel documentation](https://help.mixpanel.com/hc/en-us/articles/115004490503-Project-Setting#project-id)

#### c. Enabling the Mixpanel integration in the Purchasely Console

1. Go in the "External integrations" section, and open the edition form for Mixpanel:

![](https://2145676854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKuAJGBnHJWZbqzA4g8yO%2Fuploads%2Fgit-blob-47dbe5b79cdcc6550081d2798aa20293a440d9ae%2FScreenshot%202022-04-21%20at%2022.55.02.png?alt=media)

2\. Enable the integration

3\. Set your Mixpanel service account Project ID

4\. Set your Mixpanel service account Username

5\. Set your Mixpanel service account Secret

![](https://2145676854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKuAJGBnHJWZbqzA4g8yO%2Fuploads%2Fgit-blob-8ae399800fb88d25600a177b76f7b56f2aebaab4%2FScreenshot%202022-04-21%20at%2022.58.25.png?alt=media)

6\. Enable the events you want to be sent to Mixpanel

7\. (Optional) Override the names of the events that will be sent to Mixpanel

![](https://2145676854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKuAJGBnHJWZbqzA4g8yO%2Fuploads%2Fgit-blob-5aa49dc84f6ff33b133e246846ec8ad4e426a9bb%2FScreenshot%202022-04-21%20at%2022.59.00.png?alt=media)

8\. Save

### Testing your integration

To test your integration, you can perform a set of in-app purchases in a Sandbox environment (eg: TestFlight for the App Store) and verify your events are received in the Mixpanel dashboard <https://mixpanel.com/login/>.
