GAITrackedViewController and UITableViewController

Manual Screen Tracking

Remember that extending GAITrackedViewController is only one way to track screen views. The manual way is just as easy.

SDK v2

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // returns the same tracker you created in your app delegate
    // defaultTracker originally declared in AppDelegate.m
    id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];

    // manual screen tracking
    [tracker sendView:@"Home Screen"];
}

SDK v3

#import "GAI.h"
#import "GAIFields.h"
#import "GAIDictionaryBuilder.h"

...

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // returns the same tracker you created in your app delegate
    // defaultTracker originally declared in AppDelegate.m
    id tracker = [[GAI sharedInstance] defaultTracker];

    // This screen name value will remain set on the tracker and sent with
    // hits until it is set to a new value or to nil.
    [tracker set:kGAIScreenName
           value:@"Home Screen"];

    // manual screen tracking
    [tracker send:[[GAIDictionaryBuilder createScreenView] build]];
}

Reference

https://developers.google.com/analytics/devguides/collection/ios/v2/screens#manual https://developers.google.com/analytics/devguides/collection/ios/v3/screens#manual


In an effort to clean up the manual tracking code in my Swift project, I created the following UIViewController extension.

extension UIViewController {
    func trackScreenView(screenName: String) {
        let tracker = GAI.sharedInstance().defaultTracker
        tracker.set(kGAIScreenName, value: screenName)
        tracker.send(GAIDictionaryBuilder.createAppView().build())
    }
}

It's probably not proper to use an extension this way, as I'm not using any properties from the UIViewController, but it's a convenient way that feels better than a global method. If you don't mind using your class name instead of a nicely formatted name, you could even use the NSStringFromClass(self.dynamicType) to get the ViewController class name like so:

extension UIViewController {
    func trackScreenView() {
        let tracker = GAI.sharedInstance().defaultTracker
        tracker.set(kGAIScreenName, value: NSStringFromClass(self.dynamicType))
        tracker.send(GAIDictionaryBuilder.createAppView().build())
    }
}

This allows me to add manual tracking from my UITableViewControllers with just the following code:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    trackScreenView("Detail View")  //Or call this without any arguments if using the NSStringFromClass idea 
}

Nice and clean. Enjoy!


As a result of the Sdk missing a GAITrackedTableViewController I created a simple neat implementation of the manual screen viev tracking.

Create a category for the GAI class, as this is already singleton and easy accessible.

#import "GAI+Tracking.h"
#import "GAIFields.h"
#import "GAIDictionaryBuilder.h"

@implementation GAI (Tracking)

- (void)trackScreenView:(NSString *)screenName
{
    [self.defaultTracker set:kGAIScreenName value:screenName];
    [self.defaultTracker send:[[GAIDictionaryBuilder createAppView] build]];
}

@end

Now just track a screen view like this

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[GAI sharedInstance] trackScreenView:@"Counts map screen"];
}

This kind of overules Googles idea of having more than one tracking at the same time. (I haven't had the need yet). To acommodate this just rename your tracking method after which tracker it uses, and use what ever tracker you want.

#import "GAI+Tracking.h"
#import "GAIFields.h"
#import "GAIDictionaryBuilder.h"

@implementation GAI (Tracking)

- (void)trackDefaultScreenView:(NSString *)screenName
{
    [self.defaultTracker set:kGAIScreenName value:screenName];
    [self.defaultTracker send:[[GAIDictionaryBuilder createAppView] build]];
}

@end

Make sure all your viewDidAppear call [super viewDidAppear]

Then make sure every single one of your subclass of UITableViewController is also a subclass of myTableViewController

in [myTableViewController viewDidAppear], implement all those other answers.

Just complementing all the other good answers out there. That way you get something as good as GAITrackedViewController.h

The same way I make sure all of my other UIViewController subclass is also a subclass of super UIViewController and then I do the same thing.