1. 程式人生 > >Google Analytics in IOS(二)——實現頁面統計和事件追蹤

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)。

準備開始(真的開始了)

一、需要下載

二、將sdk中的下列檔案拽入你的工程中:


三、GA使用了CoreData andSystemConfiguration框架,所以你需要將CoreData.frameworkSystemConfiguration.framework也新增到你的工程裡

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

To initialize the tracker, import theGAI.hheader in your applicationdelegate.mfile and add this code to your application delegate'sapplication: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上檢視一下對應的資料,這是可以幫助理解的。

 NoteThe 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.

       這樣就可以實現事件追蹤了。