GAITrackedViewController and UITableViewController

IphoneIosGoogle Analytics

Iphone Problem Overview


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

Iphone Solutions


Solution 1 - Iphone

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

Solution 2 - Iphone

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!

Solution 3 - Iphone

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

Solution 4 - Iphone

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.

Solution 5 - Iphone

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"

Solution 6 - Iphone

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.

Solution 7 - Iphone

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpalmiView Question on Stackoverflow
Solution 1 - IphonedmzzaView Answer on Stackoverflow
Solution 2 - IphoneKeithView Answer on Stackoverflow
Solution 3 - IphoneesbenrView Answer on Stackoverflow
Solution 4 - Iphoneuser4951View Answer on Stackoverflow
Solution 5 - IphoneVarun KumarView Answer on Stackoverflow
Solution 6 - Iphones73v3rView Answer on Stackoverflow
Solution 7 - IphoneJramesh1967View Answer on Stackoverflow