Home » Ios » GAITrackedViewController and UITableViewController

GAITrackedViewController and UITableViewController

Posted by: admin November 30, 2017 Leave a comment

Questions:

With Google Analytics for iOS v2 Google suggests subclassing their GAITrackedViewController class in place of UIViewController. What do we do in the case of UITableViewController?

source

#import "GAITrackedViewController.h"

@interface AboutViewController : GAITrackedViewController
Answers:

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

Questions:
Answers:

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!

Questions:
Answers:

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

Questions:
Answers:

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.

Questions:
Answers:

For Swift you can use

AppDelegate.appDelegateIns.defTrackerIns?.set(kGAIScreenName, value: "Enter Your Screen Name")
        AppDelegate.appDelegateIns.defTrackerIns?.send(GAIDictionaryBuilder.createScreenView().build() as [NSObject : AnyObject])

Include This in your App Delegate

static let appDelegateIns = AppDelegate()
    let gai = GAI.sharedInstance()
    let defTrackerIns = GAI.sharedInstance().tracker(withTrackingId: "Your Tracking Id")
    let gaDefTargetURL = "https://developers.google.com/analytics"

Questions:
Answers:

Assuming they don’t have a subclass of UITableViewController themselves, I don’t see why you couldn’t use their GAITrackedViewController, and then implement the UITableViewDataSource and UITableViewDelegate protocols yourself.

Questions:
Answers:

From https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewController_Class/Reference/Reference.html

UITableViewController Inherits from UIViewController : UIResponder : NSObject

On can subclass GAITrackedViewController and not do anything extra for virtually any kind of ViewController that inherits fro UIViewController.