Gordon Choi

Author of the Analytics Book

User Tools

Site Tools


google_mobile_app_analytics_android_sdk

Google Mobile App Analytics Android SDK

Chapter 17

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.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.analytics">
  
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  
  <application android:name="AnalyticsApplication">
    ...
  </application>
</manifest>

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


Gordon Choi's Analytics Book has been available since August 2016 and is proudly powered by Folks Analytics.

Thank you for reading! If you love my book, you're welcome to donate through Paypal.

google_mobile_app_analytics_android_sdk.txt · Last modified: 2017/02/17 01:22 by Gordon Choi