I am cyciling through 50 GA accounts and there is one with user permission that I do not have access to the error I am getting is...
Error : API returned: User does not have sufficient permissions for this profile. Error in error_check(out) : API returned: User does not have sufficient permissions for this profile.
I new to R and am wondering if I can write a trycatch method to skip this account and continue the method. At the moment the process stops with a Teequest Status Code:403/
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:nrow(account_summary)){
view_id <- account_summary$viewId[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
}
# Make a more compact set of data
clean_summary <- select(account_summary,
Account = accountName,
Property + webPropertyName,
View = viewName,
Type = type,
Level = level,
'Start Date' = start_date,
'End Date' = end_date,
Sessions = sessions,
Pageviews = pageviews)
write.csv (clean_summary, "summary_results.csv", row.names = FALSE)
You can use tryCatch
like this:
...
ga_data <- tryCatch(google_analytics_4(viewId = view_id,
date_range = c(start_date,end_date),
metrics = metrics,
dimensions = dimensions),
error = function(ex){
warning(ex)
NULL
})
...
The example turns the error into a warning instead, and returns a NULL