Enable and Use Analytics on iOS

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 the
2// notification by opening the app, dismissing the notification, or choosing a
3// UNNotificationAction. Set the delegate before the app returns from
4// applicationDidFinishLaunching:.
5func userNotificationCenter(
6  _ center: UNUserNotificationCenter,
7  didReceive response: UNNotificationResponse,
8  withCompletionHandler completionHandler: @escaping () -> Void
9) {
10  // tell the SDK about the notification
11  PushFeature.requestSdk { pushFeature in
12    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 the
2// notification by opening the app, dismissing the notification, or choosing a
3// UNNotificationAction. Set the delegate before the app returns from
4// applicationDidFinishLaunching:.
5func userNotificationCenter(
6  _ center: UNUserNotificationCenter,
7  didReceive response: UNNotificationResponse,
8  withCompletionHandler completionHandler: @escaping () -> Void
9) {
10  // tell the SDK about the notification
11  SFMCSdk.requestPushSdk { mp in
12      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 the
2// notification by opening the app, dismissing the notification, or choosing a
3// UNNotificationAction. Set the delegate before the app returns from
4// applicationDidFinishLaunching:.
5func userNotificationCenter(
6  _ center: UNUserNotificationCenter,
7  didReceive response: UNNotificationResponse,
8  withCompletionHandler completionHandler: @escaping () -> Void
9) {
10  // tell the MarketingCloudSDK about the notification
11  MarketingCloudSDK.sharedInstance().sfmc_setNotificationRequest(
12    response.notification.request
13  )
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 in
4    mc?.trackMessageOpened(inboxMessage)
5  }
6  // Your selection handling
7}

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 in
4        mp.trackMessageOpened(inboxMessage)
5    }
6    // ... your selection handling
7}

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 handling
5}

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 in
2  // Example: Setting and clearing pi identifier.
3  // Setting the pi identifier
4  mc?.setPiIdentifier("example@email.com")
5
6  // Clearing the pi identifier
7  mc?.setPiIdentifier(nil)
8}

For version 8 of the SDK, use this code.

SDK for iOS, version 8
1SFMCSdk.requestPushSdk { mp in
2    // Example: Setting and clearing pi identifier.
3    // Setting the pi identifier
4    mp.setPiIdentifier("example@email.com")
5
6    // Clearing the pi identifier
7    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 identifier
3MarketingCloudSDK.sharedInstance().sfmc_setPiIdentifier("example@email.com")
4
5// Clearing the pi identifier
6MarketingCloudSDK.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 in
2  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}

For version 8 of the SDK, use this code.

SDK for iOS, version 8
1SFMCSdk.requestPushSdk { mp in
2  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  mp.trackCartContents(cart!)
10}

If you use version 7 of the SDK, use this code.

SDK for iOS, version 7
1let cartItem = MarketingCloudSDK.sharedInstance().sfmc_cartItemDictionary(
2  withPrice: 1.10,
3  quantity: 1,
4  item: "123456",
5  uniqueId: "uniqueId_123456"
6)
7let cart = MarketingCloudSDK.sharedInstance().sfmc_cartDictionary(
8  withCartItemDictionaryArray: [cartItem!]
9)
10MarketingCloudSDK.sharedInstance().sfmc_trackCartContents(cart!)

Track Conversion 

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 in
2  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 in
2  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}

If you use version 7 of the SDK, use this code.

SDK for iOS, version 7
1let cartItem = MarketingCloudSDK.sharedInstance().sfmc_cartItemDictionary(
2  withPrice: 1.10,
3  quantity: 1,
4  item: "123456",
5  uniqueId: "uniqueId_123456"
6)
7let cart = MarketingCloudSDK.sharedInstance().sfmc_cartDictionary(
8  withCartItemDictionaryArray: [cartItem!]
9)
10let order = MarketingCloudSDK.sharedInstance().sfmc_orderDictionary(
11  withOrderNumber: "123456", shipping: 2.11, discount: 4.99, cart: cart!
12)
13MarketingCloudSDK.sharedInstance().sfmc_trackCartConversion(order!)

Track Page Views 

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.

SDK for iOS, version 10 or higher
1MarketingCloudSdk.requestSdk { mc in
2  mc?.trackPageView(
3    url: "http://my.example.com",
4    title: "page title",
5    item: "item name",
6    search: "search term"
7  )
8}

If you use version 8 of the SDK, use this code.

SDK for iOS, version 8
1SFMCSdk.requestPushSdk { mp in
2  mp.trackPageView(
3    url: "http://my.example.com",
4    title: "page title",
5    item: "item name",
6    search: "search term"
7  )
8}

For version 7 of the SDK, use this code.

7.x
1MarketingCloudSDK.sharedInstance().sfmc_trackPageView(
2  withURL: "http://my.example.com",
3  title: "page title",
4  item: "item name",
5  search: "search term"
6)

Rights of ALBERT EINSTEIN are used with the permission of The Hebrew University of Jerusalem. Represented exclusively by Greenlight.