在Google Analytics for IOS中,defaultTracker为nil

时间:2015-07-30 08:21:23

标签: ios swift google-analytics

我按照本指南Google Analytics for iOS尝试在我的iOS应用中设置Google Analytics 我已经完成了所有步骤,但是当我运行我的应用程序时,它会崩溃,并说我的defaultTracker是零。这是我的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])

}

在我的AppDelegate中,我有这个代码,应该初始化有关跟踪器的所有内容:

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
}

有谁知道问题可能是什么以及如何解决?

提前致谢 瓦当

2 个答案:

答案 0 :(得分:7)

我遇到了同样的问题,修正了在AppDelegate applicationDidFinishLaunching方法下添加默认跟踪器初始化,如下所示;

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")
    //...
 }

答案 1 :(得分:0)

我认为您需要在定义defaultTracker变量后在gai中设置applicationDidFinishLaunchinggai变量。