user1785130
user1785130

Reputation:

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

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

Upvotes: 10

Views: 14215

Answers (4)

naXa stands with Ukraine
naXa stands with Ukraine

Reputation: 37993

To send an event to Google Analytics, use tracker.send(MapBuilder.createEvent(...).build()) as shown here - Event tracking in Google Analytics SDK V3.

Upvotes: 0

Nargis
Nargis

Reputation: 4787

I think you are using the Google Analytics old library ,try using the new V2 library,its easy to use and supported by good documentation. Below is the link for event tracking using the v2 version :https://developers.google.com/analytics/devguides/collection/android/v2/events

Upvotes: 0

Calvin
Calvin

Reputation: 3312

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.

Upvotes: 10

patel
patel

Reputation: 850

Please add Google analytics jar file in lib folder. (copy jar file in lib folder. & project properties -> Java build path -> Libraries -> add jar) libGoogleAnalytics.jar

jar file link :- https://developers.google.com/analytics/devguides/collection/android/resources

I hope solved your problem for adding jar file.

Thank you.

Upvotes: 0

Related Questions