Welcome to Day 30 of the 31 Days of iOSYesterday, we talked about putting advertisements into iOS applications using iAd.  Today we’re going to look at an important way of understanding how your app is used:  adding analytics.  Specifically we’ll be looking at adding Google Analytics into your applications.  Analytics allow you to log the usage of your apps so, without asking the people using your app, you can know what features they’re using, how often, and much much more.  We’ll be starting with a brand new project, but if you’d like to follow along with the completed code, you can access it here.  You’ll need to change the tracking ID in the AppDelegate.h to one of your own (which will make more sense as you read along) before it will work correctly.

Creating our project and handling the UI

Open up Xcode and choose File, New, Project.  We’ll use a Single View Application and name it DayThirty:

New project for Day 30

For today’s project, we’re going to add a few things to our user interface.  Open up MainStoryboard.storyboard.  The first thing we’re going to do is click on your view and go to the Editor menu and choose Embed In and selected Navigation Controller.  Afterwards, your storyboard should look like this:

Adding a Nav Controller

The next thing we’ll do is drop a few things into our UI including a text field, and two round rect buttons.  Afterwards, the UI will look like this:

Day 30 UI with buttons

Open the Assistant Editor in the top right of Xcode and control + click and drag from the text field and first button to your code behind.  For the text field, you’ll want to create an outlet and for the button, an action.  When you’re done, the code will look like this in ViewController.h:

Now, close the Assistant Editor.  Drag a new View Controller on to your storyboard.  Then control + click and drag from the second button to the new View Controller.  In the Action Segue modal that pops up when you release, choose push.  When you’re done, the storyboard should look like this:

Storyboard with segue

Now we’re finally ready to start adding analytics.

Adding the Google Analytics SDK

The first thing we need to do is download the Google Analytics SDK for iOS.  As of writing, the recommended version is the 2.0 beta 4.  This may be different when you read this but unless the version is drastically different, everything here should be pretty much the same. After you unzip the library into your apps directory, expand the Frameworks group in the Xcode Project Navigator.  Right click on Frameworks and choose New Group and name it GoogleAnalytics.  Right click on that group then and choose Add Files.  Navigate to the Library folder in what you unzipped.  Select the following files: GAI.h, GAITracker.h, GAITrackerController.h, GAITransaction.h, GAITranscationItem.h, and libGoogleAnalytics.lib.  When you’re done, your project will look like this:

Added GAI files

Since Google Analytics makes use of CoreData and SystemConfiguration, we need to do one more thing for our project.  Click the project in the project navigator so you pull up the target properties.  Click on Build Phases and expand Link Binary with Libraries.  Choose CoreData.framework and SystemConfiguration.framework.  Your libraries will then look like this:

Libraries for Google Analytics

Now before we can proceed with the code, we need to do a couple things in the Google Analytics portal. 

Creating a Google Analytics account and app

Go to http://www.google.com/analytics and sign in or create an account if you don’t already have one.  Once signed in, click the Admin button near the top right.  If you already have an account you want to add your mobile app to, choose that one, otherwise, create an account using the + New Account button and then select the account.  If you are creating a new account, make sure you select App when asked what you would like to track.  If you already have an account, click the + New Property (web or app) button and fill out the information needed.  When you’re done, you should be presented with a Tracking ID that will look like this:

Google Analytics Tracking ID

Now we can start working with our code.

Setting up the tracker

Return to Xcode and open the AppDelegate.m file.  First import the GAI.h class in the file, and then we’re going to make some changes to application: didFinishLaunchingWithOptions:

Here all we’re doing is setting up the tracker to use the tracking ID we have above.  This is all we have to do before we start tracking things, however, there are a few additional options we’re going set (as Google recommends in their docs):

The first thing we’re setting is trackUncaughtExceptions.  This makes it so our tracker will track and log any uncaught exceptions.  Next we have dispatchInterval.  This controls how often tracking information is sent to the analytics servers  If it’s 20, as we’ve set it, then at the most frequent, tracking information will be sent every 20 seconds.  If this is set to zero, then tracking information will be sent immediately.  If it’s a negative value, the tracker will never automatically send over information, but will require you to call it’s dispatch method on it.  Lastly, the default value is 120 so if you don’t set it to anything, that’s what will be used.  The last thing we’re setting is the debug property.  Setting this means that messages will also be sent to the debugger using NSLog which is helpful for debugging your calls to the tracker.

Logging screen views

The first thing we’re going to look at is how to log screen views.  Fortunately, Google has provided a built in way to do this with ViewControllers.  First, open up ViewController.h and import that GAITrackedViewController.h class.  Next, replace the class your view controller subclasses from UIViewController to GAITrackedViewController.  When you’re done, your class header will look like this:

Now before switching over to the implementation, try running your app.  You should see a debug message show up in the debug console:

2013-01-22 21:41:34.535 DayThirty[6740:c07] GoogleAnalytics 2.0b4 -[GAITrackedViewController viewDidAppear:] (GAITrackedViewController.m:26) DEBUG: Tracked view controller missing view name.

When you use a GAITrackedViewController it automatically tracks the view whenever it appears.  However, in order to do this, you also need to specify the view’s name.  Open up ViewController.m and set the controller’s trackedViewName in the viewDidLoad method like this:

Now run your app again.  Now you’ll see a different message:

2013-01-22 21:44:58.903 DayThirty[6788:c07] GoogleAnalytics 2.0b4 -[GAIDispatcher internalCreateTimer] (GAIDispatcher.m:195) DEBUG: Created timer to fire every 20.0s

And then a little bit later:

2013-01-22 21:45:19.094 DayThirty[6788:c07] GoogleAnalytics 2.0b4 -[GAIDispatcher dispatchComplete:withStartTime:withRetryNumber:withResponse:withData:withError:] (GAIDispatcher.m:394) DEBUG: Hit /GAIHit/p1 dispatched (182ms): HTTP status 200
2013-01-22 21:45:19.096 DayThirty[6788:c07] GoogleAnalytics 2.0b4 -[GAIDispatcher dispatchComplete:withStartTime:withRetryNumber:withResponse:withData:withError:] (GAIDispatcher.m:415) DEBUG: Successfully dispatched hit /GAIHit/p1 (0 retries).
2013-01-22 21:45:19.097 DayThirty[6788:c07] GoogleAnalytics 2.0b4 -[GAIDispatcher dispatchComplete:withStartTime:withRetryNumber:withResponse:withData:withError:] (GAIDispatcher.m:422) DEBUG: Pending hit queue drained.
2013-01-22 21:45:19.097 DayThirty[6788:c07] GoogleAnalytics 2.0b4 -[GAIDispatcher cancelTimer] (GAIDispatcher.m:224) DEBUG: Canceled timer with interval 20.0s

So after the 20 seconds, the tracking message is dispatched to the server.  We received a 200 response code and then the timer was turned off because we didn’t have any other messages.  Now if you go to the analytics website for your app and expand the Engagement section and look at Screens, you’ll see one hit against My First Screen:

Tracking our frist screen

If you want to track a screen view without using the GAITrackedViewController class, you could do so by getting a reference to the tracker and calling this:

[tracker sendView:@”Home Screen”];

Tracking events

Now that we’ve seen how to track screen views let’s look at how to track events.  Events could be anything from a button press or data entry / selection, use of an item in a game, use of a feature or anything else.  There are four values you can track when an event occurs: Category, Action, Label, and Value.  All but the Value are strings and Value is a number.  Let’s return to ViewController.m and track a button press:

Now when you run the app and tap the first button, a few minutes later we’ll see the info show up in the analytics portal.  Under Engagement, go to Events and then Overview:

Events in the Analytics site

This is where you start to see some of the amazing power (and complication) with using something like Google Analytics.  We can choose whatever standard or format we want for the data we send over for events.  Here I’ve used the screen as the category, what the user did (press a button) as the action, and the name of the button for the label.  However, I could choose to do whatever I want.  For example, since I have a label on my screen, I could pull the text value out and send that over as one of the properties.  You’re not bound to do anything specific, but that does mean you need to pick your own format for what you’re going to send over.

Other tracking

There is a lot of other options when it comes to tracking with Google Analytics.  We aren’t going to cover any of them in depth, but now that you’ve seen the basics, you should be able to figure them out from the documentation:

Other platforms

We’ve only looked at using Google Analytics today but there are many other providers you can use for iOS app analytics including (just some of them):

Conclusion

Today we looked at implementing Google Analytics into your iOS apps.  Using GA, you can get insight into how your application is used, what features aren’t used, and what users are doing.  Even if you’re just building an app for fun, putting in analytics is a great idea so you understand what’s going on in your app.  Even more so, many of the providers have some sort of crash reporting which is really useful in making sure you know if your app is having problems.  You can download the completed code from today’s walkthrough here.  Remember that you’ll need to change the tracking ID in AppDelegate.h.


Chris Risner


Leave a Comment