衡量用户互动度

塞巴斯蒂安·本茨
Sebastian Benz

本指南介绍了如何衡量 Chrome 自定义标签页的互动信号。如果您的应用经常向用户显示指向网络内容的链接(例如在新闻信息流中),请务必了解哪些链接对用户而言很有价值,哪些链接没有价值。在自定义标签页中,您可以通过导航次数、滚动方向变更次数和滚动深度来衡量特定于会话的用户互动度。如需了解互动信号的实际运用,请参阅 GitHub 上的“自定义标签页”演示版应用

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

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

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

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

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

private CustomTabsCallback mCustomTabsCallback = new CustomTabsCallback() {
    @Override
    public void onNavigationEvent(int navigationEvent, @Nullable Bundle extras) {
        String event;
        switch (navigationEvent) {
            case CustomTabsCallback.NAVIGATION_ABORTED:
                event = "NAVIGATION_ABORTED";
                break;
            case CustomTabsCallback.NAVIGATION_FAILED:
                event = "NAVIGATION_FAILED";
                break;
            case CustomTabsCallback.NAVIGATION_FINISHED:
                event = "NAVIGATION_FINISHED";
                break;
            case CustomTabsCallback.NAVIGATION_STARTED:
                event = "NAVIGATION_STARTED";
                break;
            case CustomTabsCallback.TAB_SHOWN:
                event = "TAB_SHOWN";
                break;
            case CustomTabsCallback.TAB_HIDDEN:
                event = "TAB_HIDDEN";
                break;
            default:
                event = String.valueOf(navigationEvent);
        }
        Log.d(TAG, "onNavigationEvent (navigationEvent=" + event + ')');
        mTextNavigation.setText("onNavigationEvent " + event);
    }
};

EngagementSignalsCallback 支持三种不同的回调:

onVerticalScrollEvent()
每当用户更改滚动方向时调用,其中 isDirectionUp(第一个参数)表示方向。
  1. onGreatestScrollPercentageIncreased:当用户到达页面底部时,自定义标签页会指示以 5% 为间隔滚动深度的滚动深度,最高可达 100%。只有用户停止滚动时,系统才会调用该回调函数。每次进行新的导航时,该值都会重置为 0%。
  2. onSessionEnded:自定义标签页停止发送互动信号时触发此事件(例如,在用户关闭自定义标签页后)。如果用户以任何方式(滚动、点击按钮等)与网页进行了互动,didUserInteract 为 true。
private EngagementSignalsCallback mEngagementSignalsCallback = new EngagementSignalsCallback() {
    @Override
    public void onVerticalScrollEvent(boolean isDirectionUp, @NonNull Bundle extras) {
        Log.d(TAG, "onVerticalScrollEvent (isDirectionUp=" + isDirectionUp + ')');
        mTextVerticalScroll.setText("vertical scroll " + (isDirectionUp ? "UP️" : "DOWN"));
    }

    @Override
    public void onGreatestScrollPercentageIncreased(int scrollPercentage, @NonNull Bundle extras) {
        Log.d(TAG, "scroll percentage: " + scrollPercentage + "%");
        mTextGreatestPercentage.setText("scroll percentage: " + scrollPercentage + "%");
    }

    @Override
    public void onSessionEnded(boolean didUserInteract, @NonNull Bundle extras) {
        Log.d(TAG, "onSessionEnded (didUserInteract=" + didUserInteract + ')');
        mTextSessionEnd.setText(didUserInteract ? "session ended with user interaction" : "session ended without user interaction");
    }
};

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

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

private CustomTabsClient mCustomTabsClient;
private CustomTabsSession mCustomTabsSession;

private final CustomTabsServiceConnection mServiceConnectionCallback = new CustomTabsServiceConnection() {

    @Override
    public void onCustomTabsServiceConnected(@NonNull ComponentName name, @NonNull CustomTabsClient client) {
        mCustomTabsClient = client;
        mCustomTabsSession = mCustomTabsClient.newSession(mCustomTabsCallback);
        try {
            boolean engagementSignalsApiAvailable = mCustomTabsSession.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;
            }
            mCustomTabsSession.setEngagementSignalsCallback(mEngagementSignalsCallback, Bundle.EMPTY);
        } catch (RemoteException e) {
            Log.w(TAG, "The Service died while responding to the request.", e);
        } catch (UnsupportedOperationException e) {
            Log.w(TAG, "Engagement Signals API isn't supported by the browser.", e);
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mCustomTabsClient = null;
        mConnection = null;
        mCustomTabsSession = null;
    }
};

唯一要做的就是绑定 CustomTabsService

@Override
protected void onStart() {
    super.onStart();
    bindCustomTabsService();
}

private void bindCustomTabsService() {
    String packageName = CustomTabsHelper.getPackageNameToUse(this);
    if (packageName == null) return;
    CustomTabsClient.bindCustomTabsService(this, packageName, mConnection);
}