Analytics in iOS Apps that Use SDK Version 10

The processes for enabling and using analytics features have been updated in version 10 of the SDK. To upgrade your app to version 10 of the SDK, first update the initialization code. Then, use these code examples to update the analytics code in your app.

Enable Analytics During SDK Initialization 

Use this code example to enable analytics during SDK initialization.

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}

Analytics Attribution 

Use this code example to set the Predictive Intelligence Identifier in the SDK’s AnalyticsManager.

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}

Track Cart Contents 

Use this code example to track the contents of an in-app shopping cart.

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}

Track Cart Conversion 

Use this code example to track the conversion of an in-app shopping cart.

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}

Track Page Views 

Use this code example to track the page views in your app.

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}

Track Inbox Message Opens 

Use this code example to track the opens of an inbox message.

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}