Issue with Google Analytics in Swift 2 or 3 Issue with Google Analytics in Swift 2 or 3 ios ios

Issue with Google Analytics in Swift 2 or 3


Update for Swift 3 (2016.10.19)

let tracker = GAI.sharedInstance().defaultTrackerlet build = (GAIDictionaryBuilder.createScreenView().build() as NSDictionary) as! [AnyHashable: Any]tracker?.send(build)

Still an ugly approach, let me know if there's an cleaner conversion.


Original

Same here, struggling to resolve tons of errors.

What I did (deprecated):

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

Edit (2015)

Thanks to @George Poulos. . Recently they updated the options, now createAppView is deprecated, should use createScreenView instead.

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


In addition to the accepted answer:

Changed this:

tracker.send(GAIDictionaryBuilder.createEventWithCategory("UX", action: "User sign in", label: nil, value: nil).build())

To this:

tracker.send(GAIDictionaryBuilder.createEventWithCategory("UX", action: "User sign in", label: nil, value: nil).build()  as [NSObject : AnyObject])


This might be a bit of an overkill, but I prefer creating a short extension and not need to type the castings every time:

In any swift file, paste the following code:

extension GAIDictionaryBuilder{    func buildSwiftCompatible() -> [NSObject:AnyObject]    {        return self.build() as [NSObject:AnyObject]    }}

Then you can call buildSwiftCompatible() instead of the usual build():

tracker.send(GAIDictionaryBuilder.createScreenView().buildSwiftCompatible())

Have fun.