Friday, April 22, 2016

Add Google Analytics to your iOS Apps

Google Analytics provides powerful information basing on the usage of your web application. It can also provide similar service to your mobile applications. Here's how you can do it:

Create mobile app GA account
  • In GA, go to Admin tab, click ACCOUNT drop down and select 'Create new account'. 
  • On New Account page, pick 'Mobile app'. Specify your account name, your app name, industry category etc.
  • Click on 'Get Tracking ID'
  • You can create multiple Tracking ID for the same mobile app by selecting 'Create new property' under 'PROPERTY' section

Add Google Analytics to iOS App

1. Install Google Analytics using CocoaPods:
  • If you haven't use pod for our current iOS app, open a terminal and cd to your Xcode project of your app, run: pod init to create a Podfile
  • Open Podfile, and add: pod 'Google/Analytics'
  • Now install pod by running this: pod install
  • As of now, it installs the following libraries:
    • Google (1.3.2)
    • GoogleAnalytics (3.14.0)
    • GoogleInterchangeUtilities (1.1.0)
    • GoogleNetworkingUtilities (1.0.0)
    • GoogleSymbolUtilities (1.0.3)
    • GoogleUtilities (1.1.0)  
2. Get Google Analytics configuration file:

  • On 'PROPERTY' section, select .js > Tracking Code
  • Then 'Getting started guide' link next to 'Google Analytics iOS SDK
     
  • It will take you to 'Analytics for iOS' page. Select which language you use for iOS development, for me I select Swift
  • Then click on 'GET A CONFIGURATION FILE' button, it looks like this:
    
  • It will bring you to this page. where you can: 
    • Specify your app name and iOS bundle ID
    • Choose and configure services (check Analytics)
    • Download generated GoogleService-Info.plist file
 3. Update your iOS app to include GA and configure GGLContext
  • Add GoogleService-Info.plist to your iOS app
  • Add #Import "Google/Analytics.h" to your app's Bridging-Header.h
  • Add GA related header files to your project
  • Override AppDelegate's didFinishLaunchingWithOptions to configure 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

 4. Add screen tracking
  • In your major screen view controller, add tracking code to the end of its viewWillAppear method:
   let tracker = GAI.sharedInstance().defaultTracker
   tracker.set(kGAIScreenName, value: screenName)

   let builder = GAIDictionaryBuilder.createScreenView()
   tracker.send(builder.build() as [NSObject : AnyObject])

 Verify tracking
  • Run your iOS app, navigate to screens with GA tracking code
  • In GA Real-Time, you should be able to see a screen view:)