无法转换类型的价值' NSMutableDictionary'输入' [NSObject:AnyObject]'在强制谷歌ios Analytics

时间:2015-11-06 07:26:04

标签: ios iphone swift google-analytics swift3

我想将google Analytics ios添加到我的项目中,但是当我尝试将代码粘贴到我的应用中时遇到了一些问题,如下面的代码。

var tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIScreenName, value: "rootPlayView")

var builder = GAIDictionaryBuilder.createScreenView()
tracker.send(builder.build() as [NSObject : AnyObject])

然后我收到了这样的错误

enter image description here

我该如何解决这个问题?

谢谢!

4 个答案:

答案 0 :(得分:45)

输入转化

enter image description here

<强> Swift3

屏幕追踪

myFile

<强>目标C

 let tracker = GAI.sharedInstance().defaultTracker
 tracker.set(kGAIScreenName, value: "Home")
 tracker.send(GAIDictionaryBuilder.createScreenView().build() as [AnyHashable : Any])

自动屏幕跟踪

<强> Swift3

 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]]; 

<强>目标C

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self!.screenName = "About Screen"
}

事件跟踪

<强> Swift3

- (void)viewDidAppear:(BOOL)animated {
 [super viewDidAppear:animated];
  self.screenName = @"About Screen";
}

<强>目标C

 let tracker = GAI.sharedInstance().defaultTracker
 tracker!.send(GAIDictionaryBuilder.createEventWithCategory("ui_action", action: "button_press", label: "menuButton", play: nil).build())

或使用

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]];

有关详细信息,请参阅this

例如

试试这个

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)
屏幕跟踪

var builder = GAIDictionaryBuilder.createScreenView().build() as! [NSObject : AnyObject]
tracker.send(builder)

for event Track

let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIDescription, value: "rootPlayView")
let builder: NSObject = GAIDictionaryBuilder.createScreenView().build()
tracker.send(builder as! [NSObject : AnyObject])

答案 1 :(得分:27)

此答案已针对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])

答案 2 :(得分:8)

对于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]!)

请注意!末尾的[NSObject: AnyObject]!

答案 3 :(得分:0)

对于惊人的答案的小跟进,在快速3中,方法是

let builder:NSObject = GAIDictionaryBuilder.createEvent(withCategory: category, action: eventName, label: label, value: nil).build()