Push metrics

Updated

Customer.io supports device-side metrics that help you determine the efficacy of your push notifications: delivered when a push notification is received by the app and opened when a push notification is clicked.

If you already configured rich push notifications, the SDK will automatically track opened and delivered events for push notifications originating from Customer.io. See section Automatic push handling below to learn more about this great feature and how to best take advantage of it.

Otherwise, you can:

Automatic push handling

After you call MessagingPushAPN.initialize or MessagingPushFCM.initialize in your AppDelegate, your app is setup to automatically handle your app’s push notifications that originate from Customer.io. No more code is required for your app to track opened push metrics or launch deep links! Read the sections below to see how you can optionally add custom handling if you choose to.

 Do you use multiple push services in your app?

The Customer.io SDK only handles push notifications that originate from Customer.io. Push notifications that were sent from other push services or displayed locally on device are not handled by the Customer.io SDK. You must add custom handling logic to your app to handle those push events.

Configure if push displayed while app in foreground

If your app is in the foreground and a push notification is received, your app gets to choose if that push is displayed or not on the device.

If the push notification originated from Customer.io, the SDK config option MessagingPushConfigBuilder.showPushAppInForeground config option is what determines if the push is displayed or not. If the push did not come from Customer.io, perform custom handling to determine if the push is displayed.

Custom handling when a Customer.io push is clicked

Follow the instructions in this section if your app needs to perform custom handling on a push notification when it’s clicked, such as processing custom data that you attached to the push notification payload.

Add the highlighted code to your AppDelegate.swift file to add custom push click handling:

class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(
      _ application: UIApplication,
      didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        // ... 

        // Set the AppDelegate as a delegate for push notification events: 
        UNUserNotificationCenter.current().delegate = self

        return true
    }
}

extension AppDelegate: UNUserNotificationCenterDelegate {
    // Function called when a push notification is clicked or swiped away.
    func userNotificationCenter(
      _ center: UNUserNotificationCenter,
      didReceive response: UNNotificationResponse,
      withCompletionHandler completionHandler: @escaping () -> Void
    ) {
        // If you need to know if a push was clicked: 
        let pushWasClicked = response.actionIdentifier == UNNotificationDefaultActionIdentifier

        // Process custom data attached to payload, if you need: 
        let pushPayload = response.notification.request.content.userInfo

        // Important: When you're done processing the push notification, you're required to call the completionHandler. 
        // Even if you do not process a push, you're still required to call the completionHandler() in this function. 
        completionHandler()
    }
}

 Do you use deep links?

If you’re performing custom handling when a push is clicked, it’s recommended not to launch a deep link URL yourself if the push notification originated from Customer.io. Instead, allow the Customer.io SDK to launch deep links to avoid unexpected behaviors when using the SDK.

Custom handling when a Customer.io push is received while app is in foreground

If your app is in the foreground and a push notification is received, your app gets to choose if that push is displayed or not on the device. For push notifications originating from Customer.io, the SDK configuration determines if the push is displayed.

Follow the instructions in this section if you need to add custom logic to your app when this event occurs.

Add the highlighted code to your AppDelegate.swift file to add custom handling when a push is received while app in foreground:

class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(
      _ application: UIApplication,
      didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        // ... 

        // Set the AppDelegate as a delegate for push notification events: 
        UNUserNotificationCenter.current().delegate = self

        return true
    }
}

extension AppDelegate: UNUserNotificationCenterDelegate {
    // Function called when a push is received while app in foreground 
    func userNotificationCenter(
      _ center: UNUserNotificationCenter,
      willPresent notification: UNNotification,
      withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
    ) {
        // Important: When you're done processing the push notification, you're required to call the completionHandler. 
        // Even if you do not process a push, you're still required to call the completionHandler() in this function. 
        completionHandler([.banner, .badge, .sound])

        // Note: If the push notification originated from Customer.io, the value returned in the `completionHandler` will be ignored by the SDK. 
        // Use the SDK's push configuration options instead. 
    }
}

Capture push metrics with UserNotifications

If you’re using a version of iOS that supports UserNotifications, you can track metrics using our UNNotificationContent helper.

func userNotificationCenter(
    _ center: UNUserNotificationCenter,
    didReceive response: UNNotificationResponse,
    withCompletionHandler completionHandler: @escaping () -> Void
) {
    // This 1 line of code might be all that you need!
    MessagingPush.shared.userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)

    // If you use `UserNotifications` for more then Customer.io push notifications, you can check 
    // if the SDK handled the push for you or not. 
    let handled = MessagingPush.shared.userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
    if !handled {
        // Notification was *not* displayed by Customer.io. Handle the notification yourself. 
    }
}

Extract delivery ID and token

If you’re not using a version of iOS that supports UserNotifications, you should send the push metric manually by extracting the CIO-Delivery-ID and CIO-Delivery-Token parameters directly to track push metrics.

guard let deliveryID: String = notificationContent.userInfo["CIO-Delivery-ID"] as? String, 
      let deviceToken: String = notificationContent.userInfo["CIO-Delivery-Token"] as? String else {
    // Not a push notification delivered by Customer.io
    return
}

MessagingPush.shared.trackMetric(deliveryID: deliveryID, event: .delivered, deviceToken: deviceToken)

Disable automatic push tracking

Automatic push metric recording is enabled by default when you install the SDK. You can disable this behavior in the SDK’s configuration.

MessagingPushAPN.initialize(
    withConfig: MessagingPushConfigBuilder()
        .autoTrackPushEvents(false) // Disable automatic push tracking
        .build()
)
Copied to clipboard!
  Contents
Current release
 3.5.1
Is this page helpful?