简体   繁体   中英

Issue with Google Analytics in Swift 2 or 3

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!)'

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)

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.

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()
    }
}

For swift 3:

let build:NSObject = GAIDictionaryBuilder.createScreenView().build()

tracker?.send(build as! [AnyHashable: Any])

let build = GAIDictionaryBuilder.createScreenView().build() as [NSObject : AnyObject]

tracker?.send(build)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM