I used Android’s Google Analytic trackEvent and tested it using Google’s demo code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tracker = GoogleAnalyticsTracker.getInstance();
// Start the tracker in manual dispatch mode...
tracker.startNewSession("UA-33260404-1", this);
// ...alternatively, the tracker can be started with a dispatch interval (in seconds).
// tracker.startNewSession("UA-33260404-1", 20, this);
setContentView(R.layout.main);
Button createEventButton = (Button)findViewById(R.id.NewEventButton);
createEventButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tracker.trackEvent(
"Clicks", // Category
"Button", // Action
"clicked", // Label
77); // Value
}
});
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("/TestActivity");
}
});
I tested it on the mobile device, and I can find base data of example visitor counter, but I cannot see trackPageView(“/TestActivity”) and trackEvent. I can see tracker.setCustomVar in the report, so my question is how to see the trackEvent. Do I need setting at backstage supporter? Google’s documentation is not very detailed on doing this in Android.
edit: I have found the location of the trackPageView and trackEvent, but I have another problem: trackEvent is logging the Clicks, Button, and clicked, but I cannot find the 77 value in the logs. Where do I find the 77?
You’re not calling
tracker.dispatch(). If you don’t set a dispatch interval when you calltracker.startNewSession()you need to calldispatchat some point, probably in youronPauselifecycle method:https://developers.google.com/analytics/devguides/collection/android/devguide#startingTheTracker