This developer guides demonstrates how to implement User ID using the Google Analytics SDK for iOS 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 iOS to send user IDs to Google Analytics.
Prerequisites
Before sending the User ID to Google Analytics:
- Set up the User ID.
- Review the User ID Policy.
- Learn how User ID works by reviewing the User ID Feature Reference.
Implementation
When a user is known to your iOS 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
kGAIUserId
parameter name, as in this example:
/** * An example method called when a user signs in to an authentication system. * * @param user represents a generic User object returned by an authentication system on sign in. */ - void signInWithUser:(User *)user { id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker]; // 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. [tracker set:kGAIUserId value:user.id]; // This hit will be sent with the User ID value and be visible in User-ID-enabled views (profiles). [tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"UX" // Event category (required) action:@"User Sign In" // Event action (required) label:nil // Event label value:nil] build]]; // Event value }
This example shows how to get the User ID:
NSString *userId = [tracker get:kGAIUserId];