2017-10-04 138 views
0

我正在使用googleanalyticsR從Google Analytics下載所有數據我的目標是構建一個小數據框來分析。Loop googleanalyticsR - 錯誤if(nrow(out)<all_rows {:參數長度爲零

要下載的所有數據我創建了一個循環:

for (i in 1:length(metricsarray)) { 
    print(paste(i)) 
    tryCatch( google_analytics_4(my_id, 
           date_range = c(start_date, end_date), 
           metrics = metricsarray[i], 
           dimensions = c('transactionId'), 
           max = -1)) %>% 
    assign(gsub(" ", "", paste("metricsarray",i, sep="")), ., inherits = TRUE) 
} 

循環從1到11,沒有任何問題,即打印我的號碼,給我的留言:

Downloaded [3537] rows from a total of [3537]

但是,當它達到I =在metricsarray 12 I得到這個錯誤[I]:

2017-10-04 10:37:56> Downloaded [0] rows from a total of []. Error in if (nrow(out) < all_rows) { : argument is of length zero

我用tryCatch,但沒有效果,它繼續。我的目標是它會繼續測試每個指標,直到最後。 此外,還繼續當它發現錯誤:

JSON fetch error: Selected dimensions and metrics cannot be queried together.

我是新來的R中使用Google分析API,隨時提出解決方案,文章或任何東西,我們可能會認爲它會幫助我獲得更多這方面的知識。 謝謝

+0

tryCatch應該爲你的用例工作,反而會發生什麼?我做類似的,它的工作正常。如果更容易,請隨時在GitHub上提出問題。 – MarkeD

回答

0

JSON fetch error: Selected dimensions and metrics cannot be queried together.

並非所有Google分析維度和指標都可以一起查詢。造成這種情況的主要原因是數據不存在或者數據無法識別。

測試可以一起查詢元數據的最佳方法是檢查dimensions and metrics reference。無效的項目將變灰。

+0

謝謝你的回答。 我知道這一點,但我想下載所有可能的數據組合,並且當循環從服務器響應時忽略該錯誤。 但是比這更重要的是,當循環發現沒有數據時('if(nrow(out) Cads