I want to add google Analytics ios into my project but i got some problems when i tried to paste their code into my app like the code below.
我想在我的项目中添加谷歌分析ios,但是当我试图将他们的代码粘贴到我的应用程序时,我遇到了一些问题,比如下面的代码。
var tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIScreenName, value: "rootPlayView")
var builder = GAIDictionaryBuilder.createScreenView()
tracker.send(builder.build() as [NSObject : AnyObject])
Then i got an error like this
然后我得到一个像这样的错误
how can i fix this?
我该怎么解决这个问题呢?
Thanks!
谢谢!
4 个解决方案
#1
45
type converson
类型converson
Swift3
Swift3
Screen Track
屏幕跟踪
let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIScreenName, value: "Home")
tracker.send(GAIDictionaryBuilder.createScreenView().build() as [AnyHashable : Any])
Objective-C
objective - c
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
// Set the screen name on the tracker so that it is used in all hits sent from this screen.
[tracker set:kGAIScreenName value:@"Home"];
[tracker send:[[GAIDictionaryBuilder createScreenView] build]];
Automatic Screen Tracking
屏幕自动跟踪
Swift3
Swift3
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self!.screenName = "About Screen"
}
Objective-C
objective - c
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.screenName = @"About Screen";
}
event Track
事件跟踪
Swift3
Swift3
let tracker = GAI.sharedInstance().defaultTracker
tracker!.send(GAIDictionaryBuilder.createEventWithCategory("ui_action", action: "button_press", label: "menuButton", play: nil).build())
Objective-C
objective - c
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action" // Event category (required)
action:@"button_press" // Event action (required)
label:@"play"
value:nil] build]];
or use
或使用
weak var tracker = GAI.sharedInstance().defaultTracker
// Set screen name on the tracker to be sent with all hits.
tracker!.set(kGAIScreenName, value: "Home Screen")
// Send a screen view for "Home Screen".
// [tracker send:[[GAIDictionaryBuilder createAppView] build]];
// Previous V3 SDK versions.
tracker!.send(GAIDictionaryBuilder.createScreenView().build())
// SDK Version 3.08 and up.
// This event will also be sent with &cd=Home%20Screen.
tracker!.send(GAIDictionaryBuilder.createEventWithCategory("UX", action: "touch", label: "menuButton", value: nil).build())
// Clear the screen name field when we're done.
tracker!.set(kGAIScreenName, value: nil)
for more information see this
有关更多信息,请参见本文
example
例子
try this
试试这个
var builder = GAIDictionaryBuilder.createScreenView().build() as! [NSObject : AnyObject]
tracker.send(builder)
for screen track
对屏幕跟踪
let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIDescription, value: "rootPlayView")
let builder: NSObject = GAIDictionaryBuilder.createScreenView().build()
tracker.send(builder as! [NSObject : AnyObject])
for event Track
事件跟踪
let tracker = GAI.sharedInstance().defaultTracker
let builder: NSObject = GAIDictionaryBuilder.createEventWithCategory(
"xxxx",
action: "buttonclicked",
label: "you pressed xxx button",
value: nil).build()
tracker.send(builder as! [NSObject : AnyObject])
#2
27
This answer has been updated for Swift 3
这个答案已经更新为Swift 3
let tracker = GAI.sharedInstance().defaultTracker
let eventTracker: NSObject = GAIDictionaryBuilder.createEvent(
withCategory: "SomeCategory",
action: "SomeAction",
label: "SomeLabel",
value: nil).build()
tracker.send(eventTracker as [NSObject : AnyObject]!)
OR...
还是……
let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIDescription, value: "ScreenViewName")
let eventTracker: NSObject = GAIDictionaryBuilder.createScreenView().build()
tracker.send(eventTracker as! [NSObject : AnyObject])
#3
8
For GA 3.17.0 use:
GA 3.17.0使用:
let tracker = GAI.sharedInstance().defaultTracker
let builder = GAIDictionaryBuilder.createEvent(withCategory: category,
action: action,
label: nil,
value: nil).build()
tracker?.send(builder as [NSObject : AnyObject]!)
Note the !
at the end of [NSObject: AnyObject]!
.
注意!在[NSObject: AnyObject]!
#4
0
Small follow up on the amazing answers, in swift 3, the method is
小跟进惊人的答案,在swift 3中,方法是
let builder:NSObject = GAIDictionaryBuilder.createEvent(withCategory: category, action: eventName, label: label, value: nil).build()
#1
45
type converson
类型converson
Swift3
Swift3
Screen Track
屏幕跟踪
let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIScreenName, value: "Home")
tracker.send(GAIDictionaryBuilder.createScreenView().build() as [AnyHashable : Any])
Objective-C
objective - c
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
// Set the screen name on the tracker so that it is used in all hits sent from this screen.
[tracker set:kGAIScreenName value:@"Home"];
[tracker send:[[GAIDictionaryBuilder createScreenView] build]];
Automatic Screen Tracking
屏幕自动跟踪
Swift3
Swift3
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self!.screenName = "About Screen"
}
Objective-C
objective - c
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.screenName = @"About Screen";
}
event Track
事件跟踪
Swift3
Swift3
let tracker = GAI.sharedInstance().defaultTracker
tracker!.send(GAIDictionaryBuilder.createEventWithCategory("ui_action", action: "button_press", label: "menuButton", play: nil).build())
Objective-C
objective - c
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action" // Event category (required)
action:@"button_press" // Event action (required)
label:@"play"
value:nil] build]];
or use
或使用
weak var tracker = GAI.sharedInstance().defaultTracker
// Set screen name on the tracker to be sent with all hits.
tracker!.set(kGAIScreenName, value: "Home Screen")
// Send a screen view for "Home Screen".
// [tracker send:[[GAIDictionaryBuilder createAppView] build]];
// Previous V3 SDK versions.
tracker!.send(GAIDictionaryBuilder.createScreenView().build())
// SDK Version 3.08 and up.
// This event will also be sent with &cd=Home%20Screen.
tracker!.send(GAIDictionaryBuilder.createEventWithCategory("UX", action: "touch", label: "menuButton", value: nil).build())
// Clear the screen name field when we're done.
tracker!.set(kGAIScreenName, value: nil)
for more information see this
有关更多信息,请参见本文
example
例子
try this
试试这个
var builder = GAIDictionaryBuilder.createScreenView().build() as! [NSObject : AnyObject]
tracker.send(builder)
for screen track
对屏幕跟踪
let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIDescription, value: "rootPlayView")
let builder: NSObject = GAIDictionaryBuilder.createScreenView().build()
tracker.send(builder as! [NSObject : AnyObject])
for event Track
事件跟踪
let tracker = GAI.sharedInstance().defaultTracker
let builder: NSObject = GAIDictionaryBuilder.createEventWithCategory(
"xxxx",
action: "buttonclicked",
label: "you pressed xxx button",
value: nil).build()
tracker.send(builder as! [NSObject : AnyObject])
#2
27
This answer has been updated for Swift 3
这个答案已经更新为Swift 3
let tracker = GAI.sharedInstance().defaultTracker
let eventTracker: NSObject = GAIDictionaryBuilder.createEvent(
withCategory: "SomeCategory",
action: "SomeAction",
label: "SomeLabel",
value: nil).build()
tracker.send(eventTracker as [NSObject : AnyObject]!)
OR...
还是……
let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIDescription, value: "ScreenViewName")
let eventTracker: NSObject = GAIDictionaryBuilder.createScreenView().build()
tracker.send(eventTracker as! [NSObject : AnyObject])
#3
8
For GA 3.17.0 use:
GA 3.17.0使用:
let tracker = GAI.sharedInstance().defaultTracker
let builder = GAIDictionaryBuilder.createEvent(withCategory: category,
action: action,
label: nil,
value: nil).build()
tracker?.send(builder as [NSObject : AnyObject]!)
Note the !
at the end of [NSObject: AnyObject]!
.
注意!在[NSObject: AnyObject]!
#4
0
Small follow up on the amazing answers, in swift 3, the method is
小跟进惊人的答案,在swift 3中,方法是
let builder:NSObject = GAIDictionaryBuilder.createEvent(withCategory: category, action: eventName, label: label, value: nil).build()