First part was about how to integrate Google Analytics in your Android App. I will now explain how to get the most out of it by registrating visits from any view and any other events that may happen.
How to register a view
Initial configuration only allows to capture visits when they come from an activity. But nowadays most Apps are divided into fragments, and detecting when one of these fragments has been loaded may be quite helpful. That’s the coding required for it:
EasyTracker tracker = EasyTracker.getInstance(context); tracker.set(Fields.SCREEN_NAME, name); tracker.send(MapBuilder.createAppView().build());
Set the name of the screen in the second line, which could be the name of the class for instance, or a more friendly name depending on the person how is going to check the analytics.
How to capture and event
Events functionality is inherited directly from web pages analytics. An event is any action performed by an user in our App, and it allows up to four parameters:
- Category: Our app may be divided into different categories depending on our needs. I tend to use a category for every sections in my App.
- Action: The kind of event: press, search, play, stop…
- Label (optional): The element that is involved in the action. It may be some specific UI component, such a press on a button, or any more dynamic capture, such a search. In this second case, the label may be the specific search performed by the user.
- Value (optional): this parameter is a Long value. It may be used to give a numeric value to the action. For instance, the amount of time a user has played some media.
The code for registering an event is:
EasyTracker tracker = EasyTracker.getInstance(context); tracker.send(MapBuilder .createEvent(category, action, label, value) .build() );
The way you use any of these fields is up to your needs and the way you want to use the recorded information.
Google Analytics is a powerful tools that give us the ability to know the way the users use our App. All this information may be extremely useful to improve those functionalities we detect the user is having some problems, evolve the most used sections or activate some ads campaigns depending on the information our users consume. Get the most out of it to lead your Android App to success.
Author: Antonio Leiva
I’m in love with Kotlin. I’ve been learning about it for a couple of years, applying it to Android and digesting all this knowledge so that you can learn it with no effort.
Want to learn Kotlin?
Check my free guide to create your first project in 15 minutes!