-
Couldn't load subscription status.
- Fork 22
Home
To configure the SDK, the following has to be done in the AppDelegate of the application:
NotemobileEngageApplicationCode: is needed if you want to use Mobile Engage features
predictMerchantId: is needed if you want to use Predict features
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
EMSConfig *config = [EMSConfig makeWithBuilder:^(EMSConfigBuilder *builder) {
[builder setMobileEngageApplicationCode:<applicationCode: NSString>];
[builder setContactFieldId:<contactFieldId: NSNumber>];
[builder setMerchantId:<predictMerchantId: NSString>];
}];
[Emarsys setupWithConfig:config];
return YES;
}func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let config = EMSConfig.make { builder in
builder.setMobileEngageApplicationCode(<applicationCode: String>)
builder.setContactFieldId(<contactFieldId: Number>)
builder.setMerchantId(<predictMerchantId: String>)
}
Emarsys.setup(with: config)
return true
}After application setup is finished, you can use setContact method to identify the user with contactFieldValue.
[Emarsys setContactWithContactFieldValue:<contactFieldValue: NSString>
completionBlock:^(NSError *error) {
}];Emarsys.setContactWithContactFieldValue(<contactFieldValue: String>) { error in
}When the user signs out, the clearContact method should be used:
NoteNo need to call
clearContactevery time, even if the user isn't logged in. Just make sure it is called when the user logs out of the application.
[Emarsys clearContactWithCompletionBlock:^(NSError *error) {
}];Emarsys.clearContact { error in
}If you want to track custom events, the trackCustomEvent method should be used, where the eventName parameter is required, but the other attributes are optional.
[Emarsys trackCustomEventWithName:<eventName: String>
eventAttributes:<eventAttributes: NSDictionary<String, String>
completionBlock:^(NSError *error) {
}];Emarsys.trackCustomEvent(withName: <eventName: String>, eventAttributes: <eventAttributes: NSDictionary<String, String>) { error in
}The pushToken has to be set when it arrives:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[Emarsys.push setPushToken:deviceToken
completionBlock:^(NSError *error) {
}];
}func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Emarsys.push.setPushToken(deviceToken) { error in
}
}If you want to remove pushToken for the Contact, you can use clearPushToken.
[Emarsys.push clearPushTokenWithCompletionBlock:^(NSError *error) {
}];Emarsys.push.clearPushToken { error in
}If you want to track whether the push messages have been opened, the trackMessageOpen method should be used.
In the simplest case this call will be in the AppDelegate's didReceiveRemoteNotification:fetchCompletionHandler: method:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[Emarsys.push trackMessageOpenWithUserInfo:userInfo
completionBlock:^(NSError *error) {
}];
}func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
Emarsys.push.trackMessageOpen(userInfo: userInfo) { error in
}
}In order to receive the inbox content, you can use the fetchNotifications method.
[Emarsys.inbox fetchNotificationsWithResultBlock:^(EMSNotificationInboxStatus *inboxStatus, NSError *error) {
if (error) {
NSLog(error);
} else {
NSLog(inboxStatus.notifications);
NSLog(inboxStatus.badgeCount);
}
}];Emarsys.inbox.fetchNotifications { status, error in
if let error = error {
print(error as Any)
} else if let status = status {
print("Notifications: \(status.notifications) badgeCount: \(status.badgeCount)")
}
}When your user opened the application inbox, you might want to reset the unread count (badge). To do so, you can use the resetBadgeCount method.
[Emarsys.inbox resetBadgeCountWithCompletionBlock:^(NSError *error) {
}];Emarsys.inbox.resetBadgeCount { error in
}To track the notification opens in inbox, use the following trackNotificationOpen method.
[Emarsys.inbox trackNotificationOpenWithNotification:<notification: EMSNotification>
completionBlock:^(NSError *error) {
}];Emarsys.inbox.trackNotificationOpen(with: <notification: EMSNotification>) { error in
}When a critical activity starts and should not be interrupted by InApp, pause InApp messages.
[Emarsys.inApp pause];Emarsys.inApp.pause()In order to show inApp messages after being paused, use the resume method.
[Emarsys.inApp resume];Emarsys.inApp.resume()In order to react to an event triggered from the InApp message, you can register for it using the setEventHandler method.
The eventHandler is a callback for an InApp message event.
[Emarsys.inApp setEventHandler:<eventHandler: id<EMSEventHandler>>];Emarsys.inApp.eventHandler = <eventHandler: EMSEventHandler>We won't go into the details to introduce how Predict works, and what its capabilities are, but here we aim to explain the mapping between the Predict commands and our interface. Please visit Predict's documentation for more details.
To use the Predict functionality, you have to setup your merchantId during the initialization of the SDK.
In order to track Predict events, you can use the methods available on our Predict interface.
When you want to track the cart items in the basket you can call the trackCart method with a list of CartItems. CartItem is an interface
that can be used in your application for your own CartItems and then simply use the same items with the SDK.
[Emarsys.predict trackCartWithCartItems:<cartItems: NSArray<EMSCartItem *> *>];Emarsys.predict.trackCart(withCartItems: <cartItems: Array<EMSCartItem>>)To report a purchase event you should call trackPurchase with the items purchased and with an orderId.
[Emarsys.predict trackPurchaseWithOrderId:<orderId: NSString>
items:<cartItems: NSArray<EMSCartItem *> *>];Emarsys.predict.trackPurchase(withOrderId: <orderId: String>, items: <cartItems: Array<EMSCartItem>>)If an item was viewed use the trackItemView method with an itemId.
[Emarsys.predict trackItemViewWithItemId:<itemId: NSString>];Emarsys.predict.trackItemView(withItemId: <itemId: String>)When the user navigates between the categories you should call trackCategoryView in every navigation. Be aware to send categoryPath
in the required format. Please visit Predict's documentation for more information.
[Emarsys.predict trackCategoryViewWithCategoryPath:<categoryPath: NSString>];Emarsys.predict.trackCategoryView(withCategoryPath:<categoryPath: String>)To report search terms entered by the contact use trackSearchTerm method.
[Emarsys.predict trackSearchWithSearchTerm:<searchTerm: NSString>];Emarsys.predict.trackSearch(withSearchTerm: <searchTerm: String>)To track custom tags, use the trackTag method, where the eventName parameter is required, but the attributes is optional.
[Emarsys.predict trackTag:<tag: NSString>
withAttributes:<attributes: NSDictionary<NSString, NSString>];Emarsys.predict.trackTag(<tag: String>, withAttributes: <attributes: [String: String]?>)With the Emarsys SDK you are able to ask for product recommendations based on different recommendation logics.
Note
recommendProductsis also going to track thevalueattached to thelogicon the backend, so no additional tracking needed when using recommendation!
This is a required parameter of the recommendProducts method.
The currently supported logics are:
-
SEARCH- based onsearchTerm -
CART- based oncartItems -
RELATED- based onitemViewId -
CATEGORY- based oncategoryPath -
ALSO_BOUGHT- based onitemViewId -
POPULAR- based oncategoryPathYou can pass the values to the chosen recommendation logic, but if you leave it empty, the SDK handles it and uses the last tracked values.
This is an optional parameter of the recommendProducts method.
You can filter product recommendations with the SDK by building RecommendationFilters.
There are two types of filters: Exclude or Include.
In every case there are four types of comparators you can use to compare your chosen field to expectationValue:
-
isValue- checking if thefieldis matching thevalue -
inValues- any of thevalueshas a match with thefield -
hasValue- One of thefieldvalues is equal toexpectation value(applicable only to fields containing multiple values) -
overlapsValues- One or more of thefieldvalues are found inexpectation values(applicable only to fields containing multiple values)
For further information please check the Predict documentation
This is an optional parameter of the recommendProducts method.
You can limit the number of recommended products received by defining a limit.
This is an optional parameter, by default it's value is 5.
This is a required parameter of the recommendProducts method.
The SDK is going to retrieve recommended products via it's productsBlock
[Emarsys.predict recommendProductsWithLogic:[EMSLogic searchWithSearchTerm:@"shirt"]
filters:@[[EMSRecommendationFilter excludeFilterWithField:@"category"
isValue:@"women"]]
limit:@10
productsBlock:^(NSArray<EMSProduct *> *products, NSError *error) {
if (products) {
for (EMSProduct *product in products) {
NSLog([product description]);
}
} else {
NSLog(error.localizedDescription);
}
}];Emarsys.predict.recommendProducts(with: EMSLogic.search(withSearchTerm: "shirt"), filters: [EMSRecommendationFilter.excludeFilter(withField: "category", isValue: "women")], limit: 10) { products, error in
if let products = products {
for product in products {
print("\(product)")
}
} else if let error = error {
print("\(error.localizedDescription)")
}
}The Emarsys SDK doesn't track automatically recommendationClicks, so you have to call manually trackRecommendationClick when an interaction happens with any of the recommended products.
[Emarsys.predict trackRecommendationClick:<product: EMSProduct>];Emarsys.predict.trackRecommendationClick(<product: EMSProduct>)
NotePlease check our DeepLink page for more information.
In order to track email link clicks that open the application directly with the Emarsys SDK, you need to call trackDeepLink in your AppDelegate's application:continueUserActivity:restorationHandler: method.
- (BOOL)application:(UIApplication *)application
continueUserActivity:(NSUserActivity *)userActivity
restorationHandler:(void (^)(NSArray *__nullable restorableObjects))restorationHandler {
return [Emarsys trackDeepLinkWith:userActivity sourceHandler:^(NSString *source) {
NSLog([NSString stringWithFormat:@"Source url: %@", source]);
}];
}func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
return Emarsys.trackDeepLink(with: userActivity, sourceHandler: { url in
if let source = url {
print(source)
}
})
}The (BOOL) return value of Emarsys.trackDeepLink indicates whether the UserActivity contained a Mobile Engage email deep link and whether it was handled by the SDK.
The first parameter is the UserActivity that comes from the AppDelegate’s application:continueUserActivity:restorationHandler: method.
The second parameter is optional, it is a closure/block that provides the source Url that was extracted from the UserActivity.
For more information, read the relevant iOS documentation.