对于iOS v2的Google Analytics(分析),Google建议将其GAITrackedViewController
类替换为UIViewController
。对于UITableViewController,我们该怎么办?
#import "GAITrackedViewController.h"
@interface AboutViewController : GAITrackedViewController
Answers:
请记住,扩展GAITrackedViewController只是跟踪屏幕视图的一种方法。手动方式也很容易。
- (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
viewWillAppear
?寄送会更好viewDidAppear
吗?
viewDidAppear
文档中GAITrackedViewController.h
这样说。
[tracker send:[[GAIDictionaryBuilder createAppView] build]];
为了清理Swift项目中的手动跟踪代码,我创建了以下 UIViewController
扩展。
extension UIViewController {
func trackScreenView(screenName: String) {
let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIScreenName, value: screenName)
tracker.send(GAIDictionaryBuilder.createAppView().build())
}
}
以这种方式使用扩展可能是不合适的,因为我没有使用UIViewController的任何属性,但这是一种比全局方法更好的便捷方式。如果您不介意使用类名而不是格式正确的名称,则甚至可以使用NSStringFromClass(self.dynamicType)
来获取ViewController类名,如下所示:
extension UIViewController {
func trackScreenView() {
let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIScreenName, value: NSStringFromClass(self.dynamicType))
tracker.send(GAIDictionaryBuilder.createAppView().build())
}
}
这使我可以使用以下代码从UITableViewControllers添加手动跟踪:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
trackScreenView("Detail View") //Or call this without any arguments if using the NSStringFromClass idea
}
干净整洁。请享用!
由于Sdk缺少GAITrackedTableViewController,因此我创建了一个简单的,简单的手动屏幕筛选跟踪实现。
为GAI类创建一个类别,因为它已经是单例且易于访问。
#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
现在只需跟踪这样的屏幕视图
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[GAI sharedInstance] trackScreenView:@"Counts map screen"];
}
谷歌的想法是同时进行多个跟踪。(我还没有需要)。要解决这个问题,只需在跟踪方法使用后重新命名您的跟踪方法,然后使用您想要的跟踪器即可。
#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
对于Swift,您可以使用
AppDelegate.appDelegateIns.defTrackerIns?.set(kGAIScreenName, value: "Enter Your Screen Name")
AppDelegate.appDelegateIns.defTrackerIns?.send(GAIDictionaryBuilder.createScreenView().build() as [NSObject : AnyObject])
将此包含在您的应用程序委托中
static let appDelegateIns = AppDelegate()
let gai = GAI.sharedInstance()
let defTrackerIns = GAI.sharedInstance().tracker(withTrackingId: "Your Tracking Id")
let gaDefTargetURL = "https://developers.google.com/analytics"
假设他们自己没有UITableViewController的子类,我不明白为什么您不能使用他们的GAITrackedViewController,然后自己实现UITableViewDataSource和UITableViewDelegate协议。
UITableViewController
不会加载任何行。:( 1)No visible @interface for 'GAITrackedViewController' declares the selector 'initWithStyle:'
2)Property 'clearsSelectionOnViewWillAppear' not found on object of type 'MenuViewController *'
UITableViewController继承自UIViewController:UIResponder:NSObject
可以对GAITrackedViewController进行子类化,并且对于继承自UIViewController的几乎所有ViewController都不需要做任何额外的事情。