Unity

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);
#if UNITY_IOS
//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 ();
#elif UNITY_ANDROID
//Mandatory - set your Android package name
 AppsFlyer.setAppID ("YOUR_ANDROID_PACKAGE_NAME_HERE");
//Mandatory - set your AppsFlyer’s Developer key.
AppsFlyer.init ("YOUR_DEV_KEY");
#endif
}

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

Android

//For getting the conversion data in Android, you need to 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.

iOS

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

iOS:

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

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

Android:

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)

AppsFlyer.setCurrencyCode("GBP");

Settings the user ID as used by the app:

AppsFlyer.setCustomerUserID("someId");

Android:

Set the AF receiver and permissions (Mandatory):

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

Deeplinking

iOS:

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.

Android:

Add the following to your manifest file:

<activity android:name="com.appsflyer.GetDeepLinkingActivity" android:exported="true">
 <intent-filter>
   <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" />
 </intent-filter>
</activity>

Uninstall

Android:

Add these lines to your manifest:

Permissions:

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

Receiver:        

<receiver
           android:name="com.google.android.gms.gcm.GcmReceiver"
           android:exported="true">
           <intent-filter>
               <action android:name="com.google.android.c2dm.intent.RECEIVE" />
           </intent-filter>
       </receiver>

Call this method:

AppsFlyer.setGCMProjectNumber("Your_GCM_Project_Number");

GCM Project number is obtained through your google developer console.

For more details please refer to our Android Uninstall guide.

iOS:

Call this method when you receive your device token:

AppsFlyer.registerUninstall("device_push_notification_token");

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

Example:

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