במדריך הזה למפתחים נסביר איך להשתמש ב-Google Analytics SDK v4 ל-Android כדי למדוד אירועים באפליקציה.
סקירה
אירועים הם דרך שימושית לאסוף נתונים על האינטראקציה של משתמש עם רכיבים אינטראקטיביים של האפליקציה, כמו לחיצות על לחצנים או שימוש בפריט מסוים במשחק.
אירוע מורכב מארבעה שדות, שניתן להשתמש בהם כדי לתאר אינטראקציה של משתמש עם תוכן האפליקציה:
שם השדה |
סוג |
חובה |
תיאור |
קטגוריה |
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());
}
});
}
}