Note: Google Analytics can be used and will work on
devices that do not have Google Play Services. In this case you still download and use the
Google Play Services SDK and Google Analytics will automatically fall back to local
dispatching.

Getting Started

This guide uses code snippets from the Mobile Playground
sample application included with the
Google Play
Services SDK. The complete source for this project is available in:
<android-sdk-directory>/extras/google/google_play_services/samples/analytics.

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:

2. Initialize Trackers

With the new SDK, developers should manage the trackers themselves. To ensure
that the metrics are not over-counted, it is highly recommended that the
tracker be created and managed in the Application class.

In the following example, three trackers are created and represented by
APP_TRACKER, GLOBAL_TRACKER,
ECOMMERCE_TRACKER. They are used throughout the application for
different purposes.

/**
* Enum used to identify the tracker that needs to be used for tracking.
*
* A single tracker is usually enough for most purposes. In case you do need multiple trackers,
* storing them all in Application object helps ensure that they are created only once per
* application instance.
*/
public enum TrackerName {
APP_TRACKER, // Tracker used only in this app.
GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.
ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a company.
}
HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

Next, the Application class can provide a method to get the
tracker that is requested and can create them on demand if needed. Note that
the tracker can be created from a PROPERTY_ID using
analytics.newTracker(PROPERTY_ID) or it can be created from a
xml resource file as analytics.newTracker(R.xml.global_tracker).

Enabling Advertising Features

To enable advertising features requires a single addition to your
Google Analytics tracking code to collect the advertising id. Call the
enableAdvertisingIdCollection method on the tracker for which
you want to enable advertising features. For example:

The feature works by collecting the advertising identifier. Please make sure
to review and adhere to all applicable SDK policies when
using this feature.

3. Create a configuration XML file

Configuration settings can be managed using
resources defined in XML. For example, if you have a global tracker you could
create a file called global_tracker.xml in
your project's res/xml directory and add the following
resources: