Introduction to Analytics in Android (Kotlin) using Firebase Analytics
Scientists in general need data to make decisions, or at least to make the right decisions 🤓, they analyze behaviors, patterns, fluctuations, specific information like demography, environment and so many other variables that can affect a specific amount of people, situation or object.
We as computers scientists aren't the exception 💻, the life of a developer or someone related to any computer science field revolves around make decisions, of any kind. Some does not affect the software UI/UX, for example, we can think about how to structure a project, which design patterns to use, which algorithm fits better for each case and other technical things.
So why do we need to measure things? 🤷🏽♂️
Despite of the previous, when the conversation about improving the user experience occurs, people need data 📊, of basically how the users behave in our app. We would like to know how much time the user spends in each of the screens, which buttons the user clicks often, how well are we retaining users, what is our userbase age-range, where are they from…
In this article you will learn how to capture all those valuable information and how to visualize it, in order to make your decisions. So, let's get started:
Set the environment up 🔧
To have our data collected, we need to install the Firebase SDK in the project, this is an easy peasy step using gradle, refer to the docs to know how to do that. Now we need to setup the analytics library:
First add the dependency in your gradle file:
Declare the FirebaeAnalytics Object in your Activity:
private var mFirebaseAnalytics: FirebaseAnalytics? = null
Initialize object in the OnCreate method
// Obtain the FirebaseAnalytics instance.
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this)
Now everything is set, it was easy, wasn't it? Now we move to the next step.
Log the events 📫
You will use the FirebaseAnalytics object to submit all the events you would like to track in your app. For example purposes I have made a simple list of
TV Shows, with hardcoded data in it:
Lists are everywhere, is difficult to think in an app of more than 3 screens that wouldn't contain a list, therefore this is a very common case. In this case if we offer multiple options, we would like to know at least the three more popular items and the three worse. We can easily achieve that by tracking the event, like this:
We first add all the data we want to track to a Bundle object:
val bundle = Bundle()
Then you call the
logEvent method of the FirebaseAnalytics object:
As you see in the previous snippets FirebaseAnalytics has some built in identifiers for the parameters and for the event itself, in this case we are using the parameter name
ITEM_NAME and the event name
SELECT_CONTENT, we can of course use our own identifiers, for example:
val bundle = Bundle()
In this case we used our own custom names
show_name for the parameter and
show_selected to identify the event. You can of course add as many parameters as you want, depending on your needs. If you don't see anything on the console right away, don't panic, it takes around 24 hours to refresh all the events.
And that's all folks, I hope I helped you guys, now go ahead and track helpful things to get valuable insights of what your users really need and want.
The code is in github, you can clone or fork it: https://github.com/euri16/FirebaseAnalyticsExample