AppsFlyer SDK Integration - Android V.4.6.0
AppsFlyer SDK Integration - Android V.4.6.0
7 – Help Center
Page Contents:
AppsFlyer's SDK provides app installation and event tracking 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 track installs, updates and sessions (by following the mandatory steps below),
and also track additional in-app events beyond app installs (including in-app purchases,
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 1/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
The mandatory steps are detailed in sections 2 and 3 below, followed by additional
optional features in sections 4 and 5.
The AppsFlyer Android SDK is compatible with Android 2.3 and above.
Advanced Integrations
Android SDK Download
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 2/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
(Mandatory)
You can integrate the AppsFlyer's SDK either automatically using Gradle's Dependency
Management or manually as an SDK.jar.
The simplest way to integrate the SDK into your project is by using Gradle's Dependency
Management.
1. Open your project (or create a new project), and then open your_app |
build.gradle
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 3/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
repositories {
mavenCentral()
}
3. Add the compile dependency with the latest version of the AppsFlyer SDK in
dependencies {
compile 'com.appsflyer:af-android-sdk:4+@aar'
}
Version info can be found here.
Now you can import AppsFlyer's SDK into your project and integrate it as described in
this guide.
import com.appsflyer.AppsFlyerLib
If you are not using Gradle, download and add the AF-Android-SDK.jar to the project's
class path.
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 4/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
AndroidManifest.xml
Android apps cannot have multiple receivers with the same intent- ltered action.
The following two options are available for implementing the install referrer broadcast
receiver:
<!—The AppsFlyer Install Receiver is first and will broadcast to all recei
<receiver android:name="com.appsflyer.MultipleInstallBroadcastReceiver" and
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 5/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
</intent-filter>
</receiver>
<!—All other receivers should follow right after -->
<receiver android:name="com.google.android.apps.analytics.AnalyticsReceiver
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
<receiver android:name="com.admob.android.ads.analytics.InstallReceiver" an
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
In the AndroidManifest.xml, add the following receiver as the FIRST receiver for
INSTALL_REFERRER or after another multiple broadcast receiver, and ensure the
receiver tag is within the application tag:
Collecting the Google Advertising ID (GAID) is essential for tracking campaigns across
several channels including Facebook, Google and Twitter.
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 6/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
1 Install the Google Play Services SDK and import it into your project. For download
details, click here.
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
NOTES:
AppsFlyer uses the Google Play Services library for retrieving the Google Advertising
ID.
Google Play Services 7.5 is the minimum requirement for the GCM Registration
Token (which is used for our Uninstall measurement). AppsFlyer recommends to
always use the latest version.
Although the library provides additional services, if you use ProGuard as part of your
build process, it has a light footprint. We only use the ads packages from Google
Services. If you want to optimize the size of your project you can exclude any other
packages.
Source: https://developer.android.com/google/play-services/setup.html
By default, IMEI and Android ID are not collected by the SDK if the OS version is higher
than KitKat (4.4) and the app contains Google Play Services.
To explicitly send these IDs to AppsFlyer, developers can use the following APIs:
AppsFlyerLib.getInstance().setImeiData("IMEI_DATA_HERE")
AppsFlyerLib.getInstance().setAndroidIdData("ANDROID_ID_DATA_HERE")
If the app does NOT contain Google Play Services the IMEI and Android ID are collected
by the SDK.
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 7/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
Developers can opt-out of collection of IMEI and Android ID by using these APIs:
AppsFlyerLib.getInstance().setCollectIMEI(false)
AppsFlyerLib.getInstance().setCollectAndroidID(false)
NOTE: At least one device identi er should be collected to allow for proper attribution.
NOTE: This is the minimum requirement to begin tracking your app installs.
AppsFlyerLib.getInstance().startTracking(this.getApplication(),"[Dev_Key]"
Replace [Dev_Key] with your own Dev_Key (accessible from your account, see
Settings >> Integrate the SDK into... in the AppsFlyer dashboard).
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 8/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
If your app supports deep linking or has multiple activities or you plan to run retargeting
campaigns, you must implement the steps in section 5.6.
If your app is a utility app running in the background you can use the API as described in
section 5.12 to measure sessions and retention within AppsFlyer.
This API enables AppsFlyer to track post install events. These events are de ned by the
advertiser and include an event name, in addition to optional event values.
Tracking in-app events helps you measure and analyze how loyal users discover your
app, and attribute them to speci c campaigns/media sources. It is recommended to
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 9/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
take the time and de ne the events you want to measure to allow you to track ROI
(Return on Investment) and LTV (Lifetime Value).
Syntax:
AFInAppEventParameterName.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: af_price
AFInAppEventParameterName.PRICE
is not counted as revenue and is a descriptive parameter which does not affect revenue
and LTV measurements.
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 10/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
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.
For details of the AppsFlyer Rich In-App Events for Android, click here.
5. Advanced Integration
The APIs below are optional and are part of the advanced integration with the AppsFlyer
SDK.
You can set a global currency code using the API below, in addition to speci c 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
AFInAppEventParameterName.CURRENCY
is not sent as part of an in-app event.
USD is the default value. You can nd acceptable ISO currency codes here.
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 11/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
AppsFlyerLib.getInstance().setCurrencyCode("GBP");
An AppsFlyer proprietary unique ID is created for every new install of an app. AppsFlyer’s
unique ID is the main ID used by AppsFlyer in the Reports and APIs.
Setting your own customer ID enables you to cross-reference your own unique ID with
AppsFlyer’s unique ID and the other devices’ IDs. This ID is available in AppsFlyer CSV
reports along with Postback APIs for cross-referencing with your internal IDs.
AppsFlyerLib.getInstance().setCustomerUserId("myId");
IMPORTANT NOTES:
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 12/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
You must set the Customer User ID using this API to use AppsFlyer’s integrations
with Analytics platforms such as Mixpanel and Swrve.
AppsFlyer allows you to access the user attribution data in real-time directly at SDK level.
It enables you to customize the landing page a user sees on the very rst app open after
a fresh app install.
AppsFlyer enables you to report one or more of the device’s associated email addresses.
You must collect the email addresses and report it to AppsFlyer according to your
required encryption method.
The following encryption methods are available: Sha1, MD5 and plain.
Example:
AppsFlyerLib.getInstance().setUserEmails(AppsFlyerProperties.EmailsCryptTyp
NOTE : Personally Identi able Information (PII) such as email addresses are not retained
by AppsFlyer and this information is not presented in any reports. The purpose of
collecting this information is solely for postback purposes to the media source.
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 13/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
For each activity used for deep linking*, other than the main activity, add the following
line in the onCreate()
AppsFlyerLib.getInstance().sendDeepLinkData(this);
* Activities that are meant to be opened by deep linking should have the below intent
lter on the activity de nitions in the manifest le.
<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 unique scheme" />
</intent-filter>
AppsFlyer’s SDK provides server veri cation for in‐app purchases. To set purchase
validation tracking, call the validateAndTrackInAppPurchase method inside the
onActivityResult function.
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 14/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
AppsFlyerLib.getInstance().registerValidatorListener(this,new AppsFlyerInAp
public void onValidateInApp() {
Log.d(TAG, "Purchase validated successfully");
}
public void onValidateInAppFailure(String error) {
Log.d(TAG, "onValidateInAppFailure called: " + error);
}
});
Usage Example:
AppsFlyerLib.getInstance().validateAndTrackInAppPurchase(context,publicKey
AppsFlyer provides you a method to opt‐out speci c users from AppsFlyer analytics. This
method complies with the latest privacy requirements and complies with Facebook data
and privacy policies. Default is NO, meaning tracking is enabled by default.
Use this API during the SDK initialization in Section 4 to explicitly opt-out:
AppsFlyerLib.getInstance().setDeviceTrackingDisabled(true);
IMPORTANT NOTE: Google Play is the default store. If you are publishing your app only
on Google Play, skip this section.
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 15/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
Examples:
Amazon:
For more details on how to track installs for out-of-store apps, read here.
Add the following permissions to the manifest. (Apps using push noti cations should
already have these permissions)
<permission android:name="YOUR-PACKAGE-NAME.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="<your-package-name>.permission.C2D_MESSA
<uses-permission android:name="com.google.android.c2dm.permission.
RECEIVE" />
NOTE: Enter the correct name of your package.
Add the following receiver before the </application> tag:
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 16/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
<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>
<service android:name="com.appsflyer.InstanceIDListener" android:exported=
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
Set the GCM Project Number on your main activity: Add this line immediately before the call to the
startTracking():
public void setGCMProjectNumber(Context context, String id);
Usage Example:
AppsFlyerLib.getInstance().setGCMProjectNumber(this, '1234567890');
To complete this process fully and correctly, you must read here.
OneLink triggers an app to open at the deep Link location by mentioning the scheme
under the af_dp parameter.
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 17/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
This is a recommended API for utility apps that want to measure user retention while
running in the background.
AppsFlyer allows you to measure push noti cations as part of a retargeting campaign.
To enable this feature, call the next method inside the onCreate method of every Activity
which will be launched upon clicking the noti cation:
AppsFlyerLib.getInstance().sendPushNoti cationData(this);
The data payload should include an object: "af" with the relevant key-value string:
To test the SDK integration before and after submitting to the Google Play Store click
here.
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 18/19
12/11/2018 AppsFlyer SDK Integration - Android v. 4.6-4.7 – Help Center
7. Known Issues
If you are using ProGuard and you encounter a warning regarding our
AFKeystoreWrapper class, then add the following code to your ProGuard rules le:
-dontwarn com.appsflyer.AFKeystoreWrapper
https://support.appsflyer.com/hc/en-us/articles/218348343-AppsFlyer-SDK-Integration-Android-v-4-6-4-7 19/19