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
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.
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.
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.
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.
Remember that extending GAITrackedViewController is only one way to track screen views. The manual way is just as easy.
- (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"]; }
#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]]; }
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With