Google Analytics(分析)初始化后,viewController.title为零

我遵循Google分析文档将其添加到我的快速应用程序

我已经做了所有那些在那里说,但我的代码在运行时失败。

这是我在AppDelegate中的代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool { loadGoogleAnalytics() chooseAndLuanchStoryboard() initMembersAfterViewControllerInit() return true } func loadGoogleAnalytics() { // 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. let gai = GAI.sharedInstance() gai.trackUncaughtExceptions = true // report uncaught exceptions gai.logger.logLevel = GAILogLevel.Error } 

这是我在ViewController中的代码:

 override public func viewWillAppear(animated: Bool) { super.viewWillAppear(true) let name = "Pattern~\(self.title!)" // The UA-XXXXX-Y tracker ID is loaded automatically from the // GoogleService-Info.plist by the `GGLContext` in the AppDelegate. // If you're copying this to an app just using Analytics, you'll // need to configure your tracking ID here. // [START screen_view_hit_swift] let tracker = GAI.sharedInstance().defaultTracker tracker.set(kGAIScreenName, value: name) let builder = GAIDictionaryBuilder.createScreenView() tracker.send(builder.build() as [NSObject : AnyObject]) // [END screen_view_hit_swift] } override public func viewWillDisappear(animated: Bool) { keyboardManager.deregisterFromKeyboardNotifications() stopRefreshTimer() } 

我得到这个运行时错误,因为let name = "Pattern~\(self.title!)"是零

 2016-02-05 19:44:44.377 Inline[95803:3750273] The configuration file 'GoogleService-Info.plist' is for another bundle identifier ('com.Inline.inline-ios'). Using this file the services may not be configured correctly. To continue with this configuration file, you may change your app's bundle identifier to 'com.Inline.inline-ios'. Or you can download a new configuration file that matches your bundle identifier from https://developers.google.com/mobile/add and replace the current one. 2016-02-05 19:44:44.383 Inline[95803:] <GMR/INFO> App measurement v.1302000 started 2016-02-05 19:44:44.384 Inline[95803:] <GMR/INFO> To enable debug logging set the following application argument: -GMRDebugEnabled (see http://goo.gl/Y0Yjwu) 2016-02-05 19:44:45.999 Inline[95803:] <GMR/INFO> App measurement enabled 2016-02-05 19:44:46.034 Inline[95803:3750273] Unknown class logo in Interface Builder file. 2016-02-05 19:44:46.048 Inline[95803:3750273] Failed to set (keyPath) user defined inspected property on (UIView): [<UIView 0x7faf9a079730> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key keyPath. 2016-02-05 19:44:46.048 Inline[95803:3750273] Failed to set (keyPath) user defined inspected property on (UIView): [<UIView 0x7faf9a0729a0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key keyPath. 2016-02-05 19:44:46.048 Inline[95803:3750273] Failed to set (keyPath) user defined inspected property on (UIView): [<UIView 0x7faf9a07f8b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key keyPath. 2016-02-05 19:44:46.051 Inline[95803:3750273] Device ID: (null) 2016-02-05 19:44:46.051 Inline[95803:3750273] RegisterDevice RPC request scanning for beacons fatal error: unexpectedly found nil while unwrapping an Optional value 

我怎样才能使self.title成为非零?

初始化didFinishLaunchingWithOptions

 let gai = GAI.sharedInstance() let id = "UA-ID" gai.trackerWithTrackingId(id) gai.trackUncaughtExceptions = true gai.logger.logLevel = GAILogLevel.Warning gai.defaultTracker.allowIDFACollection = false 

通过viewDidAppear上的setScreenName调用Google Analytics(分析)的简单方法。

 extension UIViewController { func setScreeName(name: String) { self.title = name self.sendScreenView() } func sendScreenView() { let tracker = GAI.sharedInstance().defaultTracker tracker.set(kGAIScreenName, value: self.title) let builder = GAIDictionaryBuilder.createScreenView() tracker.send(builder.build() as [NSObject : AnyObject]) } func trackEvent(category: String, action: String, label: String, value: NSNumber?) { let tracker = GAI.sharedInstance().defaultTracker let trackDictionary = GAIDictionaryBuilder.createEventWithCategory(category, action: action, label: label, value: value) tracker.send(trackDictionary.build() as [NSObject : AnyObject]) } } 

我有一个相同的问题,发现defaultTracker属性造成麻烦。

 override func viewWillAppear(animated: Bool) { super.viewWillAppear(true) // I called trackerWithTrackingId method in AppDelegate, // but defaultTracker value could be nil. // So, you got a error message like this : // fatal error: unexpectedly found nil while unwrapping an Optional value // According to google analytics documents, trackerWithTrackingId has an // initial value with nil, and then will be set with return value // from trackerWithTrackingId method call. // But it doesn't look work well sometimes. if let default_tracker = GAI.sharedInstance().defaultTracker { #if DEBUG print("default tracker") #endif } // Instead of defaultTracker, use trackerWithTrackingId method // it will return a new tracker(=create) or the existing tracker let tracker = GAI.sharedInstance().trackerWithTrackingId("tracking-ID") tracker.set(kGAIScreenName, value: "ViewController") let builder = GAIDictionaryBuilder.createScreenView() tracker.send(builder.build() as [NSObject : AnyObject]) } 

有趣的是,默认跟踪器在AppDelegate中运行良好。

  let tracker = GAI.sharedInstance().trackerWithTrackingId("tracking-ID") GAI.sharedInstance().trackUncaughtExceptions = true; GAI.sharedInstance().dispatchInterval = 20 GAI.sharedInstance().logger.logLevel = .Warning //.Verbose let firstTracker = GAI.sharedInstance().defaultTracker if tracker.isEqual(firstTracker) { #if DEBUG print("same Tracker") #endif } 

我的项目环境

  • Podfile

pod'GoogleAnalytics'

  • 转职Header.h
 #import <GoogleAnalytics/GAI.h> #import <GoogleAnalytics/GAIFields.h> #import <GoogleAnalytics/GAILogger.h> #import <GoogleAnalytics/GAIDictionaryBuilder.h> #import <GoogleAnalytics/GAITrackedViewController.h> #import <GoogleAnalytics/GAITracker.h> 

我使用谷歌分析与CocoaPods,但不能遵循谷歌分析的指示迅速 。 因为我不想在我的项目中添加GoogleService-Info.plist。

使用时

 let name = "Pattern~\(self.title!)" 

对于您的跟踪,请确保您在代码或界面生成器中具有View Controller标题集:

设置视图控制器标题