我的目标是使用 Google Analytics Event Tracking 计算按钮点击次数。
如何计算按钮点击次数以及哪个用户(和设备)点击了此按钮?
我正在使用这段代码:
public class TestActivity extends Activity {
GoogleAnalyticsTracker tracker;
Button clickBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tracker = GoogleAnalyticsTracker.getInstance();
tracker.startNewSession("UA-XXXXXXXX-1", 30, this);
tracker.setDebug(true);
clickBtn = (Button) findViewById(R.id.click);
setContentView(R.layout.main);
final Button createEventButton = (Button) findViewById(R.id.NewEventButton);
createEventButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tracker.trackEvent("Clicks", // Category
"Button", // Action
"" + clickBtn, // Label
77); // Value
tracker.trackEvent("Clicks", "" + createEventButton, "Easy", 1);
tracker.trackEvent("Completions", "Game-Deaths",
"Hard-Level-One", 15);
tracker.trackEvent("Die", "Easy", " Two", 15);
tracker.trackTransactions();
}
});
Button createPageButton = (Button) findViewById(R.id.NewPageButton);
createPageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Add a Custom Variable to this pageview, with name of "Medium"
// and value "MobileApp"
tracker.setCustomVar(1, "Medium", "Mobile App");
// Track a page view. This is probably the best way to track
// which parts of your application
// are being used.
// E.g.
// tracker.trackPageView("/help"); //to track someone looking at
// the help screen.
// tracker.trackPageView("/level2"); //to track someone reaching
// level 2 in a game.
// tracker.trackPageView("/uploadScreen"); //to track someone
// using an upload screen.
tracker.trackPageView("/testApplicationHomeScreen");
}
});
Button quitButton = (Button) findViewById(R.id.QuitButton);
quitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
Button dispatchButton = (Button) findViewById(R.id.DispatchButton);
dispatchButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Manually start a dispatch, not needed if the tracker was
// started with a dispatch
// interval.
tracker.dispatch();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
// Stop the tracker when it is no longer needed.
tracker.stopSession();
}
}