Tom Coomer Tom Coomer - 4 months ago 33
iOS Question

UIButton Event Tracking Google Analytics (Swift)

I would like to track an event when the user taps a UIButton with Google Analytics. I have the code below but I am shown an error. Where am I going wrong? Thanks

Code:

@IBAction func startGame1(sender: AnyObject) {

var tracker = GAI.sharedInstance().defaultTracker

tracker.send(GAIDictionaryBuilder.createEventWithCategory("Game 1", action: "Start Pressed", label: "Start Timer One", value: nil).build())
}


Error:

Cannot invoke 'send' with an argument list of type'(NSMutableDictionarry)'

Answer

Since Swift 1.2 is released, you have to cast GAIDictionaryBuilder as [NSObject : AnyObject]

See this post on StackOverflow

Google Analytics not working with Swift 1.2 and Xcode 6.3

There is the solution for your issue

@IBAction func startGame1(sender: AnyObject) {

        var tracker = GAI.sharedInstance().defaultTracker

        tracker.send(GAIDictionaryBuilder.createEventWithCategory("Game 1", action: "Start Pressed", label: "Start Timer One", value: nil).build() as [NSObject : AnyObject]) 
    }

Hope it helps!

Comments