Saturday, June 11, 2011

Google Analytics in iPhone SDK

Hello Friends,

Today i would like to introduce one important topic about tracking your iPhone or iPad application with one of the best fee google analytical tool. This facility provides you complete tracking with many more functionalities. This analytical tool let you track any event in your application happens. We can analyze which screens of our application are visited most. And all such information we can access from google's website. We can filter out information for better understanding our application usage world wide.

Google provides different graphs and charts to analyze data. Google analytic SDK provides easy integration with application. We have to just add one framework and one header file which is available here.

Drag framework and header file given in sdk in your project and add the following code.
Import "GNATracker.h" file in App delegate class and write following code in didFinishedLaunching method of App delegate class.

// **************************************************************************
  // PLEASE REPLACE WITH YOUR ACCOUNT DETAILS.
  // **************************************************************************
  [[GANTracker sharedTracker] startTrackerWithAccountID:@"UA-00000000-1"
                                         dispatchPeriod:kGANDispatchPeriodSec
                                               delegate:nil];
  NSError *error;

  if (![[GANTracker sharedTracker] setCustomVariableAtIndex:1
                                                       name:@"iPhone1"
                                                      value:@"iv1"
                                                  withError:&error]) {
    NSLog(@"error in setCustomVariableAtIndex");
  }


Above code initiate your account and connection with google analytics. Now following code will be written at place where event or action should be tracked.

if (![[GANTracker sharedTracker] trackEvent:@"Application iPhone"
                                       action:@"Launch iPhone"
                                        label:@"Example iPhone"
                                        value:99
                                    withError:&error]) {
    NSLog(@"error in trackEvent");
  }