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.



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

How To Install Google Analytics on WordPress?

Setup Guide for WordPress websites: Set up Google Analytics on WordPress website to track/measure user behavior data. Optimize your website & traffic/marketing strategies based on GA reports.

How To Install Google Analytics on Shopify?

Setup Guide of Google Analytics on Shopify websites and/or online stores: Track the behavior of your Shopify website visitors. Improve your digital marketing campaigns based on the measured data.

Google Ads (AdWords) Setup & Optimization Checklist

Setup Guide of Google Ads (AdWords): After installing analytics, get people to visit your website through Google Ads. Optimize Google Ads in 2 phases: One-time setup & ongoing optimization. Build ad campaigns that can always return highest sales with lowest cost.

Copyright 2016-2020