Google Analytics integration in Android apps

Tracking of Android app is one of the key to success as you can analyse your user behaviour, app’s performance, most used features of your app or unused features, most active regions, etc. Which gives you a complete insight of your app and your user base. To do so analytics comes to picture and there are several options to use analytics in your start from Google Analytics to mixpanel, flurry etc. But the most used and free to use is Google analytics(GA). Here is a sample Android app with Google analytics

In this article we all come to know how to integrate Google analytics with your Android app. GA comes with several advanced and powerful features like real-time tracking, cohort analysis, user behaviour and many more. Integrating GA is includes few number of steps, these are –

Creating Google Analytics project

The first and foremost step is to create a GA project

Google Analytics

Google Analytics Signup

Fill this form from signup . Once the form you fill and submit you will get tracking id which looks like UA-XXXXXXXX-X

Once the tracking id get generated, we need to put that within  string.xml or a string resource file which can be referred from the code.

<string name="">UA-XXXXXXXX-X</string>

Then to integrate GA, android is providing built-in api for Android within Google play services library which we need to add into the gradle dependency within build.gradle file i.e.,

compile ''

Next thing you need to add meta-data in AndroidManifest.xml under Application tag i.e.,

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

Analytics api need internet to send data to server so we need to add internet permission within AndroidManifest.xml before Application tag.

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

As GA is now part of Google services we need to get the configuration file, one we get google-services.json . This file need to be placed within the App directory of the android project.

Now we all set to start tracking the events and screens within Android app by adding the code to track. Here we go –

  • Create a custom class which extends
    public class App extends Application {
      private Tracker mTracker;
      synchronized public Tracker getTracker() {
        if (mTracker == null) {
          GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
          mTracker = analytics.newTracker(R.string.google_analytics_tracking_id);
        return mTracker;
  • Now to track screen view in an Activity need to add the following code
    App application = (App) getApplication();
    mTracker = application.getTracker();
    mTracker.setScreenName("Activity - " + name);
    mTracker.send(new HitBuilders.ScreenViewBuilder().build());
  • To track an event add the following code
    App application = (App) getApplication();
    mTracker = application.getTracker();
    mTracker.send(new HitBuilders.EventBuilder()

As per Google documentation –

Add tracking code to every Activity or Fragment that represents a screen. Be sure to set a name inside every Activity or Fragment if you want to differentiate between screen views for your app in Analytics. All activity recorded on the shared tracker sends the most recent screen name until replaced or cleared (set to null).

Google Analytics

Google Analytics Dashboard

Once the integration is completed, you can able to track your Android app. Now the question is how to track, its very simple just open and logged in with your email which you have used to create Google Analytics project and it will open the analytics home page from where you can navigate to the dashboard to see more insight of your Android apps.

I am sure this article will be helpful to integrate GA with Android app.

Happy coding!!

Leave a Reply

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