Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GAITrackedViewController and UITableViewController

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 
like image 700
palmi Avatar asked Apr 29 '13 22:04

palmi


People also ask

How does uitableviewcontroller work with a table view?

Table view controllers already adopt the protocols you need to manage your table view's content and respond to changes. In addition, UITableViewController implements the following behaviors: It automatically loads the table view archived in a storyboard or nib file. Access the table view using the tableView property.

How to use the Table View Controller?

The Table View Controller manages its Table View consisting of Table View Cells and an empty Content View in the cell: Select the section using the outline or with Shift + Click. Set Countries as Header text: Select the cells by Shift + clicking and configure Basic as Style for all cells. A Basic Cell has one Label by default: Run the app with ⌘R.

When to use subclass uitableviewcontroller in Salesforce?

Subclass UITableViewController when your interface consists of a table view and little or no other content. Table view controllers already adopt the protocols you need to manage your table view's content and respond to changes.

How to load the table view archived in a storyboard or nib?

It automatically loads the table view archived in a storyboard or nib file. Access the table view using the tableView property. It sets the data source and the delegate of the table view to self. It implements the viewWillAppear (_:) method and automatically reloads the data for its table view on first appearance.


2 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

like image 154
dmzza Avatar answered Sep 19 '22 02:09

dmzza


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!

like image 32
Keith Avatar answered Sep 21 '22 02:09

Keith