Measure user engagement

This guide explains how to measure engagement signals for Chrome Custom tabs. If your app regularly surfaces links to web content to its users, for example in a news feed, it can be important to know which links users find valuable and which not. In Custom Tabs, you can measure session specific user engagement via the number of navigations, scroll direction changes and scroll depth. To see engagement signals in action, checkout the Custom Tabs demo app on GitHub.

Custom Tab engagement signals demo.

Custom Tabs provide two different callbacks for measuring user engagement:

  • CustomTabsCallback for tracking basic navigation events, such as "NAVIGATION_STARTED" or "NAVIGATION_FINISHED".
  • EngagementSignalsCallback for tracking page specific user engagement, such as scroll direction or scroll percentage.

Both require an active CustomTabsServiceConnection. See the previous CustomTabsService guide for details on how to connect to a CustomTabsService.

To measure user engagement, first create a CustomTabsCallback and an EngagementSignalsCallback instance. The CustomTabsCallback receives a navigationEvent constant describing which kind of navigation happened:

private val customTabsCallback = object : CustomTabsCallback() {
    override fun onNavigationEvent(navigationEvent: Int, extras: Bundle?) {
        val event = when (navigationEvent) {
            CustomTabsCallback.NAVIGATION_ABORTED -> "NAVIGATION_ABORTED"
            CustomTabsCallback.NAVIGATION_FAILED -> "NAVIGATION_FAILED"
            CustomTabsCallback.NAVIGATION_FINISHED -> "NAVIGATION_FINISHED"
            CustomTabsCallback.NAVIGATION_STARTED -> "NAVIGATION_STARTED"
            CustomTabsCallback.TAB_SHOWN -> "TAB_SHOWN"
            CustomTabsCallback.TAB_HIDDEN -> "TAB_HIDDEN"
            else -> navigationEvent.toString()
        }
        Log.d(TAG, "onNavigationEvent (navigationEvent=$event)")
        textNavigation.text = "onNavigationEvent $event"
    }
}

The EngagementSignalsCallback supports three different callbacks:

onVerticalScrollEvent()
Called every time the user changes the scroll direction, where isDirectionUp (the first argument) indicates the direction.
  1. onGreatestScrollPercentageIncreased: the Custom Tab signals scroll depth in 5% intervals up to 100% when the user has reached the bottom of the page. The callback is only invoked once the user stops scrolling. The value is reset to 0% with every new navigation.
  2. onSessionEnded: the Custom Tab fires this event when it stops sending engagement signals (for example, after the user has closed the Custom Tab). didUserInteract will be true if the user interacted with the page in any way (scrolling, button click, etc.).
private val engagementSignalsCallback = object : EngagementSignalsCallback {
    override fun onVerticalScrollEvent(isDirectionUp: Boolean, extras: Bundle) {
        Log.d(TAG, "onVerticalScrollEvent (isDirectionUp=$isDirectionUp)")
        textVerticalScroll.text = "vertical scroll ${if (isDirectionUp) "UP" else "DOWN"}"
    }

    override fun onGreatestScrollPercentageIncreased(scrollPercentage: Int, extras: Bundle) {
        Log.d(TAG, "scroll percentage: $scrollPercentage%")
        textGreatestPercentage.text = "scroll percentage: $scrollPercentage%"
    }

    override fun onSessionEnded(didUserInteract: Boolean, extras: Bundle) {
        Log.d(TAG, "onSessionEnded (didUserInteract=$didUserInteract)")
        textSessionEnd.text = if (didUserInteract) {
            "session ended with user interaction"
        } else {
            "session ended without user interaction"
        }
    }
}

Both CustomTabsCallback and EngagementSignalsCallback require an active Custom Tab service connection. Once the service is connected, you can create a new CustomTabsSession by calling CustomTabsClient.newSession() and passing the CustomTabsCallback.

Afterwards, you should call isEngagementSignalsApiAvailable() to check if engagement signals are supported by the current browser. If they are supported, you can register your EngagementSignalsCallback via CustomTabsSession.setEngagementSignalsCallback().

private var customTabsClient: CustomTabsClient? = null
private var customTabsSession: CustomTabsSession? = null

private val serviceConnectionCallback = object : CustomTabsServiceConnection() {

    override fun onCustomTabsServiceConnected(name: ComponentName, client: CustomTabsClient) {
        customTabsClient = client
        val session = client.newSession(customTabsCallback) ?: return
        customTabsSession = session
        try {
            val engagementSignalsApiAvailable =
                session.isEngagementSignalsApiAvailable(Bundle.EMPTY)
            if (!engagementSignalsApiAvailable) {
                Log.d(
                    TAG,
                    "CustomTab Engagement signals not available, make sure to use the " +
                        "latest Chrome version and enable via chrome://flags/#cct-real-time-engagement-signals"
                )
                return
            }
            session.setEngagementSignalsCallback(
                engagementSignalsCallback,
                Bundle.EMPTY
            )
        } catch (e: RemoteException) {
            Log.w(TAG, "The Service died while responding to the request.", e)
        } catch (e: UnsupportedOperationException) {
            Log.w(TAG, "Engagement Signals API isn't supported by the browser.", e)
        }
    }

    override fun onServiceDisconnected(name: ComponentName) {
        customTabsClient = null
        customTabsSession = null
    }
}

The only thing left to do is binding the CustomTabsService:

override fun onStart() {
    super.onStart()
    bindCustomTabsService()
}

private fun bindCustomTabsService() {
    val packageName = CustomTabsClient.getPackageName(this, null) ?: return
    CustomTabsClient.bindCustomTabsService(this, packageName, serviceConnectionCallback)
}