AppsFlyer’s Unity Plugin

You can find the plugin on Github here: https://github.com/AppsFlyerSDK/Unity

Set out below are the integration instructions for using AppsFlyer’s Unity Plugin.

Installation instructions for the AppsFlyer's plugin:

  1. Import the AppsFlyerUnityPlugin.unitypackage into your Unity project.  
  2. Go to Assets >> Import Package >> Custom Package
  3. Select AppsFlyerUnityPlugin.unitypackage file.
  4. In your Start / Init methods add the following code:
void Start () {
// For detailed logging
//AppsFlyer.setIsDebug (true);
//Mandatory - set your AppsFlyer’s Developer key.
AppsFlyer.setAppsFlyerKey ("YOUR_APPSFLYER_DEV_KEY_HERE");
//Mandatory - set your apple app ID
//NOTE: You should enter the number only and not the "ID" prefix
   AppsFlyer.setAppID ("YOUR_APP_ID_HERE");
   AppsFlyer.trackAppLaunch ();
//Mandatory - set your Android package name
//Mandatory - set your AppsFlyer’s Developer key.
AppsFlyer.init ("YOUR_DEV_KEY");

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

Getting Conversion Data

To load AppsFlyer's conversion data from its servers:

  1. Add an empty GamObject
  2. Attach to it the AppsFlyerTrackerCallbacks.cs which is included in the project (you must name this gameobject AppsFlyerTrackerCallbacks).  


//For getting the conversion data in Android, you need to this listener.

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

public void didReceiveConversionData(string conversionData) {
   print ("AppsFlyerTrackerCallbacks:: got conversion data = " + conversionData);

In App Purchase Receipt Validation


For testing purposes, ensure you test against the Apple sandbox server call:

AppsFlyer.validateReceipt(string productIdentifier, string price, string currency,
string transactionId, Dictionary additionalParametes);


For Android call:

//To get the callbacks
//AppsFlyer.createValidateInAppListener ("AppsFlyerTrackerCallbacks", "onInAppBillingSuccess", "onInAppBillingFailure");

AppsFlyer.validateReceipt(string publicKey, string purchaseData, string signature, string price, string currency, Dictionary additionalParametes);

Call the listener (for Android only):

AppsFlyer.createValidateInAppListener ("AppsFlyerTrackerCallbacks", "onInAppBillingSuccess", "onInAppBillingFailure");

(The purchaseData variable should hold the original JSON from the purchase).

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

Track Event API:

Tracking Event Example:

System.Collections.Generic.Dictionary<string, string> purchaseEvent = new   
System.Collections.Generic.Dictionary<string, string> ();
purchaseEvent.Add ("af_currency", "USD");
purchaseEvent.Add ("af_revenue", "0.99");
purchaseEvent.Add ("af_quantity", "1");
AppsFlyer.trackRichEvent ("af_purchase", purchaseEvent);

Setting user local currency code for in app purchases: The currency code should be a 3 character ISO 4217 code. (default is USD)


Settings the user ID as used by the app:



Set the AF receiver and permissions (Mandatory):

// receiver should be inside the <application> tag
<receiver android:name="com.appsflyer.MultipleInstallBroadcastReceiver" android:exported="true">
<action android:name="com.android.vending.INSTALL_REFERRER" />
//Mandatory permission:
<uses-permission android:name="android.permission.INTERNET" />



iOS, configurations for deeplinking are performed inside XCode.

The plugin also supports Universal Links.

Please follow the native iOS Integration Guide.

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.


Add the following to your manifest file:

<activity android:name="com.appsflyer.GetDeepLinkingActivity" android:exported="true">
   <action android:name="android.intent.action.VIEW" />
   <category android:name="android.intent.category.DEFAULT" />
   <category android:name="android.intent.category.BROWSABLE" />
   <data android:scheme="your_scheme" />



Add these lines to your manifest:


 <uses-permission android:name="android.permission.WAKE_LOCK" />
   <permission android:name="android.appsflyer.sampleapp.permission.C2D_MESSAGE"
       android:protectionLevel="signature" />
   <uses-permission android:name="android.appsflyer.sampleapp.permission.C2D_MESSAGE" />
   <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

NOTE:  You must replace android.appsflyer.sampleapp with the bundle identifier of your application.


               <action android:name="com.google.android.c2dm.intent.RECEIVE" />

Call this method:


GCM Project number is obtained through your google developer console.

For more details please refer to our Android Uninstall guide.


Call this method when you receive your device token:


You can get your device token from UnityEngine.iOS.NotificationServices.deviceToken.


void Start () {
   // register to push notifications
       NotificationServices.RegisterForRemoteNotificationTypes (RemoteNotificationType.Alert |  RemoteNotificationType.Badge |  RemoteNotificationType.Sound);
       Screen.orientation = ScreenOrientation.Portrait;
}   void Update () {
       if (!tokenSent) { // tokenSent needs to be defined somewhere (bool tokenSent = false)
           byte[] token = UnityEngine.iOS.NotificationServices.deviceToken;           
if (token != null) {     
               AppsFlyer.registerUninstall (token);
               tokenSent = true;

For more details please refer to our iOS Uninstall Guide.



Additional Links

SDK integration guides for complete API documentation:

iOS Integration Guide.

Android Integration Guide.

Unity Sample Project:

Sample Project.

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request
Powered by Zendesk