Monaca Docs
Search…
AppsFlyer
AppsFlyer is the market leader in mobile advertising attribution and analytics, helping marketers to pinpoint their targeting, optimize their ad spend and boost their ROI (Return on Investment).
AppsFlyer allows users to monitor and track application installations, downloads, and conversions. The AppsFlyer API allows developers to access and integrate the functionality of AppsFlyer with other applications.

Prerequisite

In order to enable AppsFlyer to start tracking your app, you are required to have the following two information such as:
  1. 1.
    devKey: Your application devKey provided by AppsFlyer.
  2. 2.
    appId: (For iOS only) Your iOS app ID in the App Store.

Adding AppsFlyer to Monaca Project

  1. 1.
    For Monaca Cloud IDE, go to Configure → Service Integration Settings.
  2. 2.
    Click on Details button of AppsFlyer service.
  3. 3.
    Then, click Install button to add it into your project.
4. You will be asked to confirm the setup. Click to start the installation.

Configuration

Add the following lines to your code to initialize the tracking with your own AppsFlyer devKey and appId:
1
document.addEventListener("deviceready", function(){
2
3
var options = {
4
devKey: 'xxXXXXXxXxXXXXxXXxxxx8' // your AppsFlyer devKey
5
};
6
7
var userAgent = window.navigator.userAgent.toLowerCase();
8
9
if (/iphone|ipad|ipod/.test(userAgent)) {
10
options.appId = "123456789"; // your ios app id in app store
11
}
12
13
window.plugins.appsFlyer.initSdk(options);
14
15
}, false);
Copied!

Usage

SDK Initialization

Initialize the SDK.
1
initSdk(options, onSuccess, onError): void
Copied!
Parameter
Name
Type
Description
options
Object
SDK configuration (please refer to the options object table below)
onSuccess
(message: string) => void
(optional) Success callback: called after a successful SDK initialization.
onError
(message: string) => void
(optional) Error callback: called when error occurs during initialization.
options object
Name
Type
Default
Description
devKey
String
appId
String
(For iOS only) Your iOS app ID in the App Store
isDebug
Boolean
false
(optional) Debug mode
onInstallConversionDataListener
Boolean
false
Accessing AppsFlyer Attribution/Conversion Data from the SDK (Deferred Deeplinking). AppsFlyer plugin will return attribution data in onSuccess callback. For more information, please refer to:
Example
The following snippet shows how to use initSdk() function:
1
var onSuccess = function(result) {
2
//handle result
3
};
4
5
function onError(err) {
6
// handle error
7
}
8
9
var options = {
10
devKey: 'd3Ac9qPardVYZxfWmCspwL',
11
appId: '123456789',
12
isDebug: false,
13
onInstallConversionDataListener: true
14
};
15
16
window.plugins.appsFlyer.initSdk(options, onSuccess, onError);
Copied!

In-App Events Tracking API

Allow you to send in-app events to AppsFlyer analytics. This method allows you to add events dynamically by adding them directly to the application code. These in-app events help you track how loyal users discover your app, and attribute them to specific campaigns/media-sources. Please take the time to define the event(s) you want to measure to allow you to track ROI (Return on Investment) and LTV (Lifetime Value).
1
trackEvent(eventName, eventValues): void (optional)
Copied!
Parameter
Name
Type
Description
eventName
String
Custom event name, is presented in your dashboard.
eventValue
Object
Event details
Example
The following snippet shows how to use trackEvent() function:
1
var eventName = "af_add_to_cart";
2
3
var eventValues = {
4
"af_content_id": "id123",
5
"af_currency":"USD",
6
"af_revenue": "2"
7
};
8
9
window.plugins.appsFlyer.trackEvent(eventName, eventValues);
Copied!

Currency Code Setting

Change the currency code.
1
setCurrencyCode(currencyId): void
Copied!
Parameter
Name
Type
Default
Description
currencyId
String
USD
Example
The following snippet shows how to use setCurrencyCode() function:
1
window.plugins.appsFlyer.setCurrencyCode("USD");
2
window.plugins.appsFlyer.setCurrencyCode("GBP"); // British Pound
Copied!

Customer User ID Setting (Advanced)

Set your own custom ID. This enables you to cross-reference your own unique ID with AppsFlyer’s user ID and the other devices’ IDs. This ID is available in AppsFlyer CSV reports along with postbacks APIs for cross-referencing with you internal IDs.
The ID must be set during the first launch of the app at the SDK initialization. The best practice is to call this API during the deviceready event, where possible.
1
setAppUserId(customerUserId): void
Copied!
Parameter
Name
Type
Description
customerUserId
String
Your custom ID
Example
The following snippet shows how to use setAppUserId() function:
1
window.plugins.appsFlyer.setAppUserId(userId);
Copied!

GCM Project Number Setting

Set a GCM Project Number in order to enable app uninstall tracking for Android platform.
1
setGCMProjectID(GCMProjectNumber): void
Copied!
Parameter
Name
Type
Description
GCMProjectNumber
String
GCM Project number. It is obtained through your google developer console. For more information, please refer to Android Uninstall Measurement.

Uninstall Tracking

Set your iOS device token in order to enable app uninstall tracking for iOS platform.
1
registerUninstall(token): void
Copied!
Parameter
Name
Type
Description
token
String
Your iOS device token. You can get your device token from UnityEngine.iOS.NotificationServices.deviceToken. For more information, please refer to Unity and iOS Uninstall Measurement.

Getting AppsFlyer's Device ID

Get AppsFlyer’s proprietary Device ID. The AppsFlyer Device ID is the main ID used by AppsFlyer in Reports and APIs.
1
getAppsFlyerUID(getUserIdCallbackFn): void
Copied!
Parameter
Name
Type
Description
getUserIdCallbackFn
() => void
Success callback
Example
The following snippet shows how to use getAppsFlyerUID() function:
1
var getUserIdCallbackFn = function(id) {
2
alert('received id is: ' + id);
3
}
4
window.plugins.appsFlyer.getAppsFlyerUID(getUserIdCallbackFn);
Copied!
Track deeplinks with AppsFlyer attribution data (for iOS only).
For Android version 4.2.5 and higher, the deeplinking metadata (scheme/host) is sent automatically.
1
handleOpenUrl(url): void
Copied!
Parameter
Name
Type
Description
url
String
Url
Example
The following snippet shows how to use handleOpenUrl() function:
1
var handleOpenURL = function(url) {
2
window.plugins.appsFlyer.handleOpenUrl(url);
3
}
Copied!
Last modified 4mo ago