Add Analytics to Your iOS App

This guide shows how to add Analytics to your iOS app to measure user activity to named screens. If you don't have an application yet and just want to see how Analytics works, take a look at our sample application.

Analytics uses CocoaPods to install and manage dependencies. Open a terminal window and navigate to the location of the Xcode project for your application. If you have not already created a Podfile for your application, create one now:

pod init

Open the Podfile created for your application and add the following:

pod 'Google/Analytics'

Save the file and run:

pod install

This creates an .xcworkspace file for your application. Use this file for all future development on your application.

Initialize Analytics for your app

Now that you have the configuration file for your project, you're ready to begin implementing. First, configure the shared Analytics object inside AppDelegate. This makes it possible for your app to send data to Analytics. You’ll do the following:

  • Include the necessary headers.
  • Set the Analytics tracker inside didFinishLaunchingWithOptions.
  • Replace YOUR_TRACKING_ID with your own Analytics tracking ID, like UA-47605289-8.
  • Send exceptions and logging info (optional).

To do these changes, first be sure your Swift project has a BridgingHeader. Then, inside this bridging header, add Analytics:

#import <Google/Analytics.h>

Finally, override the didFinishLaunchingWithOptions method to configure GGLContext:

guard let gai = GAI.sharedInstance() else {
  assert(false, "Google Analytics not configured correctly")
gai.tracker(withTrackingId: "YOUR_TRACKING_ID")
// Optional: automatically report uncaught exceptions.
gai.trackUncaughtExceptions = true

// Optional: set Logger to VERBOSE for debug information.
// Remove before app release.
gai.logger.logLevel = .verbose;

Add screen tracking

Here you’ll send a named screen view to Analytics whenever the user opens or changes screens on your app. Open a View Controller that you'd like to track, or if this is a new application, open the default view controller. Your code should do the following:

  • Add the required <Google/Analytics.h> header.
  • Use a viewWillAppear method or function override to insert screen tracking.
  • Provide a name for the screen and execute tracking.
guard let tracker = GAI.sharedInstance().defaultTracker else { return }
tracker.set(kGAIScreenName, value: name)

guard let builder = GAIDictionaryBuilder.createScreenView() else { return }
tracker.send( as [NSObject : AnyObject])

Next steps

  • Read the Mobile App Implementation Guide to learn how to use Google Analytics to measure user interactions and answer questions about app usage.
  • Review additional configuration options such as sampling, testing and debugging, opt-out settings, etc.
  • If required by your app, enable optional features such as Enhanced Ecommerce, IDFA (Identifier for Advertisers), and iAd install campaign measurement.