Xamarin Binding integration guide For Android
AppsFlyer Xamarin Binding version v6.13.1
Built with AppsFlyer Android SDK v6.13.1
We have renamed some of the APIs. For more details, please check out our Help Center
AppsFlyer SDK provides app installation and event logging functionality. We have developed an SDK that is highly robust (7+ billion SDK installations to date), secure, lightweight and very simple to embed.
You can measure installs, updates and sessions and also log additional in-app events beyond app installs (including in-app purchases, game levels, etc.) to evaluate ROI and user engagement levels.
AppsFlyer Xamarin binding provides application installation and events logging functionality.
The API for the binding coincides with the native Android API, which can be found here.
Install-Package AppsFlyerXamarinBindingAndroid
https://www.nuget.org/packages/AppsFlyerXamarinBindingAndroid/
1. Go to Project > Add NuGet Packages...
2. Select the `AppsFlyerXamarinBindingAndroid`
3. Select under version - `6.12.2`
4. Click `Add Package`
To Embed SDK into your Application Manually:
1. Copy AppsFlyerXamarinBindingAndroid.dll into your project.
2. On Xamarin Studio go to References and click on Edit References.
3. Go to .Net Assembly tab and click on Browse… button
4. Locate AppsFlyerXamarinBindingAndroid.dll and chose it.
5. Locate GooglePlayServicesLib.dll and add it as well (for advertising Id)
The AndroidManifest.xml should include the following permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- Optional : -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
2.3.1 Using Broadcast Receiver (Note: this method of passing the referrer is deprecated by Google since March 1, 2020. Please see instructions on how to include install_referrer library below )
The following two options are available for implementing the install referrer broadcast receiver:
Using a Single Broadcast Receiver If you do not have a receiver listening on the INSTALL_REFERRER, in the AndroidManifest.xml, add the following receiver within the application tag:
<receiver android:name="com.appsflyer.SingleInstallBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
Using a Multiple Broadcast Receiver
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:
<receiver android:name="com.appsflyer.MultipleInstallBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
- Download aar of the desired version of com.android.installreferrer library from Maven
- Add it to your Android Xamarin project, for example under the lib folder
- Set Build Action to AndroidAarLibrary
- Make sure you can see relevant ItemGroup attribute in the csproj file
Go to your MainActivity.cs and add:
-
using Com.Appsflyer;
at the top of the file. -
Add the following code to the OnCreate() method:
AppsFlyerLib.Instance.Init("YOUR_DEV_KEY", new AppsFlyerConversionDelegate(this), this.Application);
AppsFlyerLib.Instance.Start(this.Application, "YOUR_DEV_KEY");
/* AppsFlyerLib.Instance.SetDebugLog(true); */
Logging in-app events is performed by calling LogEvent
with event name and value parameters. See In-App Events documentation for more details.
Purchase Event Example:
Dictionary<string, Java.Lang.Object> eventValues = new Dictionary<string, Java.Lang.Object>();
eventValues.Add(AFInAppEventParameterName.Price , 2);
eventValues.Add(AFInAppEventParameterName.Currency, "USD");
eventValues.Add(AFInAppEventParameterName.Quantity, "1");
AppsFlyerLib.Instance.LogEvent(this.BaseContext, AFInAppEventType.Purchase , eventValues);
For Conversion data your should call this method:
AppsFlyerLib.RegisterConversionListener (this, new AppsFlyerConversionDelegate ());
To access AppsFlyer's conversion data from the Android SDK implement the ConversionDataListener:
public class AppsFlyerConversionDelegate : Java.Lang.Object, IAppsFlyerConversionListener {
public AppsFlyerConversionDelegate()
{
Console.WriteLine("AppsFlyerConversionDelegate called");
}
public void OnAppOpenAttribution(IDictionary<string, string> p0)
{
Console.WriteLine("OnAppOpenAttribution = " + p0.ToString());
}
public void OnAttributionFailure(string p0)
{
Console.WriteLine("OnAttributionFailure = " + p0);
}
public void OnInstallConversionDataLoaded(IDictionary<string, string> p0)
{
foreach (var kvp in p0){
Console.WriteLine(kvp.Key + " = " + kvp.Value);
}
}
public void OnInstallConversionFailure(string p0)
{
Console.WriteLine("OnInstallConversionFailure = " + p0);
}
}
AppsFlyerConversionDelegate.cs can be found in the sample app provided with this guide.
-
Add relevant intent filters to the activity.
a. You can read more about intent filters for deep linking here: https://developer.android.com/training/app-links/deep-linking.html
b. You can read more on implementing Android Manifest by adding custom attributed in Xamarin here: https://docs.microsoft.com/en-us/xamarin/android/platform/android-manifest
Example intent filter attribute implementation:
[Activity(Label = "appsflyerxamarinandroidsampleapp", MainLauncher = true, Icon = "@mipmap/icon")]
[IntentFilter(new[] { Android.Content.Intent.ActionView },
AutoVerify = true,
Categories = new[]
{
Android.Content.Intent.CategoryDefault,
Android.Content.Intent.CategoryBrowsable
},
DataScheme = "https",
DataHost = "domain.com",
DataPathPrefix = "/path/")]
public class MainActivity : Activity
{...}
More information on Deep Linking on Android can be found here:
AppsFlyer enables you to measure app uninstalls.
To complete this process fully and correctly, you must read here.
-
Add the Xamarin Google Play Services - FCM Nugget to your project as instructed in the oficial Xamarin documentation.
-
Make sure to add google-services.json into your project
3.1 Developers who first use Firebase for AppsFlyer uninstall measurement
If a developer integrates FCM for the sole purpose of measurement uninstalls with AppsFlyer, they can make use of the appsFlyer.FirebaseMessagingServiceListener service, included in our SDK. If you already use Firebase Messaging in your app, skip to step 3.2
Add the following receiver to your AndroidManifest.xml file:
<application
<!-- ... -->
<service
android:name="com.appsflyer.FirebaseMessagingServiceListener">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<!-- ... -->
</application>
The appsflyer.FirebaseMessagingServiceListener extends Firebase's <>FirebaseMessagingService class, which is used in order to receive Firebase's Device Token.
3.2 Developers who already use Firebase for other third-party platforms
If a developer intends to use FCM with more than one platform / SDK, they would need to implement a logic that collects the Device Token and passes it to all relevant platforms. This is done by extending a new instance of the FirebaseMessagingService (similar to the service the AppsFlyer SDK extends):
using System;
using Android.App;
using Com.Appsflyer;
using Firebase.Messaging;
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyNewFirebaseManager : FirebaseMessagingService
{
public override void OnNewToken(string newToken)
{
base.OnNewToken(newToken);
// Sending new token to AppsFlyer
Console.WriteLine("MyNewFirebaseManager onNewToken");
AppsFlyerLib.Instance.UpdateServerUninstallToken(ApplicationContext, newToken);
// the rest of the code that makes use of the token goes in this method as well
}
}
- Add your Server Key to AppsFlyer's dashboard.
For more information regarding how to obtain the Server Key, please review this article : https://support.appsflyer.com/hc/en-us/articles/208004986-Android-Uninstall-Tracking
AppsFlyerLib.Instance.SetCustomerUserId("custom_id");
AppsFlyerLib.Instance.SetDebugLog(true);
AppsFlyerLib.Instance.GetAppsFlyerUID(this.BaseContext);
For AppsFlyer to count two separate sessions, the default time between each session must be at least 5 seconds. A session commences when the user opens the app. If you want to configure a different time between sessions, use the following API:
AppsFlyerLib.Instance.SetMinTimeBetweenSessions(int time);
AppsFlyerLib.Instance.AnonymizeUser(true);
In some extreme cases you might want to shut down all SDK attribution functions due to legal and privacy compliance. This can be achieved with the Stop() API. Once this API is invoked, our SDK will no longer communicate with our servers and stop functioning.
AppsFlyerLib.Instance.Stop(true, this.BaseContext);
It is possible to delay the SDK Initialization until the customerUserID is set. This feature makes sure that the SDK doesn't begin functioning until the customerUserID is provided. If this API is used, all in-app events and any other SDK API calls are discarded, until the customerUserID is provided and logged.
AppsFlyerLib.Instance.WaitForCustomerUserId(true);
AppsFlyerLib.Instance.SetCustomerIdAndLogSession("custom_id", this.BaseContext);
AppsFlyerLib.Instance.SetPreinstallAttribution(string mediaSource, string campaign, string siteId);
As part of the EU Digital Marketing Act (DMA) legislation, big tech companies must get consent from European end users before using personal data from third-party services for advertising.
Read more here
//Automatic collection
AppsFlyerLib.Instance.EnableTCFDataCollection(true); // When using CMP, the SDK will collect the consent data
// OR
// Manual collection
// hasConsentForDataUsage (1st argument): Indicates whether the user has consented to use their data for advertising purposes.
// hasConsentForAdsPersonalization (2nd argument): Indicates whether the user has consented to use their data for personalized advertising purposes.
AppsFlyerConsent consent = AppsFlyerConsent.ForGDPRUser(true, true);
// or
// If GDPR doesn’t apply to the user
AppsFlyerConsent consent = AppsFlyerConsent.ForNonGDPRUser();
AppsFlyerLib.Instance.SetConsentData(consent);
...
AppsFlyerLib.Instance.Start(this.Application, "xXxXxXx");
Sample app can be found here: https://github.com/AppsFlyerSDK/XamarinAndroidBinding/tree/master/XamarinSample
In order for us to provide optimal support, we would kindly ask you to submit any issues to support@appsflyer.com.
When submitting an issue please specify your AppsFlyer sign-up (account) email, your app ID, production steps, logs, code snippets and any additional relevant information.