Enabling Google Analytics in your Android app can be a bit complicated given the messy and outdated official documentation so I've wrapped up the basic steps to enable it with last Google Play Services (currently v6).

Get a Property Id

The very first step is to create a new Google Analytics Property for our application or use an existing one. Just head to Analytics website and register a new Mobile Application, you will need the property id for the configuration settings (UA-XXXXXXX-Y).


In order to use Google Play Services we need to download the SDK from the SDK Manager:

And add them to our build.gradle dependencies:

dependencies {  
    compile 'com.google.android.gms:play-services:6.+'

Android Manifest

We need to add Internet permissions

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

And the following meta-data in the application tag:

            android:value="@integer/google_play_services_version" />

I also load the global GA config from an xml file so it needs to be added as another meta-data tag:

       android:resource="@xml/analytics_global_config" />

XML config files

Lets define the global settings for GA. Create a new xml resource directory in /res for the xml files (if you don't have it already) and create analytics_global_config.xml

    <?xml version="1.0" encoding="utf-8" ?>
        <string name="ga_appName">APP_NAME</string>
        <string name="ga_appVersion">APP_VERSION</string>
        <!--Remember to disable verobse log level for production -->
        <string name="ga_logLevel">verbose</string>
        <integer name="ga_dispatchPeriod">1000</integer>
        <bool name="ga_dryRun">false</bool>

These are the basic settings I use, feel free to change them. Check the docs for more info.

Now we have the global settings file but we also need our application tracker configuration file.
Create another xml named app_tracker.xml with the following content:

    <?xml version="1.0" encoding="utf-8"?>
          <integer name="ga_sessionTimeout">300</integer>

          <!-- Enable automatic Activity measurement -->
          <bool name="ga_autoActivityTracking">true</bool>

          <!-- The screen names that will appear in reports -->
          <screenName name="com.test.MainActivity">
          <screenName name="com.test.SecondActivity">
          <!--  The following value should be replaced with correct property id. -->
          <string name="ga_trackingId">UA-XXXXXXX-Y</string>

There are a couple of important points here:

  1. The screenName tags are the Activities names that we will see in our GA dasboard for the defined Activities.
  2. The ga_autoActivityTracking will send a screenView each time an activity is created automatically but I recommend sending them manually for more control (I'll explain how later).

Java code

The Best approach here is to create a new Class that extends Application in order to access our tracker from anywhere of our code. So create a new file (in this sample I'm using MyApp.java)

public class MyApp extends Application {

    HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

    public synchronized Tracker getTracker(TrackerName trackerId) {

        if (!mTrackers.containsKey(trackerId)) {
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            Tracker t = analytics.newTracker(R.xml.app_tracker);
            mTrackers.put(trackerId, t);
        return mTrackers.get(trackerId);

    public enum TrackerName {

Note that I've left the HashMap and TrackerName sample code in case someone need to use different tracker configurations but I'll be using just one(the one deffined in the app_tracker.xml).

We also need to add the application:name tag with the class name to the Android Manifest:

        .... >

And create/init the tracker in your onCreate method of the Main Activity like so:

    Tracker t = ((MyApp) getApplication()).getTracker(MyApp.TrackerName.APP_TRACKER);

Sending ScreenView

For sending new ScreenViews for Activities, Fragments, Dialogs etc... just use the following code in the onCreate (or any other method):

    Tracker t = ((MyApp) getApplication()).getTracker(MyApp.TrackerName.APP_TRACKER);
    t.send(new HitBuilders.ScreenViewBuilder().build());

To see if it's working correctly just check the Logcat since we enabled the Verbose mode in the global config. You should see something like this:

11-16 03:14:37.359  32560-32592/com.test V/GAV4﹕ Thread[GAThread,5,main]: Sending hit to service   PATH: https:  PARAMS: ul=en-us,  ht=1416104072499,  sr=1080x1920,  a=2111314142,  sc=start,  aid=com.test,  cid=5f81f0ec-7136-4825-a3d0-7b21905fbb50,  av=1.0,  v=1,  adid=5d6c9236-7265-4513-9bf9-59093326878b,  t=screenview,  an=MyApp,  ate=0,  tid=UA-XXXXXXX-Y,  _u=.8,  cd=MainActivity  

Sending Events

It's also very usefull to track other events, like button clicks, so we can always know what users are using most. To do so we need to create an event with the HitBuilder:

        Tracker t = ((MyApp) getApplication()).getTracker(MyApp.TrackerName.APP_TRACKER);
        t.send(new HitBuilders.EventBuilder()

That's all! Hope this tutorial gets you started =)