2017-11-03 1 views
1

Die Schleife funktioniert völlig anders und ich bin mir nicht sicher, ob es wegen der Google Analytics-Paket ist, weil sehr wenig zwischen den Code ist.Google Analytics organische Suchschleife Probleme

funktioniert nicht Unterschied in print-Anweisung Ausgabe zeigt Ergebnisse nicht richtig herauskommen.

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) 

DIESE ARBEITEN !!!!!!!!!!!!!!! Druckanweisung druckt Code

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) 

Kann nicht wirklich verstehen, was hier falsch geht. Möchte eine detaillierte Hilfe. Ich habe eine Datei mit dem Namen Book1CSV hochgeladen, da ich keine try-catch-Anweisung erhalten konnte, die den Fehler abfangen würde. Google Merchant Store Beta-Konto verursachte den Absturz.

+0

So funktioniert "Google Merchant Shop-Beta-Konto den Absturz verursacht wurde" bedeutet das Problem ist gelöst? –

+0

Noch immer verursacht der Absturz alle Konten heruntergeladen und das Beta-Konto entfernt und sie in eine Liste in Book1CSV hochgeladen. Konnte nicht versuchen, Catch-Anweisung zu arbeiten. Das Problem besteht nun darin, mehrere organische Suchen durchzuführen. Wenn es läuft, stimmt etwas nicht mit dem Druck in der Schleife. Funktioniert gut für den unteren Code, aber nicht für top, auch wenn sie das gleiche ausdrucken:/ – hellogoodbye

Antwort

0

Dies ist ein Anti-R-Muster, das ein Objekt in einer Schleife verändert. Es ist besser, eine Liste von data.frames zurückzugeben, dann am Ende zusammenzuführen und Ihre Zusammenfassung zu erstellen.

So etwas wie:

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

Es funktioniert jetzt genau, wie ich es auch will !! Ich wünschte, ich könnte deinen Kommentar verbessern, aber nicht hoch genug, haha. Habe noch nicht in R codiert, also ist es wirklich praktisch zu wissen, dass for-Schleifen beim Modifizieren des Objekts nicht funktionieren. Danke nochmal :) – hellogoodbye

+0

Hey kannst du dir meine neue Frage ansehen https://stackoverflow.com/questions/47404867/calling-ga-goal-from-the-googleanalyticsr-package – hellogoodbye

+0

Sicher - kannst du die Antwort dafür akzeptieren ein? – MarkeD

Verwandte Themen