# Push metrics

 There's a new version available!

These pages cover version 3 of our SDK, but a newer version is available. In general, we suggest that you update to the latest version to take advantage of new features and fixes.

*   Are you new to our SDKs? [Check out the latest docs.](/integrations/sdk/ios/getting-started)
*   Otherwise, [learn about updating to the latest version](/integrations/sdk/ios/whats-new/)

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](/integrations/sdk/ios/rich-push/), the SDK will automatically track `opened` and `delivered` events for push notifications originating from Customer.io. See section [*Automatic push handling*](#automatic-push-handling) below to learn more about this great feature and how to best take advantage of it.

Otherwise, you can:

*   [Record push metrics with `UserNotifications`](#metrics-userNotifications).
*   [Extract delivery ID and Delivery Token parameters directly](#metrics-direct).

## Automatic push handling[](#automatic-push-handling)

After you call `MessagingPushAPN.initialize` or `MessagingPushFCM.initialize` in your `AppDelegate` and set up the `CioAppDelegateWrapper`, your app is ready to automatically handle push notifications that originate from Customer.io. No additional code is required for your app to track `opened` push metrics or launch deep links.

 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 push behavior[](#configure-push-behavior)

Configure push notification behavior using the `MessagingPushConfigBuilder`:

```swift
MessagingPushAPN.initialize(
    withConfig: MessagingPushConfigBuilder()
        .autoFetchDeviceToken(true)    // Automatically fetch device token and upload to CustomerIO
        .autoTrackPushEvents(true)     // Automatically track push metrics
        .showPushAppInForeground(true) // Enable Notifications in the foreground
        .build()
)
```

#### Configure push display behavior while app in foreground[](#configure-push-display-behavior-while-app-in-foreground)

If your app is in the foreground and receives a push notification, Customer.io will show the notification based on the value of the `showPushAppInForeground` configuration property.

You can choose whether or not to display push notifications when your app is foregrounded by implementing the `userNotificationCenter(_:didReceive:withCompletionHandler:)` method in your AppDelegate. Note that when you implement `UNUserNotificationCenterDelegate`, it takes priority over the `showPushAppInForeground` configuration property.

Add the highlighted code to your `AppDelegate.swift` file for custom handling:

```swift
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 arrives while app is in foreground
    func userNotificationCenter(
      _ center: UNUserNotificationCenter, 
      willPresent notification: UNNotification, 
      withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
    ) {
        // To show it, return appropriate options for your case
        completionHandler([.banner, .list, .badge, .sound])

        // To hide it, return empty array
        // completionHandler([])
    }
}
```

#### Custom handling when a Customer.io push is clicked[](#custom-handling-when-a-customerio-push-is-clicked)

Add the highlighted code to your `AppDelegate.swift` file if your app needs to perform custom handling when users click push notifications. For example, you’d need to perform custom handling if you need to process custom data that you attached to the push notification payload.

```swift
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()
    }
}
```

#### Deep links handling when push is clicked[](#deep-links-handling-when-push-is-clicked)

Our SDK handles deep links automatically for notifications originated from Customer.io. You can find more details at [Deep Links](/integrations/sdk/ios/push/deep-links/).

## Capture push metrics with `UserNotifications`[](#metrics-userNotifications)

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

```swift
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[](#metrics-direct)

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.

```swift
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[](#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.

```swift
MessagingPushAPN.initialize(
    withConfig: MessagingPushConfigBuilder()
        .autoTrackPushEvents(false) // Disable automatic push tracking
        .build()
)
```