Reputation: 8328
I have a problem with Swift 2 (Swift 3) and Google Analytics.
This is the line with the problem:
tracker.send(GAIDictionaryBuilder.createScreenView().build())
Xcode tell's me:
Cannot invoke 'send' with an argument list of type '(NSMutableDictionary!)'
Upvotes: 36
Views: 9243
Reputation: 65
This is a solution I came up with.. Maybe it could help some of you. It's a struct you need to instantiate in every UIViewController, but it helps with the boilerplate.
import UIKit
struct Analytics {
fileprivate let viewController: UIViewController
fileprivate let tracker = GAI.sharedInstance().defaultTracker
init (forScreen viewController: UIViewController) {
self.viewController = viewController
}
func startTracking () {
let screenView = GAIDictionaryBuilder.createScreenView().build() as NSDictionary
guard
let tracker = tracker,
let build = screenView as? [AnyHashable: Any]
else { return }
tracker.set(kGAIScreenName, value: String(describing: viewController))
tracker.send(build)
}
}
class HomeViewController: UIViewController {
lazy var analytics: Analytics = {
return Analytics(forScreen: self)
}()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear() {
super.viewWillAppear()
analytics.startTracking()
}
}
Upvotes: 0
Reputation: 1174
Update for Swift 3 (2016.10.19)
let tracker = GAI.sharedInstance().defaultTracker
let 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)
Upvotes: 63
Reputation: 1
For swift 3:
let build:NSObject = GAIDictionaryBuilder.createScreenView().build()
tracker?.send(build as! [AnyHashable: Any])
Upvotes: -1
Reputation: 1
let build = GAIDictionaryBuilder.createScreenView().build() as [NSObject : AnyObject]
tracker?.send(build)
Upvotes: -2
Reputation: 578
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.
Upvotes: 5
Reputation: 39181
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])
Upvotes: 10