本開發人員指南說明如何使用 Android 版 Google Analytics (分析) SDK v4,評估應用程式中的事件。
總覽
如要收集使用者與應用程式互動元件的互動相關資料,例如按下按鈕或使用遊戲中的特定項目,事件即可派上用場。
事件包含四個欄位,可用來描述使用者與應用程式內容的互動情形:
欄位名稱 |
類型 |
必要 |
說明 |
類別 |
String |
可 |
事件類別。 |
操作 |
String |
可 |
事件動作。 |
唱片公司 |
String |
否 |
事件標籤。 |
值 |
Long |
否 |
事件值。 |
導入作業
如要將事件傳送至 Google Analytics (分析),請使用 HitBuilders.EventBuilder
並傳送命中,如以下範例所示:
// Get tracker.
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
TrackerName.APP_TRACKER);
// Build and send an Event.
t.send(new HitBuilders.EventBuilder()
.setCategory(getString(categoryId))
.setAction(getString(actionId))
.setLabel(getString(labelId))
.build());
如要進一步瞭解 getTracker
方法,請參閱「
進階設定」。
非互動事件
在某些情況下,您可能會想以
非互動事件傳送事件。方法是將 true
傳遞至
setNonInteraction
:
t.send(new HitBuilders.EventBuilder()
.setCategory(getString(categoryId))
.setAction(getString(actionId))
.setNonInteraction(true)
.build());
如要進一步瞭解非互動命中及其使用時機,請參閱 Analytics (分析) 說明中心的
非互動事件一文。
查看範例應用程式中的程式碼片段
下列程式碼片段是傳送事件的範例 Fragment
:
package com.google.android.apps.mobileplayground;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import com.google.android.apps.mobileplayground.AnalyticsSampleApp.TrackerName;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
/**
* Class to exercise Event hits.
*/
public class EventFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.event, container, false);
setupEvent(view, R.id.video1Play, R.string.videoCategory, R.string.videoPlay, R.string.video1);
setupEvent(view, R.id.video1Pause, R.string.videoCategory, R.string.videoPause,
R.string.video1);
setupEvent(view, R.id.video2Play, R.string.videoCategory, R.string.videoPlay, R.string.video2);
setupEvent(view, R.id.video2Pause, R.string.videoCategory, R.string.videoPause,
R.string.video2);
setupEvent(view, R.id.book1View, R.string.bookCategory, R.string.bookView, R.string.book1);
setupEvent(view, R.id.book1Share, R.string.bookCategory, R.string.bookShare, R.string.book1);
final Button dispatchButton = (Button) view.findViewById(R.id.eventDispatch);
dispatchButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Manually start a dispatch (Unnecessary if the tracker has a dispatch interval)
GoogleAnalytics.getInstance(getActivity().getApplicationContext()).dispatchLocalHits();
}
});
return view;
}
private void setupEvent(View v, int buttonId, final int categoryId, final int actionId,
final int labelId) {
final Button pageviewButton = (Button) v.findViewById(buttonId);
pageviewButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Get tracker.
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
TrackerName.APP_TRACKER);
// Build and send an Event.
t.send(new HitBuilders.EventBuilder()
.setCategory(getString(categoryId))
.setAction(getString(actionId))
.setLabel(getString(labelId))
.build());
}
});
}
}