Implementing Google Analytics in iOS application

When a mobile application is developed for a wide range of users across the world, then the performance of the application becomes a priority. The marketing panel of the application needs to keep a track of the overall performance of the application throughout the world off the shelf.

Google Analytics is such a framework that provides the answer to all such aspects that only the marketers as well as the developers are looking for to implement in their mobile application. So, “What is ‘Google Analytics’?” 

In simple terms Google Analytics is a service offered by Google that generates detailed statistics about a website’s or mobile application’s traffic. The Google Analytics lets you measure user interactions with your business across various devices and environments. This platform provides all the computing resources to collect the information about the application, stores and processes it and provides a detailed report out of it.

Google Analytics can be implemented to track mobile (iOS and Android) as well as website and other low level protocols. Here, as we are concerned about our iOS application so we will leave behind other platforms. You can check the following link to know about other platforms as well.

https://developers.google.com/analytics/devguides/platform/

This blog will dive you through the prerequisites of deploying the Google Analytics framework, how to implement the framework and some basic tracking and then will take you through the advanced level by discussing about the event tracking and about the multiple trackers and how can we visualize the application’s performance. So, here it is:

  1. Prerequisites of deploying the framework:

Before we implement the Google Analytics SDK, please make sure that you have:
a. iOS developer SDK 5.0 later (updated)— if you don’t have you can download it from:
https://developer.apple.com/devcenter/ios/index.action

b. Google Analytics for mobile apps iOs SDK v3 (updated)– For this, please create a google account or just sign in if you have a google account. Then,

I. Go to the link: http://www.google.co.in/analytics/ and click on “Access Google Analytics” button.

Access Google Analytics

Access Google Analytics

II. You will be able to view the following screen:

III. Click on the Sign Up button and create profile. Please refer the screenshot.entering all the information, you will be provided tracking ID.

New Account

New Account

C. An iOS app that you want to measure using Google Analytics – Specify     the name of the application in the field “App Name” above  whose performance you want to measure.

2.      Implementing the Google Analytics SDK:

Please follow the steps below to add the SDK and configure it:

1. Download the SDK when you receive the tracking ID.
2. Add the following files from the SDK package to your app:

  • GAI.h
  • GAITracker.h
  • GAITrackedViewController.h
  • GAIDictionaryBuilder.h
  • GAIFields.h
  • GAILogger.h
  • libGoogleAnalyticsServices.a

3.  Please add the following frameworks to your application:

  • AdSupport.framework
  • CoreData.framework
  • SystemConfiguration.framework
  • libz.dylib

4. Now, we need to initialize the tracker. For this you need to import ‘GAI.h’ file in your ‘AppDelegate.m’ and write the following code:

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
		{
 			// Optional: automatically send uncaught exceptions to Google Analytics.
		        	[GAI sharedInstance].trackUncaughtExceptions = YES;

		 	// Optional: set Google Analytics dispatch interval to e.g. 20 seconds.
 		 	[GAI sharedInstance].dispatchInterval = 20;

 			// Optional: set Logger to VERBOSE for debug information.
		 	[[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];

 			// Initialize tracker.
 			id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"UA-XXXX-Y"];
	       	}

5. Now, we need to know how to track the screens. We can do it automatically by just having our view controller extend “GAITrackedViewController.h” and set the property ‘screenName’ like:

‘ self.screenName = @”Home Screen”; ‘
OR, we can also do it manually because everytime we cannot extend the class for reasons. So we can add the following in ‘viewDidLoad’ of the view controller.

// May return nil if a tracker has not yet been initialized with a property ID.

id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];

// Sending the same screen view hit using  createAppView

[tracker send:[[[GAIDictionaryBuilder createAppView] set:@”Home Screen”

forKey:kGAIScreenName] build]];

3.    Advanced Configuration of the SDK:

This section deals with how do we set and send data to Google Analytics, using multiple trackers and sampling, opt-out and dry run features.
1. Set and send data to Google Analytics — We set only those values to the tracker that persist across multiple hits. Below is an example to show how do we set and send data:

// May return nil if a tracker has not yet been initialized with a property ID.
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
// Set screen name on the tracker to be sent with all hits.
[tracker set:kGAIScreenName value:@"Home Screen"];
// Send a screen view for "Home Screen".
[tracker send:[GAIDictionaryBuilder createAppView]];
// This event will also be sent with &cd=Home%20Screen.
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"UX"
action:@"touch" label:@"menuButton" value:nil] build]];
// Clear the screen name field when we're done.
[tracker set:kGAIScreenName value:nil];

II. Using multiple trackers – We can also add multiple trackers into our application.

// t1 becomes the default tracker because it is the first tracker initialized.
			id<GAITracker> t1 =[[GAI sharedInstance] trackerWithTrackingId: @"UA-XXXX-1"];

			id<GAITracker> t2 = [[GAI sharedInstance] trackerWithTrackingId:
			@"UA-XXXX-2"];

			// Returns t1.
			id<GAITracker> defaultTracker = [[GAI sharedInstance] defaultTracker];

			// Hit sent to UA-XXXX-1.
			[defaultTracker send:[[[GAIDictionaryBuilder createAppView]
				set:@"Home Screen" forKey:kGAIScreenName] build]];

			// Override the default tracker.
			[[GAI sharedInstance] setDefaultTracker:t2];

			// Returns t2.
			defaultTracker = [[GAI sharedInstance] defaultTracker];

III. Sampling & App level opt-out – Sampling is used to limit the number of hits sent to Google Analytics. Sampling refers to analysing a subset of the whole data set as it will provide the same report as the whole data set. It is very useful when we work on large data sets. In order to ensure that the default tracker is sampling data consistently you should maintain a constant sample rate for each version of the app that you release. For this, please add the following in your ‘AppDelegate.m’ ‘didFinishLaunchingWithOptions’ method:

NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:	(NSString *)kCFBundleVersionKey];
			// Fetches the current app version and sets it to the tracker object and then sets the sampling rate to 50%.
				[tracker set:kGAIAppVersion value:version];
				[tracker set:kGAISampleRate value:@"50.0"];

Enabling app level opt-out specifies that you need to disable the Google Analytics  across the entire app. We can do this by:
[[GAI sharedInstance] setOptOut:YES];

IV. Performing testing & debugging – Testing & debugging can be a lot easier with Google Analytics. We can perform dry run by setting the dry run flag to YES.

                                    [[GAI sharedInstance] setDryRun:YES];
Google Analytics also has 4 logLevels: kGAILogLevelError, kGAILogLevelInfo,
kGAILogLevelWarning & kGAILogLevelVerbose. We can set any of them like:
[[GAI sharedInstance].logger setLogLevel:kGAILogLevelVerbose];

4. Analysing the report:

Till now, we have been working on the coding section. Now, we need to analyse the report.You can also create a dummy project by following the guidelines from the link: http://www.raywenderlich.com/53459/google-analytics-ios

For analysing the report, access the Google Analytics, we click on the ‘Reporting’ section and can access the dashboard for overall view. We can also analyse the app in real-time when we will be able to know how many active users are there. In addition to this, we can check on the events, the active screens and crashes & exceptions in different versions and get their description by clicking on them. Please refer to some screen shots attached for better understanding.

There is lot more to EXPLORE about Google Analytics…

Google Analytics

Google Analytics

 

 

Written By: Neha Sinha, Mobile Developer, Mindfire Solutions

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s