Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

defaultTracker is nil in Google Analytics for IOS

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

like image 295
vato Avatar asked Jul 30 '15 08:07

vato


1 Answers

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")
    //...
 }
like image 63
mert Avatar answered Nov 08 '22 14:11

mert