衡量用户互动度

本指南介绍了如何衡量 Chrome 自定义标签页的互动信号。如果您的应用会定期向用户显示指向 Web 内容的链接(例如在新闻信息流中),那么了解用户认为哪些链接有价值、哪些链接没有价值可能非常重要。在自定义标签页中,您可以通过导航次数、滚动方向变化和滚动深度来衡量特定会话的用户互动度。如需查看互动信号的实际运作方式,请查看 GitHub 上的自定义标签页演示版应用。

自定义标签页互动信号演示。

自定义标签页提供了两种不同的回调来衡量用户互动度:

  • CustomTabsCallback,用于跟踪基本导航事件,例如 "NAVIGATION_STARTED" 或 "NAVIGATION_FINISHED"。
  • EngagementSignalsCallback,用于跟踪特定网页的用户互动度,例如滚动方向或滚动百分比。

两者都需要有效的 CustomTabsServiceConnection。如需详细了解如何连接到 CustomTabsService,请参阅上一篇 CustomTabsService 指南。

如需衡量用户互动度,请先创建一个 CustomTabsCallback 和一个 EngagementSignalsCallback 实例。CustomTabsCallback 会收到一个 navigationEvent 常量,用于描述发生了哪种导航:

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"
    }
}

EngagementSignalsCallback 支持三种不同的回调:

onVerticalScrollEvent()
每当用户更改滚动方向时调用,其中 isDirectionUp(第一个参数)表示方向。
  1. onGreatestScrollPercentageIncreased:自定义标签页会以 5% 的间隔发出滚动深度信号,直到用户到达页面底部时达到 100%。只有在用户停止滚动后,系统才会调用该回调。每次开始新的导航时,该值都会重置为 0%。
  2. onSessionEnded:当自定义标签页停止发送互动信号时(例如,在用户关闭自定义标签页后),会触发此事件。如果用户以任何方式(滚动、点击按钮等)与页面互动,didUserInteract 将为 true。
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"
        }
    }
}

CustomTabsCallback 和 EngagementSignalsCallback 都需要有效的自定义标签页服务连接。服务连接后,您可以通过调用 CustomTabsClient.newSession() 并传递 CustomTabsCallback 来创建新的 CustomTabsSession。

之后,您应调用 isEngagementSignalsApiAvailable(),以检查当前浏览器是否支持互动信号。如果支持,您可以通过 CustomTabsSession.setEngagementSignalsCallback() 注册 EngagementSignalsCallback。

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

现在只需绑定 CustomTabsService:

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

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