I'm trying to set up Google Analytics in my iOS app by following this guide Google Analytics for iOS I have completed all the steps but when I run my app it crashes and says that my defaultTracker is nil. This is the following code in my ViewController:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
var tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIScreenName, value: nil)
var builder = GAIDictionaryBuilder.createScreenView()
tracker.send(builder.build() as [NSObject : AnyObject])
}
In my AppDelegate I have this code that should initialize everything regarding the tracker:
func applicationDidFinishLaunching(application: UIApplication) {
// Configure tracker from GoogleService-Info.plist.
var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
// Optional: configure GAI options.
var gai = GAI.sharedInstance()
gai.trackUncaughtExceptions = true // report uncaught exceptions
gai.logger.logLevel = GAILogLevel.Verbose // remove before app release
}
Does anyone know what the problem might be and how to solve it?
Thanks in advance Vatan
I had same issue, fixed with adding default tracker initialization at AppDelegate
applicationDidFinishLaunching
method like below;
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//...
var gai = GAI.sharedInstance()
gai.trackUncaughtExceptions = true // report uncaught exceptions
gai.defaultTracker = gai.trackerWithTrackingId("UA-XXXXX-X")
//...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With