谷歌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







数据分析技术白皮书上的内容按下列许可协议发布: CC Attribution-Noncommercial 4.0 International

Gordon Choi's Analytics Book