Slinky
Slinky

Reputation: 5832

Implementing Google Analytics for iOS

As someone coming from experience with Flurry Analytics, can someone explain the correct location for implementing event tracking and custom variables in Google Analytics for iOS? The example that Google provides shoves everything into the AppDelegate. Not sure if they did that for the sake of brevity or not.

I can see why the init call goes in the AppDelegate:

//AppDelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application {

   [[GANTracker sharedTracker] startTrackerWithAccountID:@"UA-0000000-1"
                                    dispatchPeriod:kGANDispatchPeriodSec
                                          delegate:nil];

   //...

}

But what about these calls that collect specific data related to a specific view? Can they go into their respective ViewControllers instead of the AppDelegate?

[[GANTracker sharedTracker] setCustomVariableAtIndex:1
                                                   name:@"iPhone1"
                                                  value:@"iv1"
                                              withError:&error]

[[GANTracker sharedTracker] trackEvent:@"my_category"
                                   action:@"my_action"
                                    label:@"my_label"
                                    value:-1
                               withError:&error]

[[GANTracker sharedTracker] trackPageview:@"/app_entry_point"
                               withError:&error]

Questions

1) What Google Analytics for iOS calls shown above need to be in the AppDelegate?
2) What Google Analytics for iOS calls shown above can be put into the ViewControllers?

Thanks

Upvotes: 3

Views: 3342

Answers (1)

Shmidt
Shmidt

Reputation: 16684

You put first part into AppDelegate, that's right.

In viewDidLoad method of each viewController put:

NSError *error;

 if (![[GANTracker sharedTracker] trackPageview:@"/app_entry_point"    
 withError:&error]) {    
 // Handle error here    
 }

where @"/app_entry_point" should be the name of ViewController, for ex.: "/mainWindow".

Next piece of code used to track for your methods, used inside methods.

     NSError *error;
     if (![[GANTracker sharedTracker] trackEvent:@"my_category"        
     action:@"my_action"        
     label:@"my_label"        
     value:-1        
     withError:&error]) {        
     // Handle error here        
     }

Upvotes: 3

Related Questions