2017-11-03 10 views
1

ループの機能はまったく異なり、Googleアナリティクスパッケージのためにループの機能が異なるかどうかはわかりません。Googleアナリティクスのオーガニック検索ループの問題

印刷結果が正しく出力されないことを示すprint文出力の違いはありません。

library(googleAnalyticsR) 
library(tidyverse) 

#settings 
start_dat <- as.character(Sys.Date()-31) 
end_dat <- as.character(Sys.Date()-1) 


#Authorize Google Analytics R- this will open a webpage 
#You must be logged into your Google Analytics account on your web browser 
ga_auth() 

account_sum <- ga_account_list() 



#Add the start and end date to the date frame, as well as some columns to use to populate the metrics 
account_sum$start_dat <- start_dat 
account_sum$end_dat <- end_dat 

## choose the v3 segment 
segment_for_call <- "gaid::-5" 

## make the v3 segment object in the v4 segment object: 
seg_ob <- segment_ga4("OrganicTraffic", segment_id = segment_for_call) 


# cycle through the list of views, pull the data, and add it to the 
#account_summary 

for (i in 1:5){ 

    view_id <- (Book1CSV[[1]][i]) 
    views=view_id 




    ga_dat <- google_analytics_4(views, 
          date_range = c(start_dat, end_dat), 
          segments = seg_ob, 
          metrics = c("sessions", "pageviews"), 
          dimensions = c("year","segment")) 

    ga_dat <- summarise(ga_dat, 
         sessions = sum(sessions), 
         pageviews = sum(pageviews)) 

    account_sum$sessions[i] <- ga_data$sessions 
    account_sum$pageviews[i] <- ga_data$pageviews 
    print(account_summary) 
} 

clean_sum <- select(account_sum, 
         ID = webPropertyId , 
         Account = accountName, 
         Views = views, 
         Type = type, 
         Level = level, 
         'Start Date' = start_dat, 
         'End Date' = end_dat, 
         Sessions = sessions, 
         Pageviews = pageviews) 



write.csv (ga_dat, "doesntwork.csv", row.names = TRUE) 

THISワークス!!!!!!!!!!!!!!!印刷ステートメントでコードを印刷

library(googleAnalyticsR) 
library(tidyverse) 

#settings 
start_date <- as.character(Sys.Date()-31) 
end_date <- as.character(Sys.Date()-1) 
metrics <- c("sessions", "pageviews") 
dimensions <- "year" 

#Authorize Google Analytics R- this will open a webpage 
#You must be logged into your Google Analytics account on your web browser 
ga_auth() 

account_summary <- ga_account_list() 



#Add the start and end date to the date frame, as well as some columns to use to populate the metrics 
account_summary$start_date <- start_date 
account_summary$end_date <- end_date 


# cycle through the list of views, pull the data, and add it to the 
#account_summary 

for (i in 1:6){ 

    view_id <- (Book1CSV[[1]][i]) 

    ga_data <- google_analytics_4(viewId = view_id, 
           date_range = c(start_date,end_date), 
           metrics = metrics, 
           dimensions = dimensions) 

    # This query might return multiple rows (if it spans a year boundary), so 
    #collapse and clean up 

    ga_data <- summarise(ga_data, 
         sessions = sum(sessions), 
         pageviews = sum(pageviews)) 

    #add the totals to the account summary 

    account_summary$sessions[i] <- ga_data$sessions 
    account_summary$pageviews[i] <- ga_data$pageviews 
    print(account_summary) 
} 

# Make a more compact set of data 

clean_summary <- select(account_summary, 
         Account = accountName, 
         View = viewId, 
         Type = type, 
         Level = level, 
         'Start Date' = start_date, 
         'End Date' = end_date, 
         Sessions = sessions, 
         Pageviews = pageviews, 
         ID = webPropertyId) 
select 

write.csv (clean_summary, "worksfine.csv", row.names = FALSE) 

ここで何が間違っているのか分かりません。いくつかの詳細なヘルプをしたいと思います。私はBook1CSVというファイルをアップロードしましたが、エラーをキャッチするためのtry-catchステートメントを取得できませんでした。 Googleの販売店のベータアカウントでクラッシュが発生していました。

+0

だから、「Googleの商人ストアのベータアカウントはクラッシュを引き起こしました。」ん問題が解決したことを意味しますか? –

+0

まだクラッシュを引き起こしただけで、すべてのアカウントがダウンロードされ、ベータアカウントが削除され、Book1CSVのリストにアップロードされました。動作するtry catchステートメントを取得できませんでした。問題は現在、複数のオーガニック検索を引き出しています。それがループで印刷物に間違って何かを実行するとき。ボトムコードではうまく動作しますが、印刷でもトップではありません:/ – hellogoodbye

答えて

0

これはループ内のオブジェクトを修正する反Rパターンです。データのリストを返す方が良いです。最後にマージして要約を作成してください。

だから、のようなもの:

my_view_ids <- c(12345,233445,232434) 

## fetch data, create a list of 
all_data <- lapply(my_view_ids, function(id){ 
    one_data <- tryCatch(google_analytics_4(viewId = id, 
       date_range = c(start_date,end_date), 
       metrics = metrics, 
       dimensions = dimensions), 
       error = function(ex){message(ex); NULL}) 
    one_data$viewId <- id 
    one_data 
}) 

## Reduce the list of data.frames to one data.frame 
all_dataframe <- Reduce(rbind, all_data) 

### make your summary etc. 
+0

これで、私はそれが欲しいと思っています!私はあなたのコメントをupvoteことができますが、十分に高いハハにランクされていないことを願います。オブジェクトを変更するときにforループが動作しないことを知っておくと、とても便利です。再度ありがとう:) – hellogoodbye

+0

あなたは私の新しい質問を見ることができますかhttps://stackoverflow.com/questions/47404867/calling-ga-goal-from-the-googleanalyticsr-package – hellogoodbye

+0

確かめてください1? – MarkeD