Varis Darasirikul Varis Darasirikul - 6 months ago 52
iOS Question

Cannot convert value of type 'NSMutableDictionary' to type '[NSObject: AnyObject]' in coercion for google ios Analytics

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.

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

enter image description here

how can i fix this?

Thanks!

Answer

try this

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

example

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