Google Analytics SDK for Android v3 (Legacy) - Getting Started

This document describes how to get started using the Google Analytics SDK for Android v3.

Before you Begin

Before implementing the SDK, make sure you have the following:

Getting Started

There are three steps to getting started with the SDK:

  1. Update AndroidManifest.xml
  2. Add EasyTracker methods
  3. Create your analytics.xml file

After completing these steps, you'll be able to measure the following with Google Analytics:

  • App installations
  • Active users and demographics
  • Screens and user engagement
  • Crashes and exceptions

1. Updating AndroidManifest.xml

Update your AndroidManifest.xml file by adding the following permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

2. Adding EasyTracker methods

Add the send methods to the onStart() and onStop() methods of each of your Activities as in the following example:

package com.example.app;

import android.app.Activity;

import com.google.analytics.tracking.android.EasyTracker;

/**
 * An example Activity using Google Analytics and EasyTracker.
 */
public class myTrackedActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }

  @Override
  public void onStart() {
    super.onStart();
    ... // The rest of your onStart() code.
    EasyTracker.getInstance(this).activityStart(this);  // Add this method.
  }

  @Override
  public void onStop() {
    super.onStop();
    ... // The rest of your onStop() code.
    EasyTracker.getInstance(this).activityStop(this);  // Add this method.
  }
}

3. Creating your analytics.xml file

When you use EasyTracker, global configuration settings are managed using resources defined in XML. Create a file called analytics.xml in your project's res/values directory and add the following resources:

<?xml version="1.0" encoding="utf-8" ?>

<resources>
  <!--Replace placeholder ID with your tracking ID-->
  <string name="ga_trackingId">UA-XXXX-Y</string>

  <!--Enable automatic activity tracking-->
  <bool name="ga_autoActivityTracking">true</bool>

  <!--Enable automatic exception tracking-->
  <bool name="ga_reportUncaughtExceptions">true</bool>
</resources>

Your lint checker may warn you about the use of the figure dash ('-') in your tracking ID. You can suppress that warning by adding additional attributes to your <resources> tag:

<resources xmlns:tools="http://schemas.android.com/tools"
tools:ignore="TypographyDashes">

See the analytics.xml parameters reference for the complete list of parameters you can use to configure your implementation. Depending on your applications requirements, you may wish to change the dispatch period to verify that you are properly sending hits. For example if you want to see real time data, you may want to set this to a low value such as 5s.

Congratulations! Your app is now setup to send data to Google Analytics.

Next steps

You can do much more with Google Analytics, including measuring campaigns, in-app payments and transactions, and user interaction events.

The following developer guides provide additional details on how to implement Google Analytics features in your app:

  • Measuring Events – Learn how to measure user engagement with interactive content like buttons, videos, and other media using Events.
  • Measuring In-App Payments – Learn how to measure in-app payments and transactions.
  • Measuring Campaigns – Learn how to implement campaign measurement to understand which channels and campaigns are driving app installs.
  • User timings – Learn how to measure user timings in your app to measure load times, engagement with media, and more.
  • Advanced Configuration – Learn more about advanced configuration options, including using multiple trackers.
  • Analytics.xml parameters – See the complete list of analytics.xml configuration parameters.