Event Tracking with Analytics API v4 for Android
As Iāve learned from developing my ownĀ mileage tracking app for cyclists and commuters, getting ratings and feedback from users can be challengingĀ and time consuming. Event trackingĀ can help by enabling you to develop aĀ sense of how popular a particular feature is and how often itās getting used by users of your app.
In Android, Google Play Servicesā Analytics API v4 can be used to gatherĀ statistics on the user-events that occur within your app. Ā In this post Iāll quickly show you how to use this API to accomplish simple event tracking.
Getting started
Itās important to say at this point that all of these statisticsĀ are totally anonymous. App developers who use analytics have no idea who is using each feature or generating each event, onlyĀ that an event occurred.
Assuming youāve set up Google Analytics v4 in your app as per my last post, tracking app events is fairly simple.Ā The first thing you need is your analytics application Tracker (obtained in my case by calling getApplication() as per the previous post). Bear in mind that this method is only available in an object that extends Androidās Activity or Service class so you canāt use it everywhere without some messing about.
Once you have your application Tracker you should use an analytics EventBuilder to build() an event and use theĀ send()Ā method on the Tracker to send itĀ to Google. Building an event is easy. You simply create a new HitBuilders.EventBuilder, setting a ācategoryā and an āactionā for yourĀ new event.
Sample code
The sample code belowĀ showsĀ how I track the usersĀ manual useĀ use of the āSTARTā button inĀ Trip Computer. I have similar tracking events for STOP and also for the use of key settings and features like the activation of the appāsĀ unique ābattery-saverā mode (which I understand is quite popular with cyclists).
// Get an Analytics Event tracker.
Tracker myTracker = ((TripComputerApplication) getApplication())
.getTracker(TripComputerApplication.TrackerName.APP_TRACKER);
// Build and Send the Analytics Event.
myTracker.send(new HitBuilders.EventBuilder()
.setCategory("Journey Events")
.setAction("Pressed Start Button")
.build());Once the events are reported back to Google, the Analytics console will display them in the āBehaviour > Events > Overviewā panel and display a simple count how many times each event wasĀ raisedĀ within the tracking period. You can also further subdivide the actions by setting a ālabelā or by providing a āvalueā (but neither of these is actually required).
More information
For more information see the following articles:
- https://support.google.com/analytics/answer/1033068?hl=en
- https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide#Anatomy
| Reference: | Event Tracking with Analytics API v4 for Android from our JCG partner Ben Wilcock at the Ben Wilcock’s blog blog. |


