Android – In Android, how to use Google Analytics Event Tracking on button click

androidgoogle analyticsgoogle-analytics-api

My goal is to count button click with the use of Google Analytics Event Tracking.

How do I count button clicks and which user (and device) clicked on this button?

I am using this code:

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();
}
}

Best Answer

In GA console, you can get several numbers. (Engagement -> Events)

Let's explain by example, if you put this line in a button click:

tracker.trackEvent("Completions", "Game-Deaths",
                "Hard-Level-One", 15);

And user clicked this button twice.

You will get this statistics:

  1. Total Event = 2 // 2 clicks
  2. Unique Event = 1 // 1 unique source
  3. Total Value = 30 // User clicked twice, so 15 + 15 = 30
  4. Average Value = 15 // Total value divide by total event

Hope it is clear for you.