googleAnalyticsR 403用户权限

时间:2017-10-25 02:00:36

标签: r google-analytics-api

我正在循环使用50个GA帐户,并且有一个用户权限,我无法访问我得到的错误...

错误:API返回:用户对此配置文件没有足够的权限。 error_check(out)出错:   返回的API:用户对此个人资料没有足够的权限。

我是R的新手,我想知道我是否可以编写一个trycatch方法来跳过此帐户并继续该方法。目前,该过程以Teequest状态代码: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)

1 个答案:

答案 0 :(得分:0)

您可以像这样使用tryCatch

...

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
                      })

...

该示例将错误转换为警告,并返回NULL