Menu

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.

  
<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





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 www.AnalyticsBook.org