Important: This document describes a legacy version of the SDK. New users should use the latest SDK.

User ID - Android SDK

This developer guides demonstrates how to implement User ID using the Google Analytics SDK for Android v3.x.

Overview

The User ID feature enables the measurement of user activities that span across devices in Google Analytics, such as attributing an interaction with a marketing campaign on one mobile device to a conversion that occurs on another mobile device or in the browser.

When User IDs are sent with Google Analytics hits using the userId field, your reports will reflect a more accurate count of unique users and offer new cross-device reporting options. Learn more about the benefits of using User ID.

This guide shows how to use the userId field and the Google Analytics SDK for Android to send user IDs to Google Analytics.

Prerequisites

Before sending the User ID to Google Analytics:

Implementation

When a user is known to your Android application, you should send an ID representing that user with all of your Google Analytics hits, such as pageviews, events, ecommerce transactions, etc., using the userId field.

To send the User ID, set the userId field using the Measurement Protocol ampersand syntax and the Fields.USER_ID parameter name, as in this example:

/**
 * An example method called when a user signs in to an authentication system.
 *
 * @param User user represents a generic User object returned by an authentication system on sign in.
 */
public void onUserSignIn(User user) {

  Tracker t = GoogleAnalytics.getInstance(context).getTracker("UA-XXXX-Y");

  // You only need to set User ID on a tracker once. By setting it on the tracker, the ID will be
  // sent with all subsequent hits.
  t.set(Fields.USER_ID, user.getId());

  // This hit will be sent with the User ID value and be visible in User-ID-enabled views (profiles).
  t.send(MapBuilder
      .createEvent("UX",       // Event category (required)
                   "Sign In",  // Event action (required)
                   null,       // Event label
                   null)       // Event value
      .build()
  );
}