評估使用者參與度

本指南說明如何評估 Chrome 自訂分頁的參與度信號。如果您的應用程式會定期向使用者顯示網站內容連結 (例如在新聞動態中),您就需要知道哪些連結對使用者而言有價值,哪些沒有。在自訂分頁中,您可以透過導覽次數、捲動方向變更和捲動深度,評估特定工作階段的使用者參與度。如要查看參與信號的實際運作情形,請查看 GitHub 上的自訂 Tabs 示範應用程式

自訂分頁參與度信號示範。

自訂分頁提供兩種不同的回呼,用於評估使用者參與度:

  • 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);
}