title: In Android, how to use Google Analytics Event Tracking on button click
body: <p>My goal is to count button click with the use of Google Analytics Event Tracking.</p>
  
  <p>How do I count button clicks and which user (and device) clicked on this button?</p>
  
  <p>I am using this code:</p>
  
  <pre><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();
  }
  }
  </code></pre>