本指南說明如何評估 Chrome 自訂分頁的參與度信號。如果您的應用程式會定期向使用者顯示網頁內容連結 (例如在新聞動態消息中),請務必瞭解使用者認為哪些連結有價值,哪些則不是。在「自訂」分頁中,您可以透過導覽次數、捲動方向變化和捲動深度來評估工作階段特定使用者參與度。如要瞭解參與信號的實際運作方式,請前往 GitHub 上的自訂分頁示範應用程式。
自訂分頁提供兩種不同的回呼來評估使用者參與度:
CustomTabsCallback
用於追蹤基本導覽事件,例如"NAVIGATION_STARTED"
或"NAVIGATION_FINISHED"
。EngagementSignalsCallback
用於追蹤特定的使用者參與度,例如捲動方向或捲動百分比。
兩者都需要有效的 CustomTabsServiceConnection
。如要進一步瞭解如何連線至 CustomTabsService
,請參閱先前的 CustomTabsService
指南。
如要評估使用者參與度,請先建立 CustomTabsCallback
和 EngagementSignalsCallback
例項。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
(第一個引數) 代表方向。
onGreatestScrollPercentageIncreased
:自訂分頁信號的捲動深度為 5%,當使用者抵達頁面底部時,該信號最長可達 100%。只有在使用者停止捲動後,系統才會叫用回呼。每次瀏覽新瀏覽時,該值都會重設為 0%。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");
}
};
CustomTabsCallback
和 EngagementSignalsCallback
都需要有效的自訂分頁服務連線。服務連線後,您可以呼叫 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);
}