这是官方文档的,应该算译文了,如有错误,欢迎指正。
GA IOS SDK使用两个类来管理实施和发送数据到GA服务器的全局声明。
GAI
– a singleton that handles the global state of your implementation, including getting newGAITracker
objects, as well as your app-level opt-out setting and dispatching settings.GAITracker
– the class from which you send data to Google Analytics. Multiple trackers can be instantiated, one per unique property ID.下面的例子中,通过使用两个跟踪的屏幕视图被发送到两个单独的属性,每个追踪者有唯一的ID。
#import "RootViewController.h" #import "GAI.h" @interface RootViewController () @end @implementation RootViewController { - (void)viewDidLoad { [super viewDidLoad]; // Send a screen view to the first property. id tracker1 = [[GAI sharedInstance] trackerWithTrackingId:@"UA-XXXX-Y"]; [tracker1 sendView:@"/HomeScreen"]; // Send another screen view to the second property. id tracker2 = [[GAI sharedInstance] trackerWithTrackingId:@"UA-XXXX-Z"]; [tracker2 sendView:@"Home"]; } @end记住自动追踪功能比如页面自动统计和异常捕获,只能通过一个追踪者来发送数据到GA.如果你要使用自动追踪功
能,并且想要使用其他追踪者,你就必须要使用手动。
仅供参考:自动屏幕测量使用GAITrackedViewController中指定的tracker属性。未捕获的异常测量使用在GAI
实例中默认的指定追踪者。
Default Tracker
虽然实现可能有多个追踪者,但是仍有一个默认的追踪者(default tracker)。The first Tracker
retrieved becomes the default tracker.
可以用下面的方法获得默认追踪者:
// Get default tracker. id myDefault = [GAI sharedInstance].defaultTracker;用下面的方法来设置默认追踪者:
// Get a new tracker. id newTracker = [[GAI sharedInstance]trackerWithTrackingId:@"UA-NEW-TRACKING-ID"); // Set the new tracker as the default tracker, globally. [GAI sharedInstance].defaultTracker = newTracker;
使用采样来确保报告不被中断。
例如,执行客户端的在50%的速率取样:
// Set a sample rate of 50%. [tracker setSampleRate:50.0]; // Sample rate is a double.为了避免报告不一致,您的每一个应用程序配置文件中采集的数据应该包含相同的采样率。如果您不同版本的应
用程序使用不同的采样率,你需要使用app version dimension配置文件过滤,来保持你app每个版本数据的独立。
这个可能有点不好理解,我把GA ios 库中关于sampling的注释搬过来一下:
The sampleRate parameter controls the probability that the visitor will be sampled. By default, sampleRate is 100, which signifies no sampling. sampleRate may be set to any value between 0 and 100, inclusive. A value of 90 means 90% of visitors should be sampled (10% of visitors to be sampled out). When a visitor is not sampled, no data is collected by Google Analytics for iOS library about that visitor's activity. If your application is subject to heavy traffic spikes, you may wish to adjust the sample rate to ensure uninterrupted report tracking. Sampling in Google Analytics occurs consistently across unique visitors, ensuring integrity in trending and reporting even when sampling is enabled, because unique visitors remain included or excluded from the sample, as set from the initiation of sampling.
获取标志的代码如下:
// Get the app-level opt out preference. if ([GAI sharedInstance].optOut) { ... // Alert the user they have opted out. }设置标志的代码如下:
// Set the app-level opt out preference. [[GAI sharedInstance].setOptOut = YES];
// Enable debug mode. [GAI sharedInstance].debug = YES;
下一篇我们来学习将捕获和未捕获的异常发送到谷歌分析:Google Analytics in IOS(五)—— 崩溃和异常