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 or higher of the SDK.
SDK for iOS, version 10 or higher
1// The method is called on the delegate when the user responds to the2// notification by opening the app, dismissing the notification, or choosing a3// UNNotificationAction. Set the delegate before the app returns from4// applicationDidFinishLaunching:.5func userNotificationCenter(6 _ center: UNUserNotificationCenter,7 didReceive response: UNNotificationResponse,8 withCompletionHandler completionHandler: @escaping() ->Void9){10 // tell the SDK about the notification11 PushFeature.requestSdk{ pushFeature in12 pushFeature?.setNotificationResponse(response)13}14 completionHandler()15}
If you use version 8 or 9 of the SDK, use this code.
SDK for iOS, version 8 or 9
1// The method is called on the delegate when the user responds to the2// notification by opening the app, dismissing the notification, or choosing a3// UNNotificationAction. Set the delegate before the app returns from4// applicationDidFinishLaunching:.5func userNotificationCenter(6 _ center: UNUserNotificationCenter,7 didReceive response: UNNotificationResponse,8 withCompletionHandler completionHandler: @escaping() ->Void9){10 // tell the SDK about the notification11 SFMCSdk.requestPushSdk{ mp in12 mp.setNotificationRequest(response.notification.request)13}14 completionHandler()15}
If you use version 7 of the SDK, use this code.
SDK for iOS, version 7
1// The method is called on the delegate when the user responds to the2// notification by opening the app, dismissing the notification, or choosing a3// UNNotificationAction. Set the delegate before the app returns from4// applicationDidFinishLaunching:.5func userNotificationCenter(6 _ center: UNUserNotificationCenter,7 didReceive response: UNNotificationResponse,8 withCompletionHandler completionHandler: @escaping() ->Void9){10 // tell the MarketingCloudSDK about the notification11 MarketingCloudSDK.sharedInstance().sfmc_setNotificationRequest(12 response.notification.request13)14 completionHandler()15}
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 or higher of the SDK.
SDK for iOS, version 10 or higher
1func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){2 let inboxMessage = dataSourceArray[indexPath.row]3 MarketingCloudSdk.requestSdk{ mc in4 mc?.trackMessageOpened(inboxMessage)5}6 // Your selection handling7}
For version 8 of the SDK, use this code.
SDK for iOS, version 8
1func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){2 let inboxMessage = dataSourceArray[indexPath.row]3 SFMCSdk.requestPushSdk{ mp in4 mp.trackMessageOpened(inboxMessage)5}6 // ... your selection handling7}
If you use version 7 of the SDK, use this code.
SDK for iOS, version 7
1func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){2 let inboxMessage = dataSourceArray[indexPath.row]3 MarketingCloudSDK.sharedInstance().sfmc_trackMessageOpened(inboxMessage)4 // ... your selection handling5}
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 or higher of the SDK.
SDK for iOS, version 10 or higher
1MarketingCloudSdk.requestSdk{ mc in2 // Example: Setting and clearing pi identifier.3 // Setting the pi identifier4 mc?.setPiIdentifier("example@email.com")56 // Clearing the pi identifier7 mc?.setPiIdentifier(nil)8}
For version 8 of the SDK, use this code.
SDK for iOS, version 8
1SFMCSdk.requestPushSdk{ mp in2 // Example: Setting and clearing pi identifier.3 // Setting the pi identifier4 mp.setPiIdentifier("example@email.com")56 // Clearing the pi identifier7 mp.setPiIdentifier(nil)8}
If you use version 7 of the SDK, use this code.
SDK for iOS, version 7
1// Example: Setting and clearing pi identifier.2// Setting the pi identifier3MarketingCloudSDK.sharedInstance().sfmc_setPiIdentifier("example@email.com")45// Clearing the pi identifier6MarketingCloudSDK.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, see Salesforce Help: Track Items in Cart.
This code example shows how to track the contents of an in-app shopping cart using version 10 or higher of the SDK.
SDK for iOS, version 10 or higher
1MarketingCloudSdk.requestSdk{ mc in2 let cartItem = mc?.cartItemDictionary(3 price: 1.10,4 quantity: 1,5 item: "123456",6 uniqueId: "uniqueId_123456"7)8 let cart = mc?.cartDictionary(cartItem: [cartItem])9 mc?.trackCartContents(cart!)10}
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, see Salesforce Help: Track Purchase Details.
This code example shows how to track a purchase made through your mobile app using version 10 or higher of the SDK.
SDK for iOS, version 10 or higher
1MarketingCloudSdk.requestSdk{ mc in2 let cartItem = mc?.cartItemDictionary(3 price: 1.10,4 quantity: 1,5 item: "123456",6 uniqueId: "uniqueId_123456"7)8 let cart = mc?.cartDictionary(cartItem: [cartItem])9 let order = mc?.orderDictionary(10 orderNumber: "123456",11 shipping: 2.11,12 discount: 4.99,13 cart: cart!14)15 mc?.trackCartConversion(order!)16}
For version 8 of the SDK, use this code.
SDK for iOS, version 8
1SFMCSdk.requestPushSdk{ mp in2 let cartItem = mp.cartItemDictionary(3 price: 1.10,4 quantity: 1,5 item: "123456",6 uniqueId: "uniqueId_123456"7)8 let cart = mp.cartDictionary(cartItem: [cartItem])9 let order = mp.orderDictionary(10 orderNumber: "123456",11 shipping: 2.11,12 discount: 4.99,13 cart: cart!14)15 mp.trackCartConversion(order!)16}
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 Salesforce Help: Track Items Viewed.
This code example shows how to implement page-view analytics in your app using version 10 or higher of the SDK.