代码之家  ›  专栏  ›  技术社区  ›  hellogoodbye

googleAnalyticsR 403用户权限

  •  0
  • hellogoodbye  · 技术社区  · 8 年前

    我正在浏览50个GA帐户,有一个具有用户权限的帐户,我无法访问我收到的错误是。。。

    错误:API返回:用户对此配置文件没有足够的权限。 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 回复  |  直到 8 年前
        1
  •  0
  •   MarkeD    8 年前

    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