Using Google Analytics with Swift on iOS Using Google Analytics with Swift on iOS swift swift

Using Google Analytics with Swift on iOS


OK so I just added GAI.h to my bridging header but didn't add others header files.And thanks DPLusV I also didn't translated correctly Obj-C to Swift.

Here is my final code which works:

let tracker = GAI.sharedInstance().defaultTrackertracker.set(kGAIScreenName, value: "/index")tracker.send(GAIDictionaryBuilder.createScreenView().build())

[EDIT] SWIFT 3

let tracker = GAI.sharedInstance().defaultTrackertracker?.set(kGAIScreenName, value: "/index")let build = (GAIDictionaryBuilder.createScreenView().build() as NSDictionary) as! [AnyHashable: Any]tracker?.send(build)


I followed the instructions in

https://developers.google.com/analytics/devguides/collection/ios/v3/?ver=swift

up till pod installation

I already had a GoogleService-Info.plist file there I enabled the google analytics

Then I did some hit and trial methods to make it work

And finally I found some thing which worked for me

let googleAnalytics : GAITracker = GAI.sharedInstance().trackerWithTrackingId("UA-XXXXXXXX-X")GAI.sharedInstance().trackUncaughtExceptions = truegoogleAnalytics.set(kGAIScreenName, value: screenName)let builder = GAIDictionaryBuilder.createScreenView()googleAnalytics.send(builder.build() as [NSObject : AnyObject])

I put this piece of code in View Controller(s) where ever it was needed.

Hope this help someone. Thanks