谷歌APP分析iOS SDK


第16章

iOS

我們來將谷歌分析SDK安裝到iOS App中。

.xcworkspace

這個iOS SDK部署的假設是你app的應用程序源代碼是用Objective-C編寫的,而且你會通過CocoaPods安裝和管理關聯文件。

打開一個終端窗口並導航到應用程序的Xcode項目的位置。如果它不存在,創建一個新的Podfile文件。

pod init

打開Podfile並添加如下:

pod 'Google/Analytics'

保存Podfile文件並且運行:

pod install

在你的應用裡,一個xcworkspace文件會被生成。

配置文件

下載配置文件。

https://developers.google.com/mobile/add?platform=ios&cntapi=analytics&cnturl=https:%2F%2Fdevelopers.google.com%2Fanalytics%2Fdevguides%2Fcollection%2Fios %2Fv3%2Fapp%3Fconfigured%3Dtrue%23add%2Dconfig&cntlbl=Continue%20Adding%20Analytics

拖動googleservice-info.plist文件(如配置文件)到你的Xcode項目的根目錄並將其添加到針對所有的目標對象。

AppDelegate.m

添加如下代碼到AppDelegate.m文件:

#import <Google/Analytics.h>

通過覆蓋(override)didFinishLaunchingWithOptions方法去配置GGLcontext:

NSError *configureError;
[[GGLContext sharedInstance] configureWithError:&configureError];
NSAssert(!configureError, @"Error configuring Google services: %@", configureError);

GAI *gai = [GAI sharedInstance];
gai.trackUncaughtExceptions = YES; // report uncaught exceptions
gai.logger.logLevel = kGAILogLevelVerbose; // remove before app release

ViewController.m

在View Controller裡添加跟踪代碼去跟踪“屏幕數”。

在文件ViewController.m中,添加上<Google/Analytics.h>文件頭。使用viewwillappear方法或函數覆蓋去插入屏幕跟踪。給屏幕一個名稱和執行跟踪。

id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker set:kGAIScreenName value:name];
[tracker send:[[GAIDictionaryBuilder createScreenView] build]];


數據分析技術白皮書在2016年11月正式出版。

英文版:Google Mobile App Analytics iOS SDK – 簡體中文版:谷歌APP分析iOS SDK







Analytics Book繁體中文版上的內容按下列許可協議發布: CC Attribution-Noncommercial 4.0 International

Gordon Choi's Analytics Book