Dec 18

Google Analytics is a powerful web analytics solution used by thousands of web sites. Like most Google products it’s free for most users.

Earlier I had toyed with the idea of adding a small UIWebView to each screen in an iPhone app to make use of Google Analytics. Since that would have been a kludge, I was happy to see that Google has introduced a native iPhone library that interfaces with Google Analytics.

Why would you choose Google Analytics over the many existing analytics solutions for the iPhone, Pinch Media, Medialets, Mobclix, et. al?

  • Google Analytics tracks user behavior, whereas most other services just track number of users with a particular attribute or who have done a particular action.
  • Google Analytics lets you slice and dice data in a seemingly infinite number of ways.
  • If you’re not using Location Services in your app to report the location of the user, then the analytics tool has to rely on IP address lookup for the location. Google has a much better database for this. (Note that determining a location by IP address is reasonably accurate when users are on WiFi, but all bets are off when users are on a mobile network.)

There are some drawbacks with the current version of Google Analytics:

  • The current version is 0.7 which means that there are some rough edges and bugs.
  • The simulator is identified as an iPhone device is the stats. This is not a big deal since the number of users on the simulator (i.e. you) should be dwarfed by the number of actual customers.
  • Another minor issue is that all devices are reported as supporting Java. 🙂
  • The OS version is not reported anywhere.
  • It’s not possible to tell Google Analytics the exact location of the user, should you know that from Location Services.
  • It’s not possible to create custom segmentations. This is a really powerful feature of GA, and it would be nice if that was exposed through the iPhone library.
  • If you’re not familiar with Google Analytics, it can be a daunting experience to explore and understand all the stats reported.

So which solution should you choose? My recommendation is both. I use both Google Analytics and Pinch Media, and I find that they complement each other well.

Adding Google Analytics to your iPhone app is easy.

  1. Download the library here. See the very bottom of the page for the download link. (There used to be a regular Google Code project for this library, but that mysteriously disappeared.)
  2. Add the libGoogleAnalytics.a static library and the GANTracker.h header file to your project.
  3. Add the framworks required by Google Analytics. If you already have another analytics library installed, then you probably already have the necessary frameworks added.
  4. Add a few lines of code to initialize the tracking and then a few lines where you want to track a page view or an event. See the included sample project for examples.

If you’re going to use more than one analytics service, you should consolidate the calls to each service in one place. Below are two methods that I use for Google Analytics and Pinch Media. Note that since Pinch Media doesn’t have any concept of page views, that method only calls Google Analytics, whereas both services track events.

- (void)trackPageview:(NSString *)pageName {
  // Google Analytics
  NSError *gaError;
  if (![[GANTracker sharedTracker] trackPageview:pageName withError:&gaError]) {
    NSLog(@"GA trackPageView Error: %@", [gaError localizedDescription]);

- (void)trackEvent:(NSString *)category
            action:(NSString *)action
             label:(NSString *)label
             value:(NSInteger)value {
  // Google Analytics
  NSError *gaError;
  if (![[GANTracker sharedTracker] trackEvent:category
                                    withError:&gaError]) {
    NSLog(@"GA trackEvent Error: %@", [gaError localizedDescription]);
  // PinchMedia
  NSString *subBeaconName = [NSString stringWithFormat:@"%@ - %@ - %@", category, action, (label ? label : @"")];
  @try {
    [[Beacon shared] startSubBeaconWithName:subBeaconName timeSession:NO];
  } @catch (NSException * e) {
    NSLog(@"Error starting SubBeacon %@ : %@", subBeaconName, [e description]);

written by Nick \\ tags:

6 Responses to “Google Analytics for iPhone Apps”

  1. Akram Says:

    Can you know, by using it in your app, from where did your app users came from?

  2. Nick Says:

    @Akram: You can see where users come from geographically, but you cannot see which web page they came from when they downloaded your app. (The latter is possible in the Android version of the analytics library.)

  3. Brad Waller Says:

    Apple is using their new location policy (not to use location services only for ads, and for user benefit) to reject apps that use location services with Google Analytics. You might want to not use location services if your app does not require it.

  4. Nick Says:

    @Brad: Since Google Analytics for the iPhone does not use Core Location, GA in itself is safe. With the other analytics libraries that I’m familiar with you have the option of allowing the analytics to access Core Location. It is here you have to be careful. As you point out, if you only use Core Location for the purpose of analytics then Apple’s new policy is to reject the app.

  5. jstar Says:

    I was wondering how to do this without manually loading a page with the GA javascript code inside it. Thanks for the post.

  6. Deepthi Says:

    By using the new iPhone SDk, I am not getting the device info in the new GA interface. Mobile->Devices->Mobile Device Info->(not set). It always says not set, is this an drawback? Or is there a way to set the device info?

Leave a Reply