How to add Google Analytics to your iOS app

Posted By : Aditya Kumar Sharma | 18-May-2016

Google Analytics is a service which gives you a detailed statistics about mobile app's usage. It provide the app user detail like: location, app performance and also provide the information about which screen user spends most of the time, so that the client or developer could focus more on that screen or page.

So for integrating Google Analytics you need to have an google developer acoount and must get your App id, by creating the new App. After creating your app and adding your bundle id on google developer account next step is to add pods in the project  so open podfile and add :

pod 'Google/Analytics'

Then save and run

pod install

Now next step is to get a configuration file to add to your project which could be done by going to this link https://developers.google.com/mobile/add?platform=ios&cntapi=analytics&cnturl=https:%2F%2Fdevelopers.google.com%2Fanalytics%2Fdevguides%2Fcollection%2Fios%2Fv3%2Fapp%3Fconfigured%3Dtrue%26ver%3Dswift%23add-config&cntlbl=Continue%20Adding%20Analytics

 After creating configuration file download GoogleService-Info.plist and drag it to the project

 

Now import follwoing the Briding header file :

#import <Google/Analytics.h>

Now goto DidFinishLaunchingWithOptions method in AppDelegate and add following lines of code:

// 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 

Now add following lines of code in every view controller owhich you want to track.  Add these lines to viewWillAppear method :

       let tracker = GAI.sharedInstance().defaultTracker
        tracker.set(kGAIScreenName, value: "MainPage")
        
        let builder = GAIDictionaryBuilder.createScreenView()
        tracker.send(builder.build() as [NSObject : AnyObject])
 

Now you can get your app analytics by going to this link : https://analytics.google.com/ . You can see your app overview by going to Dashboard then on RealTime on side and then to Overview, as you Build and run your project the details will be shown here:

So you have successfully integrated google analytics in your app, you can also get detail when a button is pressed which will shown on Google analytics try to do it...

Thanks

About Author

Author Image
Aditya Kumar Sharma

Aditya is a bright iOS developer, have knowledge of objective C, swift, swift 3, JSON, Core data and iPhone development. Apart from that he loves to travel and explore new things.

Request for Proposal

Name is required

Comment is required

Sending message..