Google Analytics

Advanced Configuration - iOS SDK

This document provides an oveview of some of the advanced configuration features of the Google Analytics SDK for iOS v2.

  1. Overview
  2. Using Multiple Trackers
    1. Default Tracker
  1. Sampling
  2. App-level Opt Out
  3. Testing and Debugging

Overview

The Google Analytics SDK for iOS uses two classes to manage the global state of the implementation and sending data to Google Analytics servers.

  • GAI – a singleton that handles the global state of your implementation, including getting new GAITracker objects, as well as your app-level opt-out setting and dispatching settings.
  • GAITracker – the class from which you send data to Google Analytics. Multiple trackers can be instantiated, one per unique property ID.

Using Multiple Trackers

As of version 2 of the SDK, you can use multiple trackers in a single implementation, one per unique tracking ID. All trackers share the same global state held by your GAI singleton.

In the following example, a screen view is sent to two separate properties by using two trackers, each with its own unique property ID:

#import "RootViewController.h"
#import "GAI.h"

@interface RootViewController ()

@end

@implementation RootViewController
{
- (void)viewDidLoad {
  [super viewDidLoad];

  // Send a screen view to the first property.
  id tracker1 = [[GAI sharedInstance] trackerWithTrackingId:@"UA-XXXX-Y"];
  [tracker1 sendView:@"/HomeScreen"];

  // Send another screen view to the second property.
  id tracker2 = [[GAI sharedInstance] trackerWithTrackingId:@"UA-XXXX-Z"];
  [tracker2 sendView:@"Home"];
}

@end

Keep in mind that automated measurement features, like automatic screen and uncaught exception measurement, will only use one tracker to send data to Google Analytics. If you are using these features and want to send data using other trackers, you will need to do so manually.

For reference, automatic screen measurement uses the tracker specified in the tracker property of a given GAITrackedViewController. Uncaught exception measurement uses the default tracker specified in your GAI instance.

Default Tracker

Though an implementation may use multiple trackers, globally it has one default tracker. The first Tracker retrieved becomes the default tracker.

To get the default tracker, use:

// Get default tracker.
id myDefault = [GAI sharedInstance].defaultTracker;

To set the default tracker, use:

// Get a new tracker.
id newTracker = [[GAI sharedInstance]trackerWithTrackingId:@"UA-NEW-TRACKING-ID");

// Set the new tracker as the default tracker, globally.
[GAI sharedInstance].defaultTracker = newTracker;

Sampling

You can enable client-side sampling to limit the number of hits sent to Google Analytics. If your app has a large number of users or otherwise sends a large volume of data to Google Analytics, enabling sampling will help ensure un-interrupted reporting.

For example, to implement client-side sampling at a rate of 50%, use the following code:

// Set a sample rate of 50%.
[tracker setSampleRate:50.0];  // Sample rate is a double.

App-level Opt Out

You can enable an app-level opt out flag that will disable Google Analytics across the entire app. Once set, the flag will persist for the life of the app or until it is reset.

To get the app-level opt out setting, use:

// Get the app-level opt out preference.
if ([GAI sharedInstance].optOut) {
  ... // Alert the user they have opted out.
}

To set the app-level opt out flag, use:


// Set the app-level opt out preference.
[[GAI sharedInstance].setOptOut = YES];

Testing and Debugging

The Google Analytics SDK for iOS provides a debug mode that will print useful information about what data is being sent to Google Analytics in your logs.

// Enable debug mode.
[GAI sharedInstance].debug = YES;

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.