Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 998
Slinky Avatar asked Mar 30 '26 22:03

Slinky


1 Answers

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        
     }
like image 134
Shmidt Avatar answered Apr 02 '26 21:04

Shmidt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!