Documentation
Search…
Integration
Connect the app's miles® SDK to all of your app's features to start driving your users and build retention.
Android Integration
🚩
You now have the app's miles banner up and running.
Next, you need to integrate the SDK deeper into your application so that your user can: -
🧠
understand how the program works. -
🔎
discover all the functionalities that you slaved away at developing ! -
🥕
be rewarded for consistently using your application's main features.

I. Connection

1. Onboarding

Skip to the Connect section if you intend to have everyone participate in the program by default
An onboarding example

Purpose

The first thing that the user will see when clicking on the app's miles banner is the onboarding.
This onboarding has two purposes:
    🎓
    Present and explain the program
    Ask the user their consent to take part in the program

Onboarding choices

There are up to 3 buttons on the onboarding, and you must attach a listener in order to redirect the user depending on their input.
The input of the user will affect the variable OPT_IN in the following way: (default: APM_DEVICE_OPT_IN_NOT_SET)
    👍
    OK: APM_DEVICE_OPT_IN_ACCEPT
    Later: APM_DEVICE_OPT_IN_NOT_SET
    👎
    Never: APM_DEVICE_OPT_IN_REFUSE

Example with AbstractActivity's lifecycle.

1
public class AbstractActivity extends FragmentActivity implements APMDeeplinkListener {
2
3
@Override
4
protected void onResume() {
5
super.onResume();
6
// Start listening
7
APMDeeplinkUtils.getInstance().addListener(this);
8
}
9
10
@Override
11
protected void onPause() {
12
super.onPause();
13
// Stop listening
14
APMDeeplinkUtils.getInstance().removeListener(this);
15
}
16
17
@Override
18
public void apmDeeplinkOnboardingButtonOkClicked() {
19
// User clicked "OK" -> "I want to participate".
20
// TODO: If user not connected, redirect to login/create-account page.
21
// Nothing to do if user connected
22
}
23
24
@Override
25
public void apmDeeplinkOnboardingButtonLaterClicked() {
26
// User clicked "Later" -> "I'll decide later".
27
// The SDK will continue showing the onboarding when clicking the banner
28
// Nothing to do here in general
29
}
30
31
@Override
32
public void apmDeeplinkOnboardingButtonNeverClicked() {
33
// User clicked "Never" => "Stop asking me, hide the banner"
34
// Note: The user may change his mind, and it is recommended to have a button
35
// that allows the user to get back in the program
36
// TODO: (Optional) You can redirect to the page with that "Get back in the program" button
37
// (with a little message saying "You can always change your mind by clicking this button")
38
}
39
40
@Override
41
public void apmDeeplinkActionButtonGoClicked(String url) {
42
//Not used for onboarding
43
}
44
45
@Override
46
public void apmDeeplinkClicked(String url) {
47
//Not used for onboarding
48
}
49
}
Copied!

Connected or non-connected

There can be two different onboardings:
    for non-connected users
    for connected users
Both of these onboardings only appear if the user has not decided whether they want to opt in or not.
An onboarding only appears if the optIn value is NOT_SET. If you want to force the optIn and still wish to present the program, it is better to use tutorials or walkthroughs (both can be configured in our BackOffice and require no code).

2. Connect

In order to offer a seamless experience, app's miles doesn't have a login or create-account page. The login/create-account process for app's miles is parallel to yours.

APMUIServicesUser.userClientConnect()

This method can both connect and create an account:
    If an app's miles account already exists with the given userID, the method signs them in.
    If no account exists, the method will automatically create their account and sign them in.
This method needs : « email », « userID » and « optIn » « email » is the email of user « userID » is a unique identifier in your database – « optIn » is the RGPD OptIn (see more below)
If you don’t want give the email, you can build an encrypted email
🔒
with the userID: {userID}@{your-company-name}.com (e.g. [email protected])
1
String email = editTextEmail.getText().toString(); //Required
2
String userId = editTextUserId.getText().toString(); //Required
3
4
//optIn, APM_DEVICE_OPT_IN_NOT_SET, APM_DEVICE_OPT_IN_REFUSE, APM_DEVICE_OPT_IN_ACCEPT
5
Integer optIn = APMServicesConfig.APM_DEVICE_OPT_IN_ACCEPT; //Required
6
7
APMUIServicesUser.userClientConnect(email, userId, optIn, new APMUserConnectListener() {
8
@Override
9
public void userConnectSuccess(APMUser user) {
10
Log.e("Test", user + " connected !");
11
}
12
13
@Override
14
public void failure(APMException exception) {
15
Log.e("Test", "error : "+exception);
16
}
17
});
Copied!

3. Logout

The same way the login process is parallel to yours, so is the logout.

APMUIServicesUser.userLogout()

1
APMUIServicesUser.userLogout(this, new APMUserLogoutListener() {
2
@Override
3
public void userLogoutSuccess() {
4
Toast.makeText(RegisterActivity.this, "Logout success !", Toast.LENGTH_SHORT).show();
5
}
6
7
@Override
8
public void failure(APMException exception) {
9
Toast.makeText(RegisterActivity.this, exception.errorMessage, Toast.LENGTH_SHORT).show();
10
}
11
});
Copied!

II. Tagging plan

1. Send an action

In order to reward the user for an action, you need to place triggers in your application. You will need to follow the Tagging plan that your marketing team created with us.
Be careful with the spelling, an extra character like a 's' will change the tag
Call the method triggerAction to send a tag:
1
APM.sharedInstance(this).triggerAction("display_product");
2
// this --> Context
3
// "display_product" --> actionName
Copied!
Make sure to test each action by looking at the logs, especially for actions that are more complex
Earn animation

2. Action with properties

If your tagging plan requires the use of filters, you may need to send properties with your action.
Call triggerAction with a second parameter to send a tag with properties – property.key : Property keys must be strings and must be in the tagging plan (e.g. « zipcode », « establishment »). – property.value : Property values must be strings. (e.g. « 33000 », « restaurant »).
1
HashMap properties = new HashMap();
2
properties.put("zipcode", "33000");
3
properties.put("establishment", "restaurant");
4
APM.sharedInstance(this).triggerAction("write_review", properties);
Copied!
This example tag will match with the action « Write a review of a restaurant in Bordeaux ».
A tag may have multiple versions configured in our BackOffice. This is an unlikely scenario, but as an example, sending the same tag and properties 3 times may match with a different version each time.

Send multiple actions

If you need to send multiple actions at once, you must use another method:
1
String[] tags = new String[3];
2
tags[0] = "tag1";
3
tags[1] = "tag2";
4
tags[2] = "tag3";
5
APM.sharedInstance(this).triggerActions(tags);
Copied!

3. User properties

Just like for 2. Send an action with properties, if your tagging plan requires the use of filters, you may need to send us userProperties so that we can offer filtered actions to the user.
Use the class APMUserPropertiesUtils to manage user properties.
userProperty.key : Property keys must strings. (e.g. « zipcode », « membership_level »). userProperty.value :Property values must strings. (e.g. « 33000 », « premium »).

Adding user properties

1
public void addUserProperty(String value, String key)
2
public void addUserProperties(HashMap userProperties)
Copied!
Be careful of the order of the arguments for addUserProperty(value first) !
Example:
1
// Single userProperty
2
APMUserPropertiesUtils.getInstance().addUserProperty("premium", "membership_level");
3
// Multiple userProperties
4
HashMap userProperties = new HashMap();
5
userProperties.put("zipcode", "33000");
6
userProperties.put("membership_level", "premium");
7
APMUserPropertiesUtils.getInstance().addUserProperties(userProperties);
Copied!
Adding a userProperty with a key that already exists will update it.

Removing user properties

If a userProperty is no longer valid, you can remove it (example: user does not wish to be geolocated anymore),
1
public void removeUserProperty(String key);
2
public void removeUserProperties(); // removes all user properties
Copied!
Example:
1
// Single userProperty
2
APMUserPropertiesUtils.getInstance().removeUserProperty("zipcode");
3
// All userProperties
4
APMUserPropertiesUtils.getInstance().removeUserProperties();
5
Copied!

🔄
Refreshing after a change

If you have changed a userProperty that may have an impact on the actions that we can propose to the user, you will want to refresh the SDK (i.e. tell it to communicate with our API to get updated info).
You can do this by calling:
1
APMServicesPublic.sharedInstance().refreshSDK(true);
Copied!
We showcase one action above others (Challenge of the day). Each action can be configured in our BackOffice with a deeplink so that the user can go to the screen where they can perform the action.
Clicking on a suggested action's deeplink
To listen to the click on the "Let's go !"
👉
button and get the deeplink URI, you need to use APMDeepLinkActionUtils and APMDeepLinkActionUtilsListener.
Example with AbstractActivity lifecycle:
1
public class AbstractActivity extends FragmentActivity implements APMDeeplinkListener {
2
3
@Override
4
protected void onResume() {
5
super.onResume();
6
// Start listening
7
APMDeeplinkUtils.getInstance().addListener(this);
8
}
9
10
@Override
11
protected void onPause() {
12
super.onPause();
13
// Stop listening
14
APMDeeplinkUtils.getInstance().removeListener(this);
15
}
16
17
@Override
18
public void apmDeeplinkOnboardingButtonOkClicked() {
19
//Not used for action's deeplink
20
}
21
22
@Override
23
public void apmDeeplinkOnboardingButtonLaterClicked() {
24
//Not used for action's deeplink
25
}
26
27
@Override
28
public void apmDeeplinkOnboardingButtonNeverClicked() {
29
//Not used for action's deeplink
30
}
31
32
@Override
33
public void apmDeeplinkActionButtonGoClicked(String url) {
34
// The user clicked the "Let's go" button.
35
// The big badge will automatically close
36
// TODO: Use the deeplink to navigate the user to the indicated screen.
37
}
38
39
@Override
40
public void apmDeeplinkClicked(String url) {
41
//Not used for action's deeplink
42
}
43
}
Copied!

III. Advanced functions

1. Show/Hide badge
👻

You can hide the badge if you have a screen where you don't want the user to be distracted (e.g. checkout).
You can hide the badge with the method :
1
APM.sharedInstance(this).setBadgeEnabled(false);
Copied!
And show the badge :
1
APM.sharedInstance(this).setBadgeEnabled(true);
Copied!
Make sure to show the badge again once you leave the screen or the user may never see it again !
Day... Night...
While the badge is hidden, the SDK continues to work. If there are triggerActions, the notifications will happen once the badge is shown.

2. Activate debug logs
🐞

Logs are quire useful, right ? Here's how to unleash the (controlled) fury of debug logs:
1
APM.sharedInstance(this).setDebugMode(true); //To show trace log app's miles
Copied!
Android logs from Android Studio (logcat)

3. Listen for changes to a user

You can be notified f the user change with the class APMServicesUserListener. Set a listener with :
1
APMServicesPublic.sharedInstance().setServicesUserListener(this);
Copied!
3 callbacks available :
    apmServicesUserChanged(APMUser user) : called when one of the user's attribute change
    apmServicesUserBalanceChanged(APMUser user) : called when the userBalance change
    apmServicesUserUnsubscribed() : called when the user unsubscribes

4. RGPD

You can disabled some features for RGPD compliances
1
//Disabled localization of user for SDK app's miles
2
APMGeolocUtils.getInstance().setGeolocEnabled(false);
Copied!

5. Add attributes/customDimensions/userProperties in your analytics

You can retrieve information from SDK to powered your analytics
    "apm_user_connected" : "yes", "no"
    "apm_user_animated" : "not_set", "animated", "anonymous", "unsubscribe"
1
private void updateUserPropertiesAPM() {
2
APMUser user = APMServicesPublic.sharedInstance().getUser();
3
APMDevice device = APMServicesPublic.sharedInstance().getDevice();
4
5
String apmUserConnected = (user != null && user.isConnected()) ? "yes" : "no";
6
7
String apmUserAnimated = "not_set";
8
if(device != null && device.getDeviceStatus() != null) {
9
if(device.getDeviceStatus() == APMServicesConfig.APM_DEVICE_STATUS_OK)
10
apmUserAnimated = "animated";
11
else if(device.getDeviceStatus() == APMServicesConfig.APM_DEVICE_STATUS_ANONYMOUS)
12
apmUserAnimated = "anonymous";
13
else if(device.getDeviceStatus() == APMServicesConfig.APM_DEVICE_STATUS_OPTIN_OFF)
14
apmUserAnimated = "unsubscribe";
15
}
16
17
//Batch example
18
// BatchUserDataEditor editor = Batch.User.editor();
19
// editor.setAttribute("apm_user_connected", (String) apmUserConnected);
20
// editor.setAttribute("apm_user_animated", (String) apmUserAnimated);
21
// editor.save();
22
23
//Google Analytics example
24
// tracker.send(new HitBuilders.ScreenViewBuilder().setCustomDimension(INDEX_APM_USER_CONNECTED, apmUserConnected) //INDEX_APM_USER_CONNECTED index for custom dimension : apm_user_connected
25
// tracker.send(new HitBuilders.ScreenViewBuilder().setCustomDimension(INDEX_APM_USER_ANIMATED, apmUserAnimated) //INDEX_APM_USER_ANIMATED index for custom dimension : apm_user_animated
26
27
//Firebase example
28
// mFirebaseAnalytics.setUserProperty("apm_user_connected", apmUserConnected);
29
// mFirebaseAnalytics.setUserProperty("apm_user_animated", apmUserAnimated);
30
}
Copied!
Call the method updateUserPropertiesAPM() :
    after the init of SDK APM (YourAppApplication.java, in the method onCreate())
    when the user changed (use the listener APMServicesUserListener)

6. Enabled/Disabled statistics for device/user

You can activate/deactivate statistics analytics with the following method :
1
APMStatUtils statUtils = APMStatUtils.sharedInstance();
2
statUtils.setEnabledOptinStat(true, new APMUserOptinsListener() {
3
@Override
4
public void userOptinsSuccess(APMOptins optins) {
5
if(optins.getStatistics().isEnabled()) {
6
//Statistics is now activated for this device/user
7
}
8
}
9
10
@Override
11
public void failure(APMException e) {
12
//An error occured
13
}
14
});
Copied!
If we are connected to an user account, this setting is set to user and device. If we are not connected, this setting is set to device only.
By default, statistics analytics are disabled.
Last modified 30d ago