# AppsFlyer

## Why combine AppsFlyer 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 AppsFyer SDK also needs to be integrated inside the app.

### Subscription events

### General overview

[Appsflyer ](https://www.appsflyer.com/)is one of the leading MMP (Mobile Measurement Partner): a third-party attribution company that collects, organizes, and presents data from marketing campaigns in order to provide brands with a unified view of their campaign performance

This integration will allow you to track your subscriptions and in-app purchases events in your AppsFlyer Dashboard.

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

![](https://1658765766-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGgUdOzhqa07uh7nB2iZA%2Fuploads%2FwQTVpgo0uU1ixEAG2Frr%2Fimage.png?alt=media\&token=189f338a-efc1-4651-8c4e-297f81768b59)

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

The following events can be sent from Purchasely to AppsFlyer to associate them to your campaigns. You can find the full list [here](../analytics/events/webhook-events/subscription-events).

<table><thead><tr><th width="368.3582625539634">Event</th><th>Description</th></tr></thead><tbody><tr><td>SUBSCRIPTION_STARTED</td><td>Sent when the user purchased a product wether it is the start of a trial or a regular purchase of a consumable product.</td></tr><tr><td>SUBSCRIPTION_RENEWED</td><td>Sent when a subscription renews</td></tr><tr><td>TRIAL_STARTED</td><td>Sent when a trial starts</td></tr><tr><td>TRIAL_CONVERTED</td><td>Sent when a user converts from a free trial to a normal paid-period</td></tr><tr><td>TRIAL_NOT_CONVERTED</td><td>Sent when a user finishes it's trial period without renewing to a paid-periodYou can also override an event's token for each store, individually, provided you h</td></tr><tr><td>PURCHASE_VALIDATED</td><td>Sent when a user purchases a one-time purchase (consumable or not consumable)</td></tr><tr><td>SUBSCRIPTION_REACTIVATED</td><td>Sent when an expired subscription is reactivated. This event is particularly useful for win-back &#x26; retargeting campaigns</td></tr><tr><td>SUBSCRIPTION_REFUNDED_REVOKED</td><td>Sent when a subscription is refunded</td></tr></tbody></table>

## **Integrating Purchasely with AppsFlyer**

The integration requires 2 steps:

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

### 1. Associating users to events

See [iOS](https://dev.appsflyer.com/hc/docs/ios-sdk-reference-appsflyerlib#getappsflyeruid) or [Android](https://dev.appsflyer.com/hc/docs/android-sdk-reference-appsflyerlib#getappsflyeruid) Appsflyer documentation for more information

{% tabs %}
{% tab title="Swift" %}
```swift
Purchasely.setAttribute(.appsflyerId, value: AppsFlyerLib.shared().getAppsFlyerUID())
```
{% endtab %}

{% tab title="Kotlin" %}
```kotlin
AppsFlyerLib.getInstance().getAppsFlyerUID(applicationContext)?.let {
    Purchasely.setAttribute(Attribute.APPSFLYER_ID, it)
}
```
{% endtab %}

{% tab title="Java" %}
```java
Purchasely.setAttribute(
	Attribute.APPSFLYER_ID,
	AppsFlyerLib.getInstance().getAppsFlyerUID(this)
);
```
{% endtab %}

{% tab title="React Native" %}
```
appsFlyer.getAppsFlyerUID((err, appsFlyerUID) => {
  if (err) {
    console.error(err);
  } else {
    Purchasely.setAttribute(Attributes.APPSFLYER_ID, appsFlyerUID);
  }
});
```
{% endtab %}

{% tab title="Cordova" %}
```javascript
var  getUserIdCallbackFn = function(id) {
alert('received id is: ' + id);
}

var appsFlyerId = window.plugins.appsFlyer.getAppsFlyerUID(getUserIdCallbackFn);

Purchasely.setAttribute(Purchasely.Attribute.APPSFLYER_ID, appsFlyerId);
```
{% endtab %}

{% tab title="Flutter" %}
```dart
appsFlyerSdk.getAppsFlyerUID().then((AppsFlyerId) {
  Purchasely.setAttribute(PLYAttribute.appsflyer_id,AppsFlyerId);
});
```
{% endtab %}
{% endtabs %}

### 2. Activating the AppsFlyer integration

The activation requires 3 steps:

1. Retrieving your app id in the AppsFlyer Dashboard (iOS and / or Android)
2. Retrieving your API key in the AppsFlyer Dashboard (iOS and / or Android)
3. Enabling the AppsFlyer integration in the Purchasely Console

#### a. Retrieving your app id in the AppsFlyer Dashboard

1. Go to your AppsFlyer dashboard: https://hq1.appsflyer.com/apps/myapps
2. You will find your app's ids beneath their names

![](https://1658765766-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGgUdOzhqa07uh7nB2iZA%2Fuploads%2FE09DzEJbcaoQQqrl6b1l%2FScreenshot%202022-03-22%20at%2019.15.30.png?alt=media\&token=f129e7cb-bb70-4ab6-89c6-cf794d2638c7)

#### b. Retrieving your API key in the AppsFlyer Dashboard

1. Go to your AppsFlyer dashboard:[ https://hq1.appsflyer.com/apps/myapps](https://hq1.appsflyer.com/apps/myapps)
2. Using the app's top right menu, access your app's settings

![](https://1658765766-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGgUdOzhqa07uh7nB2iZA%2Fuploads%2Fc7qNKrybY8zheKesVk8D%2FScreenshot%202022-03-22%20at%2019.20.48.png?alt=media\&token=4c4ffb0f-96de-46d6-ad78-6ae8c1d1638b)

3\. You will find your app's API key at the top of the page

![](https://1658765766-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGgUdOzhqa07uh7nB2iZA%2Fuploads%2FGhiKqB9AeeOSPQe3rfqd%2FScreenshot%202022-03-22%20at%2019.21.40.png?alt=media\&token=c891a17e-a146-4491-89db-a0f27133de4f)

4\. (optional) repeat for your Android / iOS app





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

1\. Visit [your Purchasely app's console](https://console.purchasely.io)

2\. Access your apps Integrations' settings

![](https://1658765766-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGgUdOzhqa07uh7nB2iZA%2Fuploads%2FCBvzT4ygtxkN2qCWaH8y%2FScreenshot%202022-03-24%20at%2019.06.22.png?alt=media\&token=dce11fe7-52af-4141-9659-01ee0526053f)

3\. Access the AppsFlyer Integration's Settings

4\. Enable the AppsFlyer integration

5\. (iOS) Set your iOS AppsFlyer app's API key & app ID

6\. (Android) Set your Android AppsFlyer app's API key & App ID

![](https://1658765766-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGgUdOzhqa07uh7nB2iZA%2Fuploads%2FfJ2ZoIR7SU9M4B8mDkzg%2FScreenshot%202022-03-24%20at%2019.08.57.png?alt=media\&token=a4f04597-4104-47a3-9325-9e44f1bf76b8)

7\. (optional) Override the name of events sent to AppsFlyer

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

{% hint style="info" %}
Bear in mind that events can take up to 24 hours to show up in your AppsFlyer dashboard.
{% endhint %}
