1. Overview

You can track installs, updates and sessions and also track post-installs events (including in-app purchases, game levels, etc.) to evaluate ROI and user engagement levels.

Mobile apps, that are developed on the Unity platform, can enjoy integrating AppsFlyer's SDK once and tracking both Android and iOS generated apps. The following guide details how to integrate AppsFlyer's SDK into your Unity code for your iOS and Android apps.

Note

AppsFlyer supports integration with Unity OS version 5 and above

Important!

Google Play Install Referrer API is supported starting from Unity Plugin v 4.16.0. If you want to use the new Referrer API, update the plugin to version 4.16.0 or above.

If you already have a receiver listening on the INSTALL_REFERRER, AppsFlyer provides a solution that broadcasts INSTALL_REFERRER to all other receivers automatically. In the AndroidManifest.xml, add the following receiver as the FIRST receiver for INSTALL_REFERRER, and ensure the receiver tag is within the application tag:

iOS Setup

After building the project in Unity for xCode, you must add Security.Framework to xCode's Linked Frameworks and Libraries if the framework was not added previously.

iOS Apple Search Ads

Add the following:

AdSupport.framework

AppsFlyer collects IDFA only if you include this framework. Failure to add this framework means that you cannot track Facebook, Twitter and most other ad networks.

iAd.framework

Adding this framework to your app project is highly recommended, since it is mandatory for tracking Apple Search Ads.

3. SDK Initialization

In your Start / Init methods you set the AppsFlyer dev key and the unique app IDs used by iTunes and Google Play. Note that you need to set the iOS app ID here with digits only, without the "id" prefix.

Important!

For Android, AppsFlyer.init includes calling trackAppLaunch. Therefore, do not call trackAppLaunch in the Android build of the Unity plugin.

In iOS, the conversion data response is triggered in the AppsFlyerTrackerCallbacks.cs class.

4. Tracking In-App Events

In-App Events provide insight on what is happening in your app. It is recommended to take the time and define the events you want to measure to allow you to track ROI (Return on Investment) and LTV (Lifetime Value).

Tracking in-app events is performed by calling trackEvent with event name and value parameters. See In App Events documentation for more details.

Note

An In-App Event name must be no longer than 45 characters. Events names with more than 45 characters do not appear in the dashboard, but only in the raw Data, Pull and Push APIs.

NOTE:

The Unity plugin utilizes an AppController with the IMPL_APP_CONTROLLER_SUBCLASS flag. If your project contains additional plugins which use that flag, you should merge all code into one AppController.

6. Tracking Revenue

Use the AFInAppEvents.REVENUE event parameter to count revenue as part of an in-app event. You can populate it with any numeric value, positive or negative.

Note

AFInAppEvents.REVENUE(equivalent to using af_revenue) is the ONLY event parameter that is counted on AppsFlyer as real revenue on the raw data and dashboard. For more details please click here.

7. Get Conversion Data

AppsFlyer allows you to access the user conversion data in real-time directly at SDK level. It enables you to customize the landing page a user views on the very first app open following a fresh app install.

To load AppsFlyer's conversion data from its servers:

Add an empty GameObject

Attach to it the AppsFlyerTrackerCallbacks.cs which is included in the project (you must name this gameobject AppsFlyerTrackerCallbacks).

Android Unity Plugin > 4.15.1Android Unity Plugin < 4.14.3 iOS

/*For getting the conversion data in Android, you need to add this listener. to the init() method*/AppsFlyer.init ("YOUR_DEV_KEY","AppsFlyerTrackerCallbacks");

In Android, you can move the methods from AppsFlyerTrackerCallbacks.cs to another class, and call that class in your listener.

You must use the exact same method namespaces that appear on AppsFlyerTrackerCallbacks.

/*For getting the conversion data in Android, you need to add this listener.*/AppsFlyer.loadConversionData("AppsFlyerTrackerCallbacks");

In Android, you can move the methods from AppsFlyerTrackerCallbacks.cs to another class, and call that class in your listener.

You must use the exact same method namespaces that appear on AppsFlyerTrackerCallbacks.

/* For getting the conversion data. This is triggered on AppsFlyerTrackerCallbacks.cs file */ AppsFlyer.getConversionData ();

Example from the AppsFlyerTrackerCallbacks.cs class (implement your logic in this method) -

8. User Identifiers

Get AppsFlyer Device ID

AppsFlyer's unique device ID is created for every new install of an app. Use the following API to obtain AppsFlyer’s Unique ID:

public String getAppsFlyerId();

Usage Example:

string AppsFlyerUID = AppsFlyer.getAppsFlyerId();

Set Customer User ID

Set the User ID as used by the app.

To set the user ID, call the following method:

AppsFlyer.setCustomerUserID("someId");

Note

The customerUserID SHOULD be set before the trackAppLaunch/init. It is recommended to set your Customer User ID as soon as possible as it is only associated to events reported after its setup. Customer User ID can also be used to complete integrations with Analytics platforms such as Mixpanel and Swrve.

Set User Email

Google Advertising ID

From SDK Version 4.8.0 AppsFlyer automatically collects the google_advertising_id. The requirement to collect the Google Advertising ID is only relevant for SDK versions 4.7.X and below.

IMEI and Android ID

By default, IMEI and Android ID are not collected by the SDK if the OS version is higher than KitKat (4.4) and the device contains Google Play Services (on Unity SDK versions 4.16.4 and below the specific app needed GPS).

To explicitly send these IDs to AppsFlyer, developers can use the following APIs:

AppsFlyer.setAndroidIdData(string)AppsFlyer.setImeiData(string)

If the app does NOT contain Google Play Services, the IMEI and Android ID are collected by the SDK. However, apps with Google play services should avoid IMEI collection as this is in violation of the Google Play policy.

Developers can opt-out of collection of IMEI and Android ID by using these APIs:

AppsFlyer.setCollectAndroidID(bool)
AppsFlyer.setCollectIMEI(bool)

Warning

At least one device identifier, GAID, Android ID or IMEI, MUST be collected to allow for proper attribution.

IDFA and IDFV

AppsFlyer automatically collects the IDFA (ID For Advertisers) and IDFV (ID For Vendors) if AdSupport.framework is included in the app.

9. Optional Features

Track Uninstalls

For Uninstall, follow the instructions according to your operating system.

Warning

Users who implement the Unity Firebase SDK should not add the following method call to enableUninstallTracking(“SenderID”) as the Firebase SDK will obtain the sender ID from the google-services.json file we have added earlier. Adding this method might cause a debug warning from Android.

Important!

Firebase Cloud Messaging (FCM) is the new version of GCM: It inherits the reliable and scalable GCM infrastructure, plus new features. If you are integrating messaging in a new app, start with FCM. GCM users are strongly recommended to upgrade to FCM, in order to benefit from new FCM features today and in the future.

Note

Cross Promotion Tracking

User Invite Tracking

Currently unavailable in Unity.

Set Currency Code

You can set a global currency code using the API below, in addition to specific currency codes that can be used as part of each in-app event sent to AppsFlyer. The global currency code is used when af_currency

AFInAppEvents.CURRENCY

is not sent as part of an in-app event.

USD is the default value. You can find acceptable ISO currency codes here.

Use the following API to set the currency code:

public void setCurrencyCode(String currencyCode);

Usage Example:

setCurrencyCode(string currencyCode)

In-App Purchase Validation

For In-App Purchase Receipt Validation, follow the instructions according to your operating system.

The validate purchase response is triggered in the AppsFlyerTrackerCallbacks.cs class

Anonymize User Data

AppsFlyer provides you with a method to anonymize specific user identifiers in AppsFlyer analytics. This method complies with the latest privacy requirements and complies with Facebook data and privacy policies. Default is NO, meaning no anonymization is performed by default.

Use this API during the SDK Initialization to explicitly anonymize a user's installs, events and sessions:

public void setDeviceTrackingDisabled(boolean isDisabled);

Usage Example:

AppsFlyer.setDeviceTrackingDisabled(true);

Tracking can be restarted by calling deviceTrackingDisabled again set to false.

Warning

Opting out users SEVERELY hurts your attribution information. Use this option ONLY for regions which legally prevent you from collecting your users' information.

Custom Time Between Sessions

Currently unavailable in Unity.

Background Sessions for Utility Apps

Currently unavailable in Unity.

Track Out-of-Store Apps

Note

Google Play is the default store. If you are publishing your app only on Google Play, skip this section.

To track installs out of Google Play, set the channel/store at the app’s AndroidManifest.xml with a unique channel or any store name for each APK. The CHANNEL value is case sensitive.

Note

You must configure the CHANNEL value at the AppsFlyer dashboard when setting up the app.

Place the meta-data tag before the </application> tag.

For more details on how to track installs for out-of-store apps, read here.

Opt Out

In some extreme cases you might want to shut down all SDK tracking due to legal and privacy compliance. This can be achieved with the isStopTracking API. Once this API is invoked, our SDK will no longer communicate with our servers and stop functioning.

AppsFlyer.stopTracking(true);

In any event, the SDK can be reactivated by calling the same API, but to pass false.

Warning

Use this API only in cases where you want to fully ignore this user from any and all tracking. Using this API SEVERELY impacts your reporting and attribution.

Setting Additional Data

The setAdditionalData API is required to integrate on the SDK level with several external partner platforms, including Segment, Adobe and Urban Airship. Use this API only if the integration article of the platform specifically states setAdditionalData API is needed.The following is a code example for implementing setAdditionalData on Unity:

Comments

We have added the stopTracking API for opting out users as part of the GDPR compliance. In addition, we have limited the collection of IMEI only in cases where the device does not have Google Play Services and exposed setCollectIMEI API to allow you to not collect IMEI at all, no matter what.