Here I will show you how you can add Google Analytics to your iOS app to measure user activity for the named screens. If you donβt have an application yet and just want to see how Google Analytics works, check out our sample application.
Note. Starting with version 3.16 of the Google Analytics SDK for iOS, Xcode 7.3 or higher is required. Objective-C Swift
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 created a subfile for your application, create it now:
pod init Open the subfile created for your application and add the following:
pod 'Google / Analytics' Save the file and run:
pod install This creates a .xcworkspace file for your application. Use this file for all future developments in your application.
Get configuration file
Click the button below to get the configuration file that will be added to your project.
The configuration file provides service information for your application. To get it, you must select an existing project for your application or create a new one. You also need to provide the package identifier for your application.
GET THE CONFIGURATION FILE
Add configuration file to your project
Drag the GoogleService-Info.plist file that you just uploaded to the root of your Xcode project and added it to all goals.
Initialize analytics for your application
Now that you have the configuration file for your project, you are ready to begin implementation. First set up a shared Analytics object inside AppDelegate. This allows your application to submit data to Google Analytics. You will do the following:
Include the necessary headers.
Install the Analytics tracker inside the didFinishLaunchingWithOptions file. Send exceptions and registration information (optional). To make these changes, first make sure your Swift project has a BridgingHeader. Then, inside this bridge header, add Analytics:
#import <Google/Analytics.h>
Finally, override the didFinishLaunchingWithOptions method to configure the GGLContext:
// Configure tracker from GoogleService-Info.plist. var configureError:NSError? GGLContext.sharedInstance().configureWithError(&configureError) assert(configureError == nil, "Error configuring Google services: \(configureError)") // Optional: configure GAI options. let gai = GAI.sharedInstance() gai.trackUncaughtExceptions = true // report uncaught exceptions gai.logger.logLevel = GAILogLevel.Verbose // remove before app release AppDelegate.swift
Add screen tracking
Here you submit a named screen view to Analytics whenever a user opens or changes screens in your application. Open the view controller that you want to track, or if it is a new application, open the default view controller. Your code should do the following:
Add required title <Google/Analytics.h>
Use the viewWillAppear method or override function to insert screen tracking. Enter a screen name and follow up.
let tracker = GAI.sharedInstance().defaultTracker tracker.set(kGAIScreenName, value: name) let builder = GAIDictionaryBuilder.createScreenView() tracker.send(builder.build() as [NSObject : AnyObject]) ViewController.swift
Note. . You can add a tracking code to each UIViewController that the screen represents, regardless of whether it was shown to your user imperatively (through code) or through a storyboard. Set a name inside each UIViewController if you want to distinguish between the screen views for your application in Google Analytics. All activity recorded on a common tracker sends the last screen name before replacement or cleaning (set to zero).
ViewController.swift