Configuring your Google Analytics Account
Google Analytics is a free server provided by Google. If you dont already have an account, you can sign up for one with your existing Gmail account. http://google.com/analytics. Sign up and log in.The first thing you will want to do is Add a new Website Profile.

On the next page:
- Select Add a Profile for a new domain
- Enter in some URL – This doesn’t really matter since google doesn’t use it to identify your app. A good convention is iphone.yoursite.com or appname.yoursite.com so that you know these stats are for your iPhone app.
- Select your country and timezone
- Click Finish

Pretty simple… On the next screen, make sure you copy the Web Property ID. This number (begins with UA) is what we will be using to uniquely identify your application.

That’s all for the web side of things! Now on to the iPhone part.
Download the Google Analytics Library and Add It To Your Application
This could not be easier. Head on Over to http://code.google.com/apis/analytics/docs/tracking/mobileAppsTracking.html . They explain much of the steps so you could read the rest there, but I will lay them out in detail here with screesnhots. Download the Analytics SDK for iPhone and extract it.There are 2 files that are important. Drag both files from the Library folder into your XCode project. These files are GANTracker.h and libGoogleAnalytics.a.

When XCode pops up the confirmation box, make sure that you check the box that says “Copy items into destination group’s folder (if needed)”. This will ensure that the code gets place in your project directory.
Include CFNetwork and Link Against the sqlite framework
The google analytics framework requires the use of CFNetwork (for interent connection detection) and the sqlite framework (most likely to store events when the user does not have internet).Let’s start by adding CFNetwork to the project. Right click on the frameworks folder and select Add -> Existing Frameworks. Next, select CFNetwork.framework from the list and click Add.

Now we need to link the project against libsqlite3.0.dylib. To do this Click Project -> Edit Active Target “<project name>” where <project name> is the name of your XCode project.
Next, click the + button at the bottom to bring up the library picker. Select libsqlite3.0.dylib and click Add

Including and Initializing the Tracker
Since the Analytics tracker is a singleton class, we only have to initialize it once and the rest of the application can use the same instance.Let’s start by opening up the app delegate and adding the code to import the Google Analytics framework.
#import "GANTracker.h"
[[GANTracker sharedTracker] startTrackerWithAccountID:@"UA-15609865-3" dispatchPeriod:10 delegate:nil];
Tracking Pageviews and Custom Events
Google gives you 2 different ways to track how your users use the application. The first way is via pageviews. Pageviews work just like they do in the browser as if the user navigated to a new page in your website. Some examples of when to use pageview are when you have a tab interface or a flipside view controller. I would not suggest using a pageview every time the user performs an action such as click a button.The second way is via custom events. These are really cool. With custom events, you can easily see HOW your users are using your application. Examples of events are button clicks, typing in a textbox, submitting high scores, etc…
We will see an example of implementing each of these methods and take a look at how they appear inside of the Google Analytics website.
Pageview method
Here is an example of a pageview when the user first launches the app. Put this code inside of your applicationDidFinishLaunching method.
NSError *error; if (![[GANTracker sharedTracker] trackPageview:@"/app_launched" withError:&error]) { // Handle error here }
Event method
Here is an example of an event that gets fired when the user presses a Save button.
- (IBAction) saveTheWorld:(id) sender { NSError *error; if (![[GANTracker sharedTracker] trackEvent:@"button_click" action:@"save_the_world" label:@"my_label" value:-1 withError:&error]) { // Handle error here } }
Now that we have implemented our code, let’s take a look inside of our Google Analytics account and see what the stats look like.

This is just some of our sample data from another app. Notice the interface is exactly the same as it is when you are tracking website statistics. It shows you unique visits as well as pageviews. You get all of the advanced reporting too including time spent on each “page”.
Now we take a look at events. To find the actions click Content -> Event Tracking inside of your Google Analytics Page.

In the above screenshot, we see tracking for an event called “clap”. It shows us the number of times the users “clapped” within a given day.
No comments:
Post a Comment