Analysing Google Analytics for Android: Quick Notes on Setup and Configuration File

GA for Android

There is an official Google Analytics SDK for Android. Google provides plenty of documentations and examples on setting things up and getting GA going. A lot of these information are spread across different pages and places so for Android GA new comers or someone who hasn’t used GA for awhile (me), it can be confusing to get data start showing on the Analytics reporting pages.

Here are some quick notes on using (and not using) the JSON configuration file.

Adding GA to a project in Android Studio

We can pretty much follow the Set up your project section in Google’s Add Analytics to Your Android App page.  It tells us the follow:

  • What permissions do we need and how to add it to the Android manifest.
  • What dependencies and plugins do we need and how to add them to the build.gradle file.

The next section (Get a configuration file) speaks about a configuration file with a button that takes you through creating your GA account and app properties and views. This is where it gets a little confusing or perhaps, misleading. Before we talk about that, you should click on the big blue ‘GET A CONFIGURATION FILE‘ button and create your app Analytics stuff.

Configuration File or Not

After following the above steps, you would have downloaded a configuration file, which is really just a JSON file named google-services.json with a bunch of stuff, including your app’s GA tracking ID.

The next section (Add the configuration file to your project) and the rest of the page speaks about adding this file to your Android Studio project’s app directory then it dived straight into some cod samples on tracking screen changes and sending hit events.

The confusing bit about this is that there are actually two ways of going about using GA in Android. With the JSON configuration file, or without it. The JSON configuration file is NOT compulsory.

When you add the JSON configuration file to your project and apply the com.google.gms.google-services plugin in the build.gradle file, a global_tracker.xml file is generated in the build folder and build time. This is used when you call the following line of code:

The generated global_tracker.xml file only contains your app tracking ID. As it’s a generated file during build, we can’t exactly modify it with further GA configurations.

Configuration in Code

Using the JSON configuration file, we would have to set all the GA configuration parameters in code at runtime when calling the corresponding methods on our Tracker object when it is created. Like this:

Configuration in XML

Alternatively, most of the parameters can be set in an XML file added to the res/xml folder. For example, I have an app_tracker.xml file for Player Log for Overwatch with the following contents:

I can then create a tracker with this XML file as below:

Which do I use?

As far as I could tell, it does not actually matter which configuration style you use. It is all personal preferences. In fact, you could mix both if you are using multiple trackers, so long they use different names than the default global_tracker name.

However, if you want to do things like auto activity tracking with screen name aliasing then you will probably need to use a custom XML configuration file.

The point is, you do not have to use the JSON configuration file, and when you do not want to, do not apply the com.google.gms.google-services plugin!

3 Replies to “Analysing Google Analytics for Android: Quick Notes on Setup and Configuration File”

  1. This was a helpful clarification. I inherited an Android project that included the xml resource approach. Wasn’t sure why I wasn’t seeing it referenced anywhere.

    Quick question: I am now attempting to enable user demographics for my client. I’ve added this statement: appTracker.enableAdvertisingIdCollection(true);
    So far, bench testing hasn’t yet yielded any data on the GA dashboard/demographics section.

    Does this work for you?

    Thank you

    1. Hi Mark, I have appTracker.enableAdvertisingIdCollection(true); in my code and I do get demographic data. I can only suggest wait a few days to see if data shows up in Google Analytics. They can take a while.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.