您的位置:首页 > 移动开发 > IOS开发

Google Analytics in IOS(二)——实现页面统计和事件追踪

2013-03-29 11:22 555 查看
      现在我们来学习一下如何实现上一篇中提到的页面统计和事件追踪,上一篇:Google Analytics
in IOS(一)。

      在开始之前你需要确保以下四项:

      iOS Developer SDK 4.0 or later;

      Google Analytics for Mobile Apps iOS SDK v2;

      An iOS app that you want to measure using Google Analytics;

      A new Google Analytics app property and profile.

      第一项貌似都差不多是ios5.0以后了;

      第二项sdk的链接在下方“准备开始”的第二项中;

      第三项和第四项可以同时完成,需要填写你需要使用GA的app的信息,并且获得一个trakerId,具体步骤如下:

     1.登录你的GA账户(谷歌邮箱登录),如果是第一次登录,右上角会有一个注册GA的按钮,点击按钮;

     2.之后如下所示:





       当然选择“应用”了,应用名称“SammyDress”(这是我的应用名),行业类别(你做什么的就填什么),账户名称,这个我后来发现其实是可以多个的,每个账户下又可以有多个应用,所以你觉得怎么好分类就写什么名称。最后,点击"获取跟踪ID".



        这个"UA-XXXXXX-1"就是追踪ID(trakerId)了,下面会用到的(这个界面也可以下载GA ios sdk)。

       准备开始(真的开始了)

一、需要下载Google Analytics IOS SDK(下载链接:Google
Analytics iOS SDK)

二、将sdk中的下列文件拽入你的工程中:



三、GA使用了
CoreData
 and
SystemConfiguration框架,所以你需要将CoreData.framework和SystemConfiguration.framework也添加到你的工程里。


四、Initialize
the 
tracker(初始化追踪者)

    To initialize the tracker, import the
GAI.h
header in your
applicationdelegate
.m
file and add this code to your application delegate's
application:didFinishLaunchingWithOptions:
 method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Optional: automatically send uncaught exceptions to Google Analytics.
[GAI sharedInstance].trackUncaughtExceptions = YES;
// Optional: set Google Analytics dispatch interval to e.g. 20 seconds.
[GAI sharedInstance].dispatchInterval = 20;
// Optional: set debug to YES for extra debugging information.
[GAI sharedInstance].debug = YES;
// Create tracker instance.
id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"UA-YOUR-TRACKING-ID"];
}
       当你通过trackerID获得一个traker(追踪者),这个traker就驻留在了资源库(library)中,再次获取可以使用下面的方法:

id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];


       做完以上这些,你就基本做好准备工作了,下面我们来实现屏幕跟踪,事件追踪,电子商务分析等数据交互。

1.Add screen measurement(屏幕测量,感觉翻译怪怪的,可以理解为跟踪当前在哪个视图)

      To automatically measure views in your app, have your view controllers extend 
GAITrackedViewController
,
a convenience class that extends 
UIViewController
, and provide the view name to give
to each view controller in your reports. Each time that view is loaded, a screen view will be sent to Google Analytics.

 
    For example, suppose you have an “About” view that you want to measure with a view controller header that looks like this:
@interface AboutViewController : UIViewController
      You would update this header to say:

#import "GAITrackedViewController.h"
@interface AboutViewController : GAITrackedViewController
      You must also provide the view name to be used in your Google Analytics reports. A good place to put this
is the view controller's initializer method, if you have one, or the 
viewDidLoad
 method:

- (void)viewDidLoad
{
[super viewDidLoad];
self.trackedViewName = @"About Screen";
}
      这上面所用的方法是自动跟踪视图。还有一个手动的方法:

[[[GAI sharedInstance] defaultTracker] sendView:@"About"];


到这里基本实现了页面统计分析,去GA页面查看一下,你会发现它告诉你你去过“About”界面了。

2.事件追踪

      通过前面的例子,相信大家已经了解到事件追踪的作用了。现在来讲一下如何实现,下面是实现的方法以及参数说明:
/*!
Track an event.

If [GAI optOut] is true, this will not generate any tracking information.

@param category The event category, or `nil` if none.

@param action The event action, or `nil` if none.

@param label The event label, or `nil` if none.

@param value The event value, to be interpreted as a 64-bit signed integer, or
`nil` if none.

@return `YES` if the tracking information was queued for dispatch, or `NO` if
there was an error (e.g. the tracker was closed).
*/
- (BOOL)sendEventWithCategory:(NSString *)category
withAction:(NSString *)action
withLabel:(NSString *)label
withValue:(NSNumber *)value;
        注意下第四个参数的类型:
NSNumber
 (Optional)
Value, interpreted as 64-bit integer
       下面是一个官网的例子,以帮助理解:

[tracker sendEventWithCategory:@"uiAction"
withAction:@"buttonPress"
withLabel:buttonName
withValue:[NSNumber numberWithInt:100]];
        如果你仍然不能理解,没关系,填上一些值,发送到GA,然后去GA上查看一下对应的数据,这是可以帮助理解的。

       Note: The
Google Android SDK for iOS may throttle events, as well as other hits, if a large number of send calls are made in a short period of time.

       这样就可以实现事件追踪了。

下一篇我们来了解GA中的电子商务追踪:Google
Analytics in IOS(三)—— 电子商务追踪
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: