Google Mobile App Analytics Android SDK

How To Add Google Analytics In Android App?

Gordon Choi
Gordon Choi
Author


Android

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

AndroidManifest.xml

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 'com.google.gms:google-services:3.0.0'

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

apply plugin: 'com.google.gms.google-services'

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

compile 'com.google.android.gms:play-services-analytics:9.2.0'

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.

https://developers.google.com/mobile/add?platform=android&cntapi=analytics&cnturl=https:%2F%2Fdevelopers.google.com%2Fanalytics%2Fdevguides%2Fcollection%2Fandroid%2Fv4%2Fapp%3Fconfigured%3Dtrue&cntlbl=Continue%20Adding%20Analytics

AnalyticsApplication.java

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

package com.google.samples.quickstart.analytics;
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.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;
  }
}

MainActivity.java

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

[elementor-template id=”764″]

Copyright 2016-2021 www.AnalyticsBook.org