Google Analytics for iOS
Learn how to add usage tracking and other metrics into your iOS app with this Google Analytics Tutorial for iOS! By .
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Google Analytics for iOS
30 mins
- Getting Started: What is Google Analytics?
- Lap 1: Setting Up Google Analytics for Your App
- Downloading the Google Analytics iOS SDK
- Lap 2: Setting Up the Google Analytics SDK
- Lap 3: Tracking
- Initializing the Tracker
- Automatic Screen Tracking
- Manual Screen Tracking
- Lap 4: Advanced Configuration
- Setting and Sending Data
- Applying Values to Multiple Measuring Events
- Using Multiple Trackers
- Sampling Rate: Avoiding Inconsistencies
- Opting Out
- Where to Go From Here?
Lap 4: Advanced Configuration
Now that you are on the home straight lets look at recording finer grained interactions such as button presses and the use of multiple trackers.
Setting and Sending Data
You can send specific data to Google Analytics if you want to record finer-grained user interactions. These can range from button presses to implicitly triggered events such as network calls. You can send this data by first setting maps of key-value pairs on the tracker and then sending them to Google using the methods set:
and send:
, respectively. You already saw an example of this in the previous section.
Recap: In viewDidAppear
, you set the screenName
and make a request to the tracker to send the information to Google Analytics.
Recap: In viewDidAppear
, you set the screenName
and make a request to the tracker to send the information to Google Analytics.
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker set:kGAIScreenName value:@"Stopwatch"];
[tracker send:[[GAIDictionaryBuilder createAppView] build]];
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker set:kGAIScreenName value:@"Stopwatch"];
[tracker send:[[GAIDictionaryBuilder createAppView] build]];
Applying Values to Multiple Measuring Events
To capture tap events, you can specify the kGAIHitType
in the events parameters. Let’s add a method to log button presses on the Stopwatch screen. You will call this method in the IBActions
associated with each of the Stopwatch buttons.
First, add this method to StopWatchViewController.m:
-(void)logButtonPress:(UIButton *)button{
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker set:kGAIScreenName value:@"Stopwatch"];
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"UX"
action:@"touch"
label:[button.titleLabel text]
value:nil] build]];
[tracker set:kGAIScreenName value:nil];
}
First you get a handle of the default tracker. As before, you need to set the current screenName
on the tracker to ensure the button press is recorded for the correct screen.
However, you’re not recording the act of the user viewing the screen. So instead of sending a screen view, you create a touch event, which is categorized as a UX event in Google’s framework. You send the title of the button as the title of the event. Finally, you remove the screenName
set on the tracker to prevent other events from being associated with the wrong screen.
In other words, the tracker object is not just a transparent conduit for passing messages. It carries state, which affects the messages you send. The only values that you should set directly on the tracker are those you want to persist across multiple hits.
Setting a screenName
on a tracker makes sense, since you can apply the same value to subsequent screen views and event hits. However, I don’t recommend that you set a field like hit type on the tracker, because it will likely change with each hit.
Call this method in both the startToggle
and reset
methods to record when a user presses these buttons:
-(IBAction)startToggle:(id)sender{
[self logButtonPress:(UIButton *)sender];
...
}
-(IBAction)reset:(id)sender{
[self logButtonPress:(UIButton *)sender];
...
}
Build and run the app. Switch to the Stopwatch page, toggle the stopwatch on and off a few times and then hit the reset button to record some data.
To view this information, go back to Google Analytics and navigate to your app’s statistics. On the left side of the page, expand Behavior->Events->Overview and you will see the events you just initiated.
Using Multiple Trackers
It is possible to use multiple trackers in your app. This can be useful for sending data to different Google Analytics properties associated with the same app. For example, maybe you have one property set up for your marketing team, who require information about how the app is being used, and another set up for your development team, who are looking at problem areas.
Note: In this tutorial you are not going to add multiple trackers to your example. This section just gives you an idea how to implement this in case you need multiple trackers.
Note: In this tutorial you are not going to add multiple trackers to your example. This section just gives you an idea how to implement this in case you need multiple trackers.
Setting up multiple trackers is as simple as initializing trackers with different property IDs:
id<GA Tracker> tracker1 = [[GAI sharedInstance] trackerWithTrackingId:@"UA-XXXX-1"];
id<GAITracker> tracker2 = [[GAI sharedInstance] trackerWithName:@"Tracker2"
trackingId:@"UA-XXXX-2"];
By default, the first tracker initialized becomes the default tracker made available through the GAI shared instance. You have seen how to access this perviously.
id<GA Tracker> tracker = [[GAI sharedInstance] defaultTracker];
If you consider the previous two code blocks together the tracker returned as the default tracker associated with GAI would refer to tracker1
. If you want to change the default tracker you can override it like so:
[[GAI sharedInstance] setDefaultTracker:tracker2];
In this case every subsequent call to the default tracker will access tracker2
.
Sampling Rate: Avoiding Inconsistencies
Sampling in Google Analytics or in any analytics software refers to the practice of selecting a subset of data from your data set and reporting on the trends available in that sample set. Sampling is widely used in statistical analysis because analyzing a subset of data gives similar results to analyzing all of the data. Sampling speeds up processing for reports when the volume of data is so large as to slow down report queries. This is important if your user-base is extremely large.
Earlier you covered three key terms, Accounts, Properties, and Views. When creating a view on a particular property (think of this as a window into your app’s usage from a given perspective) you must ensure that the data being sent has been sampled at the same rate. If this is not the case, data that is sampled at a higher frequency (events logged more often) will overshadow those sampled at a lower frequency (events logged less often) which can skew your analysis.
To ensure that your tracker is sampling data consistently you should maintain a constant sample rate for each version of the app you release. Updating the tracker with the current version number is easy: add the following code to your AppDelegate
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
[tracker set:kGAIAppVersion value:version];
[tracker set:kGAISampleRate value:@"50.0"];
}
This code will fetch the current app version from your Xcode Info.plist file automatically and set it to the tracker object. It then sets a 50% sampling rate.
Now change the version number of your app to 1.2 in the project settings.
When you run the application the tracker will reflect this change and record all data with the correct version number. The final step is to create a view in Google Analytics to filter the data by version number.
Go back to your Google Analytics dashboard and on the right-hand view drop down choose create new view.
Be sure to to select mobile app and create a name for this new view, here I am using Clock – v1.2.
This will successfully create a new view and as next step you have to set a filter to specify what data you want that view to show.
Choose the filters option below the view drop down and choose new filter (red button that comes in).
Apply the appropriate filter settings as shown below. You are filtering the data by app version and in this case you are only looking at data from version 1.2.
Now Build and Run the iOS app and watch the data stream in on your newly created view by selecting Reporting->Real-Time->Screens