2016-01-27 57 views
9

我正在關注iOS的Analytics(developers.google.com/analytics/devguides/collection/ios/v3/?ver=swift)指南,並且我在我的Swift代碼項目中出現錯誤我無法修復。 我正在使用XCode 6.4,Swift和iOS部署目標8.1。在ios swift中實現Google Analytics

步驟1

首先,我使用的CocoaPods安裝了谷歌SDK。 這是運行pod install命令後,控制檯結果:

Updating local specs repositories 

CocoaPods 1.0.0.beta.2 is available. 
To update use: `gem install cocoapods --pre` 
[!] This is a test version we'd love you to try. 

For more information see http://blog.cocoapods.org 
and the CHANGELOG for this version http://git.io/BaH8pQ. 

Analyzing dependencies 
Downloading dependencies 
Using Google (1.0.7) 
Using GoogleAnalytics (3.14.0) 
Using GoogleNetworkingUtilities (1.0.0) 
Using GoogleSymbolUtilities (1.0.3) 
Using GoogleUtilities (1.1.0) 
Generating Pods project 
Integrating client project 
Sending stats 
Pod installation complete! There is 1 dependency from the 
Podfile and 5 total pods installed. 

步驟2

然後打開,在引導,我的應用程序的項目.xcworkspace文件說。

我Podfile看起來是這樣的:

# Uncomment this line to define a global platform for your project 
# platform :ios, '8.0' 
# Uncomment this line if you're using Swift 
# use_frameworks! 

target 'XXXXXX' do 

source 'https://github.com/CocoaPods/Specs.git' 
platform :ios, '8.1' 
pod 'Google/Analytics', '~> 1.0.0' 

end 

target 'XXXXXXTests' do 

pod 'Google/Analytics', '~> 1.0.0' 

end 

其中XXXXXX是我的項目的名稱。

步驟3

我得到的配置文件GoogleService-Info.plist並列入我的計劃將所有的目標(2個目標在我的項目)。

步驟4

我通過選擇文件>新建>文件>的iOS>來源>頭文件創建的BridgingHeader通過。 我將它命名爲BridgingHeader.h,它位於我的項目的根目錄中。 內容是:

#ifndef XXXXX_BridgingHeader_h 
#define XXXXX_BridgingHeader_h 

#import "Google/Analytics.h" 
#import <Google/Analytics.h> 
#include "GAI.h" 

#import <CoreData/CoreData.h> 
#import <SystemConfiguration/SystemConfiguration.h> 

#import "Libraries/GoogleAnalytics/GAI.h" 
#import "Libraries/GoogleAnalytics/GAIFields.h" 
#import "Libraries/GoogleAnalytics/GAILogger.h" 
#import "Libraries/GoogleAnalytics/GAITracker.h" 
#import "Libraries/GoogleAnalytics/GAIDictionaryBuilder.h" 

#endif 

其中 「XXXXX」 是我的項目的名字。

步驟5

現在的問題: 我想包括/導入谷歌分析了我的AppDelegate.swift,但我不能。這是錯誤:

AppDelegate.swift import Google Analytics

我也試過import "Google/Analytics.h"但出現另一個錯誤:Expected identifier in import declaration

  • 我怎樣才能解決這個如此的XCode不給我的錯誤?
  • BridgingHeader是否錯誤?我是否必須以某種方式指出它的內部標題?
  • 我是否必須爲我現在缺少的Google Analytics配置其他內容?

非常感謝。

回答

29

對於使用CocoaPods的Google Analytics進行實施有兩種選擇。

  1. 莢 '谷歌/分析'
  2. 莢 'Google分析'

它們之間存在着利弊。

莢 '谷歌/分析'

  • 需要使用Google配置文件(GoogleService-Info.plist中)
  • 簡單橋接頭文件。只需在橋接頭文件中添加#import <Google/Analytics.h>即可。
  • 在您要實施Google Analytics(分析)的每個文件中添加import Google

莢 'Google分析'

  • 沒有谷歌配置文件(GoogleService-Info.plist中)
  • 更復雜的橋接頭文件。

我喜歡用莢「Google分析」,但我會解釋如何解決這個問題,使用莢「谷歌/分析」 因爲谷歌的官方網站建議莢「谷歌/分析」。

  1. 橋接報

你只需要谷歌分析的一行代碼。

#import <Google/Analytics.h> 

不要忘記爲Objective-C-Bridging-Header設置目標生成設置。 您必須提供正確的路徑才能啓用Objective-C-Bridging-Header。

設置目標 - 建造設置目標-C-橋接報頭 $(SRCROOT)/ $(PRODUCT_NAME)/projectName_Bridging_Header.h

  • AppDelegate.swift
  • import Google 
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) 
    -> Bool { self.setupGoogleAnalytics() 
    .. 
        self.setupGoogleAnalytics() 
    .. 
    } 
    
    func setupGoogleAnalytics() { 
    
        // Configure tracker from GoogleService-Info.plist. 
        let configureError:NSError? 
        GGLContext.sharedInstance().configureWithError(&configureError) 
        assert(configureError == nil, "Error configuring Google services: \(configureError)") 
    
        let gai = GAI.sharedInstance() 
        gai.trackUncaughtExceptions = true // report uncaught exceptions 
        gai.logger.logLevel = GAILogLevel.Verbose // remove before app release 
    } 
    
  • SomeViewController.swift
  • override func viewWillAppear(animated: Bool) { 
        super.viewWillAppear(true) 
    
        if let default_tracker = GAI.sharedInstance().defaultTracker { 
         #if DEBUG 
    
          print("default tracker") 
    
         #endif 
        } 
    
        //  let tracker = GAI.sharedInstance().defaultTracker 
        let tracker = GAI.sharedInstance().trackerWithTrackingId("tracking_ID") 
        tracker.set(kGAIScreenName, value: screenName) 
        let builder = GAIDictionaryBuilder.createScreenView() 
        tracker.send(builder.build() as [NSObject : AnyObject]) 
    
    } 
    

    爲什麼我會使用trackerWithTrackingId而不是defaultTracker屬性?

    致命錯誤:如果你使用defaultTracker你可以得到一個錯誤意外發現零而展開的可選值

    defaultTracker屬性的初始值是零,但 trackerWithTrackingId方法被調用後,將被設置。但它有時並不完美。爲了避免這個問題,我建議直接使用trackerWithTrackingId方法。

    我使用pod'GoogleAnalytics'製作sample project。你可以從中得到一個想法。 祝你好運。

    測試信封

    Google分析3.14

    的Xcode 7.2.1

    +0

    我想在我的框架目標中使用GoogleAnalytics。問題是框​​架目標不支持橋接頭,並且上面的兩個pod都不適用於我。你有任何建議,使其工作? – user1615898

    +1

    @ user1615898,我認爲你有兩個選擇:1)使用Firebase分析。它不需要創建橋接頭。 2)您可以使用Google Analytics服務SDK 手動設置Google Analytics。 更多的信息和說明在這裏: https://developers.google.com/analytics/devguides/collection/ios/v3/sdk-download –

    7

    我面臨同樣的問題。由於Xcode生成錯誤,我無法導入「Google/Analytics.h」標題。因爲Google官方頁面中提到的「GoogleAnalytics sdk」中沒有「Google/Analytics.h」標頭。

    所以,我只是用下面一行

    #import "GAI.h" 
    

    希望它會工作得很好。 環境 的Xcode 8.2 iOS版:10.2

    +0

    我也無法導入GoogleAnalytics工作,所以用導入GoogleToolboxForMac –

    7

    Podfile

    pod 'Google/Analytics' 
    

    YourFantasticProjectName-Bridging-Header.h

    #import "Google/Analytics.h" 
    

    你不需要這個

    GGLContext.sharedInstance().configureWithError(&configureError) 
    

    你需要有一個適當的tracker

    let gai = GAI.sharedInstance() 
    let tracker = gai.tracker(withTrackingId: "UA-12345678-1") 
    

    爲了實時取景在GA的儀表板工作,則必須使用GAIDictionaryBuilder和正確的密鑰kGAIScreenName

    tracker.set(kGAIScreenName, value: "this is my screen") 
    let event = GAIDictionaryBuilder.createScreenView() 
    tracker?.send(event!.build() as! [NSObject: Any]) 
    

    在同樣跟蹤屏,跟蹤事件,您需要使用GAIDictionaryBuilder,因爲它會創建帶有正確GA鍵的字典,並且GA喜歡正確的鍵

    let event = GAIDictionaryBuilder.createEvent(withCategory: "category", action: "action", label: "level", value: NSNumber(value: 120)) 
    tracker?.send(event!.build() as! [NSObject: Any]) 
    

    似乎在模擬太

    2

    爲SWIFT 3工作:

    var configureError:NSError? = nil 
        GGLContext.sharedInstance().configureWithError(&configureError) 
        assert(configureError == nil, "Error configuring Google services: \(configureError)") 
        let gai = GAI.sharedInstance() 
        gai?.trackUncaughtExceptions = true 
        gai?.logger.logLevel = GAILogLevel.verbose 
    
    0

    我覺得這是更好地發送錯誤crashlytics,而是繼續執行的應用程序:

    func configureGoogleAnalytics() { 
        var configureError: NSError? = nil 
        GGLContext.sharedInstance().configureWithError(&configureError) 
        if configureError != nil { 
         Crashlytics.sharedInstance().recordError(configureError!) 
        } 
    } 
    

    還要檢查this答案是添加分析的最新方式。

    相關問題