2012-10-20 20 views
4

我試圖在我的iOS應用程序中安裝Google AnalyticsSDK以跟蹤頁面瀏覽量和事件。按照提供的文檔,我似乎無法跟蹤工作,並想知道任何人都可以幫我嗎?下面的信息可能是相關的 - 我會盡我所能描述。iOS - GANTracker無法發送

  • 我針對iOS 4.3以上的所有設備。
  • 我已經在我的項目,prefix.pch文件中包含GANTracker.h和它建立正常

下面是我appdelegate.m代碼樣本被稱爲在didFinishLaunchingWithOptions方法

/* Google Analytics tracking code */ 

[[GANTracker sharedTracker] setSampleRate:100]; 

[[GANTracker sharedTracker]setDebug:NO]; 

[[GANTracker sharedTracker] setDryRun:NO]; 

[[GANTracker sharedTracker] startTrackerWithAccountID:@"UA-111111-1" 
             dispatchPeriod:kGANDispatchPeriodSec 
              delegate:self]; 
NSLog(@"Dispatch%@", [[GANTracker sharedTracker] dispatch] ? @"ed Successfully": @" Failed"); 

NSError *error; 
if (![[GANTracker sharedTracker] setCustomVariableAtIndex:1 
                name:@"iPhone" 
                value:@"iv1" 
               withError:&error]) 
{ 
    NSLog(@"There was an error setting this custom variable\n Description: %@\n", [error localizedDescription]); 
    NSLog(@"Failure reason: %@\n", [error localizedFailureReason]); 
    NSLog(@"May we suggest: %@\n", [error localizedRecoverySuggestion]); 
} 

if (![[GANTracker sharedTracker] trackEvent:@"Loading" 
            action:@"App Finished Launching" 
             label:@"appDidFinishLaunchingWithOptions" 
             value:-1 
            withError:&error]) 
{ 
    NSLog(@"There was an error in tracking events\n Description: %@\n", [error localizedDescription]); 
    NSLog(@"Failure reason: %@\n", [error localizedFailureReason]); 
    NSLog(@"May we suggest: %@\n", [error localizedRecoverySuggestion]); 
} 

NSString *pageUrlString = [[NSString stringWithFormat:@"http://ios.organisation.tld/appentrypoint"] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
if(![[GANTracker sharedTracker] trackPageview:pageUrlString withError:&error]) 
{ 
    NSLog(@"There was an error in tracking initial app entry\n Description: %@\n", [error localizedDescription]); 
    NSLog(@"Failure reason: %@\n", [error localizedFailureReason]); 
    NSLog(@"May we suggest: %@\n", [error localizedRecoverySuggestion]); 

} 

在我的應用程序視圖控制器我想跟蹤的瀏覽量和我soing所以下面的方法:

- (void)viewWillAppear:(BOOL)animated 
{ 
[super viewWillAppear:animated]; 
NSError *trackError; 
NSString *pageUrlString = [[NSString stringWithFormat:@"/aboutsection"] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
if(![[GANTracker sharedTracker] trackPageview:pageUrlString withError:&trackError]) 
{ 
    //Handle error here 
    NSLog(@"There was an error tracking this pageview\n Description: %@\n", [trackError localizedDescription]); 
    NSLog(@"Failure reason: %@\n", [trackError localizedFailureReason]); 
    NSLog(@"May we suggest: %@\n", [trackError localizedRecoverySuggestion]); 
} 
} 

要回到我的appdelegate.m我也甲肝E以下的委託成立

- (void)trackerDispatchDidComplete:(GANTracker *)tracker eventsDispatched:(NSUInteger)hitsDispatched eventsFailedDispatch:(NSUInteger)hitsFailedDispatch 
{ 
    NSLog(@"Google analytics dispatch\n Succeeded?:\n %i, \n Failed?: %i", hitsDispatched, hitsFailedDispatch); 
} 

此記錄,就像這樣成功?:5失敗?:0,沒有失敗來了有

我也看到了日誌消息...沒有派遣。登錄Google Analytics我看不到任何訪問。查看實時分析也沒有發生任何事情。

我在這裏錯過了什麼嗎?

+2

事實證明,上面的代碼是好的。我犯了使用錯誤版本的SDK的簡單錯誤。 Google Analytics服務提供兩種類型的儀表板。一個用於網站,一個用於應用程序。 SDK的1.4版旨在將頁面瀏覽和事件推送到應該設置爲網站的UA代碼。在我的情況下,它被設置爲一個應用程序,雖然這些請求正在進行,但它們卻被派往任何地方。我能夠通過兩種方式解決問題。 1)創建一個新的儀表板作爲網站,並使用UA代碼或2)使用我現有的UA ID SDK版本2。 – matfin

回答

0

當我試圖此步驟它只工作:

1-在的appdelegate .H類#進口 「GAI.h」

2-在的appdelegate在函數 「didFinishLaunchingWithOptions」 添加此代碼

// 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 = NO; 
    // Create tracker instance. 
    [[GAI sharedInstance] trackerWithTrackingId:"your tracking application id from google analytics"]; 

3-在任何uiviewcontroller類。 ħ#進口 「GAI.h」

4-在.H替換這一部分 「:UIViewController中」 有 「:GAITrackedViewController」 在.M

5-視圖中添加做負載這一行

[[[GAI sharedInstance] defaultTracker] trackView:"your view title"]; 
  • 要小心第5點,必須以這種方式實現,我認爲GA庫在其他方面和文檔中都有問題。我試着搜索了很多關於這個問題的內容,偶然發現它只是以這種方式工作,除了它是在GA屏幕上顯示效果的最快方式之外。

  • 花葯注意:必須在谷歌分析創建您的移動應用,照顧有關應用程序的ID,將其添加正確的IOS應用