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:
Open the Podfile created for your application and add the following:
Save the file and run:
This creates an
.xcworkspace file for your application. Use this file for all
future development on your application.
Get a configuration file
Click the button below to get a configuration file to add to your project.
The configuration file provides service-specific information for your app. To get it, you must select an existing project for your app or create a new one. You'll also need to provide a bundle ID for your app.Get a Configuration File
Add the configuration file to your project
GoogleService-Info.plist file you just
downloaded into the root of your Xcode project and add it to all targets.
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
- Send exceptions and logging info (optional).
To do these changes, first add Analytics inside
Then, override the
didFinishLaunchingWithOptions method to
// Configure tracker from GoogleService-Info.plist. NSError *configureError; [[GGLContext sharedInstance] configureWithError:&configureError]; NSAssert(!configureError, @"Error configuring Google services: %@", configureError); // Optional: configure GAI options. GAI *gai = [GAI sharedInstance]; gai.trackUncaughtExceptions = YES; // report uncaught exceptions gai.logger.logLevel = kGAILogLevelVerbose; // remove before app release
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
- Use a
viewWillAppearmethod or function override to insert screen tracking.
- Provide a name for the screen and execute tracking.
id<GAITracker> tracker = [GAI sharedInstance].defaultTracker; [tracker set:kGAIScreenName value:name]; [tracker send:[[GAIDictionaryBuilder createScreenView] build]];
- 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.