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:
1 |
mTracker = analytics.newTracker(R.xml.global_tracker); |
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:
1 2 3 4 5 6 7 |
mTracker = analytics.newTracker(R.xml.global_tracker); mTracker.enableAdvertisingIdCollection(true); mTracker.setAnonymizeIp(true); mTracker.setAppName("app name"); mTracker.setAppVersion("version"); mTracker.setSessionTimeout(30000); mTracker.setSampleRate(1.0); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?xml version="1.0" encoding="utf-8"?> <!-- reference: https://developers.google.com/analytics/devguides/collection/android/v4/parameters --> <resources xmlns:tools="http://schemas.android.com/tools"> <string name="ga_trackingId" tools:ignore="TypographyDashes" translatable="false">UA-88755498-1</string> <integer name="ga_sessionTimeout">300</integer> <bool name="ga_reportUncaughtExceptions">true</bool> <!-- Enable automatic Activity measurement --> <bool name="ga_autoActivityTracking">true</bool> <!-- The screen names that will appear in reports --> <screenName name="me.benh.overwatchplayerlog.controllers.listanddetails.OwPlayerItemListActivity"> Player Entries List Screen </screenName> <screenName name="me.benh.overwatchplayerlog.controllers.listanddetails.OwPlayerItemDetailActivity"> Player Entry Detail Screen </screenName> <screenName name="me.benh.overwatchplayerlog.controllers.OwPlayerRecordCreateActivity"> Player Entry Create Screen </screenName> <screenName name="me.benh.overwatchplayerlog.controllers.OwPlayerRecordEditActivity"> Player Entry Edit Screen </screenName> <screenName name="me.benh.overwatchplayerlog.controllers.SettingsActivity"> Settings Screen </screenName> </resources> |
I can then create a tracker with this XML file as below:
1 2 3 4 5 6 7 8 9 |
synchronized public Tracker getAppTracker() { if (appTracker == null) { GoogleAnalytics analytics = GoogleAnalytics.getInstance(this); // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG appTracker = analytics.newTracker(R.xml.app_tracker); appTracker.enableAdvertisingIdCollection(true); } return appTracker; } |
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 on “Analysing Google Analytics for Android: Quick Notes on Setup and Configuration File”
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
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.I appreciate that feedback, Benjamin. I’ll give it a few days.