Contents
Further reading
Resources
What is the Emarsys SDK?
The Emarsys SDK enables you to uses Mobile Engage in a very straightforward way. By incorporating the SDK in your app, we support you, among other things, in handling credentials, API calls, tracking of opens and events as well as logins and logouts in the app.
The Emarsys SDK is open sourced to enhance transparency and to remove privacy concerns. This also means that you can always be up-to-date with what we are working on.
Using the SDK is also beneficial from the product aspect: it simply makes it much easier to send push messages through your app. You do not have to follow and implement changes in our API calls, endpoints and parameters: we do it for you in the SDK. Your only task is to use the latest version of the SDK in your app and we take care of everything else.
By completing the steps on this page, the Android app will be able to:
Integrating with iOS
1. Requirements
- The iOS target should be iOS 11 or higher.
- Emarsys App code, which is generated when the app is added to the account.
- Emarsys Predict Merchant ID (If the Predict feature is enabled on the Emarsys account).
Wherever you see <TargetName> or anything similar in <> brackets, you should change those according to your own naming convention.
2. Installation with CocoaPods
CocoaPods is a dependency manager for iOS, which automates and simplifies the process of using 3rd-party libraries.
You can install it with the following command:
$ sudo gem install cocoapods
3. Pod file
To integrate the Emarsys SDK into your Xcode project using CocoaPods, specify it in your Pod file:
3.1. Install Pods
After creating the Pod file, navigate to the project directory in terminal and execute the command below to download dependencies:
pod install
4. Initialization
To configure the SDK, the following has to be done in the AppDelegate
of the application:
The pushToken
has to be set when it arrives:
To track whether the push messages have been opened, the trackMessageOpen
function should be used. In the simplest case this call will be in the AppDelegate's didReceiveRemoteNotification:fetchCompletionHandler:
method:
5. Contact Management
5.1. Identifying Contacts
Objective-C
[Emarsys setContactWithContactFieldValue:<contactFieldValue: NSString> completionBlock:^(NSError *error) { }];
Swift
Emarsys.setContactWithContactFieldValue(<contactFieldValue>)
5.2. Clear Contacts
Use the clearContact
method to remove the device details from the contact record, for example, if the user signs out of the app, and they should not receive personalised messages.
Objective-C
[Emarsys clearContactWithCompletionBlock:^(NSError *error) { }];
Swift
Emarsys.clearContact { error in}
5.3. Clear Push Token
Use the clearPushToken
method to remove the push token of the contact.
Objective-C
[Emarsys.push clearPushTokenWithCompletionBlock:^(NSError *error) { }];
Swift
Emarsys.push.clearPushToken { error in}
When the application setup is finished, you can use the setContactWithContactFieldValue
to identify the user.
6. Tracking Predict Events
Events are a powerful tool to allow marketers to automate user journeys. Therefore, implementing events on any key user milestone within the app mitigates the need for future ad-hoc development requests to satisfy new marketing contact journeys.
Predict tracking needs to be enabled on the Emarsys account to utilise Predict mobile tracking.
6.1. Track Predict Cart Events
When a contact adds items to the cart, the contents of the cart can be tracked by using the trackCart
method.
Objective-C
[Emarsys.predict trackCartWithCartItems:<cartItems: NSArray<EMSCartItem *> *>];
Swift
Emarsys.predict.trackCart(withCartItems: <cartItems: Array<EMSCartItem>>)
6.2. Track Predict Purchase Events
When a contact makes a purchase, the purchase can be tracked by using the trackPurchase
method. It is required to include the orderId
parameter when calling this method.
Objective-C
[Emarsys.predict trackPurchaseWithOrderId:<orderId: NSString> items:<cartItems: NSArray<EMSCartItem *> *>];
Swift
Emarsys.predict.trackPurchase(withOrderId: <orderId: String>, items: <cartItems: Array<EMSCartItem>>)
6.3. Track Predict Item Views
When a contact views an item, the view can be tracked by using the trackItemView
method. It is required to include the itemId
parameter when calling this method.
Objective-C
[Emarsys.predict trackItemViewWithItemId:<itemId: NSString>];
Swift
Emarsys.predict.trackItemView(withItemId: <itemId: String>)
6.4. Track Predict Category Views
When a contact views a category, the category view can be tracked by using the trackCategoryView
method.
Objective-C
[Emarsys.predict trackCategoryViewWithCategoryPath:<categoryPath: NSString>];
Swift
Emarsys.predict.trackCategoryView(withCategoryPath:<categoryPath: String>)
The categoryPath
is required to exactly match that of the product catalogue
6.5. Track Predict Search Term
When a contact enters a search term, the search term can be tracked by using the trackSearchTerm
method.
Objective-C
[Emarsys.predict trackSearchWithSearchTerm:<searchTerm: NSString>];
Swift
Emarsys.predict.trackSearchTerm(withSearchTerm: <searchTerm: String>)
6.6. Track Predict Custom Tag
Use the trackTag
method to track custom tags. The eventName
parameter is required, though the attributes are optional.
Objective-C
[Emarsys.predict trackTag:<tag: NSString> withAttributes:<attributes: NSDictionary<NSString, NSString>];
Swift
Emarsys.predict.trackTag(<tag: String>, withAttributes: <attributes: [String: String]?>)
7. Tracking Custom Events
Use the trackCustomEvent
method to track custom events. When implementing, the eventName
parameter is required, though the attributes are optional.
Objective-C
[Emarsys trackCustomEventWithName:<eventName: String> eventAttributes:<eventAttributes: NSDictionary<String, String> completionBlock:^(NSError *error) { }];
Swift
Emarsys.trackCustomEvent(withName: <eventName: String>, eventAttributes: <eventAttributes: NSDictionary<String, String>) { error in}
8. In-App
8.1. Pause In-App campaigns
When a critical contact activity starts, use pause to stop any disruptions in the user journey by pausing In-App messages. The most common utilisation of the pause
method is on the payment wall.
Objective-C
[Emarsys.inApp pause];
Swift
Emarsys.inApp.pause()
8.2. Resume In-App campaigns
When the contact is no longer engaging with a critical activity, and In-App messages should resume after a pause, use the resume
method.
Objective-C
[Emarsys.inApp resume];
Swift
Emarsys.inApp.resume()
8.3. React to In-App message triggered event
To react to an event triggered by the In-App message, you can register for it using the setEventHandler
method. The eventHandler
is a callback for an In-App message event.
Objective-C
[Emarsys.inApp setEventHandler:<eventHandler: id<EMSEventHandler>>];
Swift
Emarsys.inApp.eventHandler = <eventHandler: EMSEventHandler>
9. Rich Push Notifications
Push notification could show media content and action buttons besides the title and body. Push notifications with these types of contents are called Rich Notifications.
Requirements
- Mobile Engage backend is setup for sending push notifications already
- application is setup for receiving push notifications already
- use Emarsys SDK notification extension as mentioned in documentation
Only HTTPS image URLs are supported.
- Add a new Notification Service Extension target to your project.
- Add the
EmarsysNotificationService
to this target in the Podfile.
target "Emarsys Sample" do pod 'EmarsysSDK' end target "EMSNotificationService" do pod 'EmarsysNotificationService' end
- Install pods with the pod install command in the workspace directory via terminal.
- If your selected language is Swift, then create a Bridging-Header for your new target.
<NameOfYourExtension-Bridging-Header.h>
#import <EmarsysNotificationService/EMSNotificationService.h>
- Open the NotificationService class in the target, then:
- Import the
EmarsysNotificationService.
- Extend the class
EMSNotificationService
instead ofUNNotificationServiceExtension.
- Import the
import EmarsysNotificationService class NotificationService: EMSNotificationService { }
- Request authorization for push notifications in the AppDelegate
application:didFinishLaunchingWithOptions
: method.
application.registerForRemoteNotifications() var options: UNAuthorizationOptions = [.alert, .sound, .badge] UNUserNotificationCenter.current().requestAuthorization(options: options) { [unowned self] granted, error in print(granted, error ?? "no error") if (granted) { Emarsys.notificationCenterDelegate.eventHandler = self UNUserNotificationCenter.current().delegate = Emarsys.notificationCenterDelegate } }
Verifying the SDK
You can check whether the SDK calls have been received by our servers by clicking the SDK events icon on the Apps tab of the Mobile Engage user interface.
The Name column shows the event that has been received, while the Status column shows the response code. If this value is 201 or 202, then the integration of your SDK has been successful.
Frequently Asked Questions
SDK updates
Why is it beneficial to update the SDK?
Each new release of the SDK brings either new features or fixes known issues, so it is always beneficial to update to the newest version.
We are working hard to ensure the SDK is of the highest quality, and this includes taking backwards compatibility very seriously.
How often should you update to the newest SDK?
In general, you should update your version as often as you can. Even if you are not planning to incorporate new features into your app, we still encourage you to at least update when a new patch version comes out, so you will not miss the bug fixes and other enhancements in the SDK.
What is the version numbering of the SDK?
The SDK versioning follows the x.y.z pattern, where x is the major version, y is the minor version and z is the patch version.
- Major version - This brings a significant release online, such as a long-awaited new feature. Although it is against our intentions, it may happen that a new major version may affect backwards compatibility with the previous release. We strive to keep this to a minimum and do so only with a solid reason for it. This will always be duly communicated beforehand.
- Minor version - This brings smaller new features. Minor versions never break backwards compatibility.
- Patch version - This brings bugfixes, stability and performance enhancements. Patch versions never break backwards compatibility, either.
I have the push token in string format, but the Mobile Engage SDK requires it in NSData format. How can I pass the push token?
If you have a native application, you can normally get the push token in its original form (NSData) in your application's AppDelegate:
didRegisterForRemoteNotificationsWithDeviceToken: method
If you have no way of obtaining it in its original format, you can use Apple's solution to create NSData from hexString: