How To Add Google Analytics In Android App?

Gordon Choi
Gordon Choi


Let’s implement the Google Analytics SDK onto your Android app. This Android SDK implementation assumes you use Android Studio and Google Play Services.


In your Android app, add INTERNET and ACCESS_NETWORK_STATE permissions to the AndroidManifest.xml file.

<manifest xmlns:android=""
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <application android:name="AnalyticsApplication">

Build.gradle (Project-level & App-level)

In your Android app, it should have a build.gradle file on the project level, and a second build.gradle file on the app level.

Add the dependency to your project-level build.gradle:

classpath ''

Add the plugin to your app-level build.gradle:

apply plugin: ''

Add the dependency to your app-level build.gradle:

compile ''

Configuration File

Download the google-services.json file (i.e. configuration file). Copy and paste the configuration file onto the app/ directory of your Android Studio project.

Create a new file which extends the Application and provides a helper method that returns your application’s tracker.


public class AnalyticsApplication extends Application {
  private Tracker mTracker;
  synchronized public Tracker getDefaultTracker() {
    if (mTracker == null) {
      GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
      mTracker = analytics.newTracker(R.xml.global_tracker);
    return mTracker;

Let’s add the tracking codes to Activities or Fragments of your Android app. The setup allows screen views to be tracked.

In the onCreate method of your activity files (e.g. MainActivity, NextScreenActivity, etc) or the fragment files (e.g. FragmentActivity, etc), add the following:

AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();

In the onResume method of your activity files or onPageSelected of ViewPager, add the following:

Log.i(TAG, "Setting screen name: " + name);
mTracker.setScreenName("Image~" + name);
mTracker.send(new HitBuilders.ScreenViewBuilder().build());

Add these same codes to all Activities or Fragments which represent “screens”.

Previous Chapters

Next Chapters

Content on Gordon Choi’s Analytics Book is licensed under the CC Attribution-Noncommercial 4.0 International license.

Gordon Choi’s Other Books:
The China Mobile SEO Book
Mobile Website Book