Click here to see your recently viewed pages and most viewed pages.
Hide

Google Analytics SDK for iOS v3 - Getting Started

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

  1. Before You Begin
  1. Getting Started
    1. 1. Adding headers and libraries
    2. 2. Initializing the tracker
    3. 3. Implementing screen measurement
  1. Next steps

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. Add headers and libraries to your project
  2. Initialize the tracker
  3. Add screen measurement

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. Adding header files and configuring your project

Download the Google Analytics for iOS SDK and add these files from the SDK package to your app:

  • GAI.h
  • GAIDictionaryBuilder.h
  • GAIEcommerceProduct.h
  • GAIEcommerceProductAction.h
  • GAIEcommercePromotion.h
  • GAIFields.h
  • GAILogger.h
  • GAITrackedViewController.h
  • GAITracker.h

To use enhanced ecommerce features you'll also need to add the following file to your app:

  • GAIEcommerceFields.h

To use the IDFA (Identifier for Advertisers) requires that you link the following files to your app and then enable IDFA collection:

  • libAdIdAccess.a
  • AdSupport.framework

The Google Analytics SDK uses the CoreData and SystemConfiguration frameworks, so you will need to add the following to your application target's linked libraries:

  • CoreData.framework
  • SystemConfiguration.framework
  • libz.dylib
  • libsqlite3.dylib
  • libGoogleAnalyticsServices.a

2. Initializing the tracker

To initialize the tracker, import the GAI.h header in your application delegate .m file and add this code to your application delegate's application:didFinishLaunchingWithOptions: method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Optional: automatically send uncaught exceptions to Google Analytics.
  [GAI sharedInstance].trackUncaughtExceptions = YES;

  // Optional: set Google Analytics dispatch interval to e.g. 20 seconds.
  [GAI sharedInstance].dispatchInterval = 20;

  // Optional: set Logger to VERBOSE for debug information.
  [[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];

  // Initialize tracker. Replace with your tracking ID.
  [[GAI sharedInstance] trackerWithTrackingId:@"UA-XXXX-Y"];

}
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];

Note that in the above example, "UA-XXXX-Y" here is a placeholder for the tracking ID assigned to you when you created your Google Analytics property. If you are only using one property ID in your app, using the default tracker method is best.

Enabling IDFA (Identifier for Advertisers) collection

To enable IDFA collection link the related files and set the allowIDFACollection property to YES on each tracker that will collect the IDFA. For example:

// Enable IDFA collection.
tracker.allowIDFACollection = YES;

3. Implementing automatic screen measurement

To automatically measure views in your app, have your view controllers extend GAITrackedViewController. Set a property called screenName with the name of the screen that you want to appear in your reporting.

For example, suppose you have a “Home Screen” view that you want to measure with a view controller header that looks like this:

@interface HomeViewController : UIViewController

You would update this header to:

#import "GAITrackedViewController.h"

@interface HomeViewController : GAITrackedViewController

You must also provide the view name to be used in your Google Analytics reports. A good place to put this is the view controller's initializer method, if you have one, or the viewWillAppear: method:

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  self.screenName = @"About Screen";
}

To learn more about screen measurement, see the Screens Developer Guide.

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.

Review the Mobile App Implementation Guide for an overview of how to use Google Analytics to measure user interactions and answer questions about app usage.

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

  • Demographics and Remarketing – Use Display Features to understand user demographic data like age/gender and create remarketing audiences for re-engaging users.
  • 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.