Integration
Connect the app's miles® SDK to all of your app's features to start driving your users and build retention.
iOS Integration

You have the app's miles banner up and running.
🚩
Now 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.
🧠
🔎
🥕
Skip to the Connect section if you intend to have everyone participate in the program by default

An onboarding example
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❓
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
Swift
Objective C
class AbstractViewController: APMDeeplinkListener {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Start listening
APMDeeplinkUtils.sharedInstance().add(self)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Stop listening
APMDeeplinkUtils.sharedInstance().remove(self)
}
//MARK: - APMDeeplinkListener
func apmDeeplinkOnboardingButtonOkClicked() {
// User clicked "OK" -> "I want to participate".
// TODO: If user not connected, redirect to login/create-account page.
// Nothing to do if user connected
}
func apmDeeplinkOnboardingButtonLaterClicked() {
// User clicked "Later" -> "I'll decide later".
// The SDK will continue showing the onboarding when clicking the banner
// Nothing to do here in general
}
func apmDeeplinkOnboardingButtonNeverClicked() {
// User clicked "Never" => "Stop asking me, hide the banner"
// Note: The user may change his mind, and it is recommended to have a button
// that allows the user to get back in the program
// TODO: (Optional) You can redirect to the page with that "Get back in the program" button
// (with a little message saying "You can always change your mind by clicking this button")
}
}
@interface AbstractViewController() <APMDeeplinkListener>
@end
@implementation AbstractViewController
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// Start listening
[[APMDeeplinkUtils sharedInstance] addListener:self];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// Stop listening
[[APMDeeplinkUtils sharedInstance] removeListener:self];
}
#pragma mark - APMDeeplinkListener
-(void)apmDeeplinkOnboardingButtonOkClicked {
// User clicked "OK" -> "I want to participate".
// TODO: If user not connected, redirect to login/create-account page.
// Nothing to do if user connected
}
-(void)apmDeeplinkOnboardingButtonLaterClicked {
// User clicked "Later" -> "I'll decide later".
// The SDK will continue showing the onboarding when clicking the banner
// Nothing to do here in general
}
-(void)apmDeeplinkOnboardingButtonNeverClicked {
// User clicked "Never" => "Stop asking me, hide the banner"
// Note: The user may change his mind, and it is recommended to have a button
// that allows the user to get back in the program
// TODO: (Optional) You can redirect to the page with that "Get back in the program" button
// (with a little message saying "You can always change your mind by clicking this button")
}
@end
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).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.
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
If you don’t want give the email, you can build a encrypted email with the userID:
{userID}@{your-company-name}.com
(e.g. [email protected])Swift
Objective C
let email: String = <EMAIL> //Required
let userId: String = <USER_ID> //Required
//optIn :
//- APM_DEVICE_OPT_IN_NOT_SET
//- APM_DEVICE_OPT_IN_REFUSE
//- APM_DEVICE_OPT_IN_ACCEPT
let optIn: Int = Int(APM_DEVICE_OPT_IN_ACCEPT) //Required
APMUIServicesUser.userClientConnect(email, partnerClientId: userId, optIn: optIn) { (user: APMUser?) in
print(user, " --> connected !")
} failure: { (error: Error?) in
print(error)
}
NSString* email = _textFieldEmail.text; //Required
NSString* userId = _textFieldUserId.text; //Required
//optIn :
//- APM_DEVICE_OPT_IN_NOT_SET
//- APM_DEVICE_OPT_IN_REFUSE
//- APM_DEVICE_OPT_IN_ACCEPT
NSNumber* optIn = APM_DEVICE_OPT_IN_ACCEPT; //Required
[APMUIServicesUser userClientConnect:email
partnerClientId:userId
optIn:optIn
userConnectSuccess:^(APMUser *user) {
NSLog(@"%@ --> connected !", user);
} failure:^(NSError *error) {
NSLog(@"%@", error);
}];
The same way the login process is parallel to yours, so is the logout
Swift
Objective C
APMUIServicesUser.userLogout {
print("Logout success")
} failure: { (error: Error?) in
print("Logout error : ", error)
}
[APMUIServicesUser userLogout:^{
NSLog(@"Logout success");
} failure:^(NSError *error) {
NSLog(@"Logout error : %@", error);
}];
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
Swift
Objective C
//"display_product" --> actionName
APM.sharedInstance().triggerAction("display_product")
//"display_product" --> actionName
[[APM sharedInstance] triggerAction:@"display_product"];
Make sure to test each action by looking at the logs, especially for actions that are complex

Earn animation
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 »).Swift
Objective C
let properties: NSMutableDictionary = NSMutableDictionary()
properties["zipcode"] = "33000"
properties["establishment"] = "restaurant"
APM.sharedInstance().triggerAction("write_review", properties: properties)
NSMutableDictionary* properties = [NSMutableDictionary dictionary];
properties[@"zipcode"] = @"33000";
properties[@"establishment"] = @"restaurant";
[[APM sharedInstance] triggerAction:@"write_review" properties:properties];
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.
If you need to send multiple actions at once, you must use another method:
Swift
Objective C
var tags: [String] = []
tags.append("tag1")
tags.append("tag2")
tags.append("tag3")
APM.sharedInstance()?.triggerActions(tags)
NSMutableArray* tags = [NSMutableArray array];
[tags addObject:@"tag1"];
[tags addObject:@"tag2"];
[tags addObject:@"tag3"];
[[APM sharedInstance] triggerActions:tags];
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 manager user properties fo user.
key : Property keys are strings. (e.g. « zipcode », « profession »).
value :Property values are strings. (e.g. « 33000 », « doctor »).
You can add user properties with the followings methods :
- addUserProperty()
- addUserProperties()
Swift
Objective C
let userPropertiesUtils: APMUserPropertiesUtils = APMUserPropertiesUtils.sharedInstance()
// Single userProperty
userPropertiesUtils.addUserProperty("premium", forKey: "membership_level")
// Multiple userProperties
var userProperties: [AnyHashable : Any] = [:]
userProperties["zipcode"] = "33000"
userProperties["membership_level"] = "premium"
userPropertiesUtils.addUserProperties(userProperties)
APMUserPropertiesUtils* userPropertiesUtils = [APMUserPropertiesUtils sharedInstance];
// Single userProperty
[userPropertiesUtils addUserProperty:@"premium" forKey:@"membership_level"];
// Multiple userProperties
NSMutableDictionary* userProperties = [NSMutableDictionary dictionary];
userProperties[@"zipcode"] = @"33000";
userProperties[@"membership_level"] = @"premium";
[userPropertiesUtils addUserProperties:userProperties];
Adding a userProperty with a key that already exists will update it.
If a userProperty is no longer valid, you can remove it (example: user does not wish to be geolocated anymore)
- removeUserProperty()
- removeUserProperties()
Swift
Objective C
let userPropertiesUtils: APMUserPropertiesUtils = APMUserPropertiesUtils.sharedInstance()
// Single userProperty
userPropertiesUtils.removeUserProperty("zipcode")
// All userProperties
userPropertiesUtils.removeUserProperties()
APMUserPropertiesUtils* userPropertiesUtils = [APMUserPropertiesUtils sharedInstance];
// Single userProperty
[userPropertiesUtils removeUserProperty:@"zipcode"];
// All userProperties
[userPropertiesUtils removeUserProperties];
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:
Swift
Objective C
APMServices.sharedInstance().refreshSDK(true)
[[APMServices sharedInstance] refreshSDK:YES];
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.
👉
Swift
Objective C
class AbstractViewController: APMDeeplinkListener {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Start listening
APMDeeplinkUtils.sharedInstance().add(self)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Stop listening
APMDeeplinkUtils.sharedInstance().remove(self)
}
//MARK: - APMDeeplinkListener
func apmDeeplinkActionButtonGoClicked(_ url: String, action: String?, params: NSMutableDictionary?) {
// The user clicked the "Let's go" button.
// The big badge will automatically close
// TODO: Use the deeplink to navigate the user to the indicated screen.
}
}
@interface AbstractViewController() <APMDeeplinkListener>
@end
@implementation AbstractViewController
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// Start listening
[[APMDeeplinkUtils sharedInstance] addListener:self];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// Stop listening
[[APMDeeplinkUtils sharedInstance] removeListener:self];
}
#pragma mark - APMDeeplinkListener
- (void)apmDeeplinkActionButtonGoClicked:(NSString *)url action:(NSString *)action params:(NSMutableDictionary *)params {
// The user clicked the "Let's go" button.
// The big badge will automatically close
// TODO: Use the deeplink to navigate the user to the indicated screen.
}
@end
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:
Swift
Objective C
APM.sharedInstance().badgeEnabled = false
[[APM sharedInstance] setBadgeEnabled:NO];
And show the badge:
Swift
Objective C
APM.sharedInstance().badgeEnabled = true
[[APM sharedInstance] setBadgeEnabled:YES];
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.
Logs are quire useful, right ?
Here's how to unleash the (controlled) fury of debug logs:
Swift
Objective C
APM.sharedInstance().setDebugMode(true)
[APM sharedInstance].debugMode = YES;

iOS logs from console.app
You can be notified if the user change with the interface APMServicesUserListener.
Set a listener with :
Swift
Objective C
APMServices.sharedInstance().servicesUserListener = self
[APMServices sharedInstance].servicesUserListener = self;
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
You can disabled some features for RGPD compliances
Swift
Objective C
//Disabled localization of user for SDK app's miles
APMGeolocUtils.sharedInstance().setGeolocEnabled(false)
//Disabled localization of user for SDK app's miles
[[APMGeolocUtils sharedInstance] setGeolocEnabled:NO];
You can retrieve information from SDK to powered your analytics
- "apm_user_connected" : "yes", "no"
- "apm_user_animated" : "not_set", "animated", "anonymous", "unsubscribe"
Swift
Objective C
func updateUserPropertiesAPM() {
let user: APMUser? = APMServices.sharedInstance().user
let device: APMDevice? = APMServices.sharedInstance().device
let apmUserConnected: String = (user != nil && user!.isConnected()) ? "yes" : "no"
print("apmUserConnected : ", apmUserConnected)
var apmUserAnimated: String = "not_set"
if(device != nil && device!.deviceStatus != nil) {
let deviceStatus: NSNumber = device!.deviceStatus
if(deviceStatus.intValue == Int(APM_DEVICE_STATUS_OK)) {
apmUserAnimated = "animated"
}
else if(deviceStatus.intValue == Int(APM_DEVICE_STATUS_ANONYMOUS)) {
apmUserAnimated = "anonymous"
}
else if(deviceStatus.intValue == Int(APM_DEVICE_STATUS_OPTIN_OFF)) {
apmUserAnimated = "unsubscribe"
}
}
print("apmUserAnimated : ", apmUserAnimated)
//Batch example
// let editor = BatchUser.editor()
// try? editor.set(attribute: apmUserConnected, forKey:"apm_user_connected")
// try? editor.set(attribute: apmUserAnimated, forKey:"apm_user_animated")
// editor.save()
//Google Analytics example
// tracker.set(GAIFields.customDimensionForIndex(INDEX_APM_USER_CONNECTED), value: apmUserConnected) //INDEX_APM_USER_CONNECTED index for custom dimension : apm_user_connected
// tracker.set(GAIFields.customDimensionForIndex(INDEX_APM_USER_ANIMATED), value: apmUserAnimated) //INDEX_APM_USER_ANIMATED index for custom dimension : apm_user_animated
//Firebase example
// Analytics.setUserProperty(apmUserConnected, forName: "apm_user_connected")
// Analytics.setUserProperty(apmUserAnimated, forName: "apm_user_animated")
}
-(void)updateUserPropertiesAPM {
APMUser* user = [APMServices sharedInstance].user;
APMDevice* device = [APMServices sharedInstance].device;
NSString* apmUserConnected = (user && user.isConnected) ? @"yes" : @"no";
NSLog(@"apmUserConnected : %@", apmUserConnected);
NSString* apmUserAnimated = @"not_set";
if(device && device.deviceStatus) {
NSNumber* deviceStatus = device.deviceStatus;
if(deviceStatus.integerValue == APM_DEVICE_STATUS_OK) {
apmUserAnimated = @"animated";
}
else if(deviceStatus.integerValue == APM_DEVICE_STATUS_ANONYMOUS) {
apmUserAnimated = @"anonymous";
}
else if(deviceStatus.integerValue == APM_DEVICE_STATUS_OPTIN_OFF) {
apmUserAnimated = @"unsubscribe";
}
}
NSLog(@"apmUserAnimated : %@", apmUserAnimated);
//Batch example
// BatchUserDataEditor *editor = [BatchUser editor];
// [editor setAttribute:apmUserConnected forKey:@"apm_user_connected"];
// [editor setAttribute:apmUserAnimated forKey:@"apm_user_animated"];
// [editor save];
//Google Analytics example
// [tracker set:[GAIFields customDimensionForIndex:INDEX_APM_USER_CONNECTED] value:apmUserConnected]; //INDEX_APM_USER_CONNECTED index for custom dimension : apm_user_connected
// [tracker set:[GAIFields customDimensionForIndex:INDEX_APM_USER_ANIMATED] value:apmUserAnimated]; //INDEX_APM_USER_ANIMATED index for custom dimension : apm_user_animated
//Firebase example
// [FIRAnalytics setUserPropertyString:apmUserConnected forName:@"apm_user_connected"];
// [FIRAnalytics setUserPropertyString:apmUserAnimated forName:@"apm_user_animated"];
}
Call the method updateUserPropertiesAPM() :
- after the init of SDK APM (application:didFinishLaunchingWithOptions)
You can activate/deactivate statistics analytics with the following method :
Swift
Objective C
let statUtils: APMStatUtils = APMStatUtils.sharedInstance()
statUtils.setEnabledOptinStat(true) { (optins: APMOptins?) in
if(optins != nil && optins!.statistics.isEnabled()) {
//Statistics is now activated for this device/user
}
} failure: { (error: Error?) in
//An error occured
}
APMStatUtils* statUtils = [APMStatUtils sharedInstance];
[statUtils setEnabledOptinStat:YES userOptinsSuccess:^(APMOptins *optins) {
if([optins.statistics isEnabled]) {
//Statistics is now activated for this device/user
}
} failure:^(NSError *error) {
//An error occured
}];
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 1yr ago