Google Analytics

Google Analytics SDK for iOS v3 (Beta) - 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
  • GAITracker.h
  • GAITrackedViewController.h
  • GAIDictionaryBuilder.h
  • GAIFields.h
  • GAILogger.h
  • libGoogleAnalyticsServices.a

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:

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

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.

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. See the following developer guides to learn how to add these features to your implementation:

  • Advanced Configuration – Learn more about advanced configuration options, including using multiple trackers.
  • Measuring Campaigns – Learn how to implement campaign measurement to understand which channels and campaigns are driving app installs.
  • 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.
  • User timings – Learn how to measure user timings in your app to measure load times, engagement with media, and more.

Authentication required

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

Signing you in...

Google Developers needs your permission to do that.