Enable analytics in your configuration file using the analytics:true value. The SDK collects analytics in the background and when SDK methods are called.
Track Push Notifications
To ensure proper tracking of push notifications by the SDK and Marketing Cloud Engagement analytics, call the SDK in your push notification handler method. If you don’t, analytic events can’t track open counts for your push messaging campaigns. This code example shows how to track push notifications using version 10 of the SDK.
SDK for iOS, version 10
// The method is called on the delegate when the user responds to the// notification by opening the app, dismissing the notification, or choosing a// UNNotificationAction. Set the delegate before the app returns from// applicationDidFinishLaunching:.func userNotificationCenter( _ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() ->Void){ // tell the SDK about the notification PushFeature.requestSdk{ pushFeature in pushFeature?.setNotificationResponse(response)} completionHandler()}
If you use version 8 of the SDK, use this code.
SDK for iOS, version 8
// The method is called on the delegate when the user responds to the// notification by opening the app, dismissing the notification, or choosing a// UNNotificationAction. Set the delegate before the app returns from// applicationDidFinishLaunching:.func userNotificationCenter( _ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() ->Void){ // tell the SDK about the notification SFMCSdk.requestPushSdk{ mp in mp.setNotificationRequest(response.notification.request)} completionHandler()}
If you use version 7 of the SDK, use this code.
SDK for iOS, version 7
// The method is called on the delegate when the user responds to the// notification by opening the app, dismissing the notification, or choosing a// UNNotificationAction. Set the delegate before the app returns from// applicationDidFinishLaunching:.func userNotificationCenter( _ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() ->Void){ // tell the MarketingCloudSDK about the notification MarketingCloudSDK.sharedInstance().sfmc_setNotificationRequest( response.notification.request) completionHandler()}
Track Inbox Message Opens
You can also track analytics for Inbox messages. To send the open analytic value to Engagement, call trackInboxOpenEvent(). We automatically provide analytical information for message downloads.
To record analytics, call trackMessageOpened with an inbox message dictionary. This code example shows how to track inbox message opens using version 10 of the SDK.
SDK for iOS, version 10
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ let inboxMessage = dataSourceArray[indexPath.row] MarketingCloudSdk.requestSdk{ mc in mc?.trackMessageOpened(inboxMessage)} // Your selection handling}
For version 8 of the SDK, use this code.
SDK for iOS, version 8
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ let inboxMessage = dataSourceArray[indexPath.row] SFMCSdk.requestPushSdk{ mp in mp.trackMessageOpened(inboxMessage)} // ... your selection handling}
If you use version 7 of the SDK, use this code.
SDK for iOS, version 7
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ let inboxMessage = dataSourceArray[indexPath.row] MarketingCloudSDK.sharedInstance().sfmc_trackMessageOpened(inboxMessage) // ... your selection handling}
Integrate Einstein Recommendations and Collect API
Einstein Recommendations analytics uses a unique identifier to attribute collected analytics to a specific user. By default, the SDK uses the contact key as this identifier, called the PIID. Your app can explicitly set this value.
The Mobile Push SDK has an optional configuration option called useLegacyPiIdentifier. This option replaces an absent or empty PIID with the MobilePush contact key. If this configuration option is false, the SDK doesn’t replace an absent or empty PIID. Review Configure the SDK section to configure the SDK with pianalytics and useLegacyPiIdentifier.
To ensure future compatibility, we recommend explicitly setting the PIID. The useLegacyPiIdentifier configuration option will be deprecated in a future release.
Important
Example: Analytic Attribution
This code example shows how to set and clear the PIID using version 10 of the SDK.
SDK for iOS, version 10
MarketingCloudSdk.requestSdk{ mc in // Example: Setting and clearing pi identifier. // Setting the pi identifier mc?.setPiIdentifier("example@email.com") // Clearing the pi identifier mc?.setPiIdentifier(nil)}
For version 8 of the SDK, use this code.
SDK for iOS, version 8
SFMCSdk.requestPushSdk{ mp in // Example: Setting and clearing pi identifier. // Setting the pi identifier mp.setPiIdentifier("example@email.com") // Clearing the pi identifier mp.setPiIdentifier(nil)}
If you use version 7 of the SDK, use this code.
SDK for iOS, version 7
// Example: Setting and clearing pi identifier.// Setting the pi identifierMarketingCloudSDK.sharedInstance().sfmc_setPiIdentifier("example@email.com")// Clearing the pi identifierMarketingCloudSDK.sharedInstance().sfmc_setPiIdentifier(nil)
Integration Methods
The methods listed in this section integrate your mobile app with Einstein Recommendations. To use these methods, you must have an existing Einstein Recommendations deployment, and you must enable the pianalytics option when you configure your SDK.
Track Cart
To track the contents of an in-app shopping cart, use the method shown in this example. For more information about this method’s general use with Einstein Recommendations, review Track Items in Shopping Cart.
This code example shows how to track the contents of an in-app shopping cart using version 10 of the SDK.
SDK for iOS, version 10
MarketingCloudSdk.requestSdk{ mc in let cartItem = mc?.cartItemDictionary( price: 1.10, quantity: 1, item: "123456", uniqueId: "uniqueId_123456") let cart = mc?.cartDictionary(cartItem: [cartItem]) mc?.trackCartContents(cart!)}
For version 8 of the SDK, use this code.
SDK for iOS, version 8
SFMCSdk.requestPushSdk{ mp in let cartItem = mp.cartItemDictionary( price: 1.10, quantity: 1, item: "123456", uniqueId: "uniqueId_123456") let cart = mp.cartDictionary(cartItem: [cartItem]) mp.trackCartContents(cart!)}
To track a purchase made through your mobile app, use the method shown in this example. For more information about this method’s general use with Einstein Recommendations, review Track Purchase Details.
This code example shows how to track a purchase made through your mobile app using version 10 of the SDK.
SDK for iOS, version 10
MarketingCloudSdk.requestSdk{ mc in let cartItem = mc?.cartItemDictionary( price: 1.10, quantity: 1, item: "123456", uniqueId: "uniqueId_123456") let cart = mc?.cartDictionary(cartItem: [cartItem]) let order = mc?.orderDictionary( orderNumber: "123456", shipping: 2.11, discount: 4.99, cart: cart!) mc?.trackCartConversion(order!)}
For version 8 of the SDK, use this code.
SDK for iOS, version 8
SFMCSdk.requestPushSdk{ mp in let cartItem = mp.cartItemDictionary( price: 1.10, quantity: 1, item: "123456", uniqueId: "uniqueId_123456") let cart = mp.cartDictionary(cartItem: [cartItem]) let order = mp.orderDictionary( orderNumber: "123456", shipping: 2.11, discount: 4.99, cart: cart!) mp.trackCartConversion(order!)}
To implement page-view analytics in your app, use the method shown in this example. For more information about this method’s general use with Einstein Recommendations, see Track Items Viewed.
This code example shows how to implement page-view analytics in your app using version 10 of the SDK.
SDK for iOS, version 10
MarketingCloudSdk.requestSdk{ mc in mc?.trackPageView( url: "http://my.example.com", title: "page title", item: "item name", search: "search term")}