Google API代码管理器 - 如何解决“找不到帐户中的错误:对象'accountId'未找到”

时间:2018-05-07 20:02:48

标签: r google-api gtm-oauth2 gtm-database

我发现了一个帖子“(自动)记录您的Google跟踪代码管理器实施”(https://statsravingmad.com/measure/document-your-gtm-implementation/)。

如果我没有错,我应该在RStudio上运行这个脚本来获取包含GTM所有内容的spreadSHEET,对吗?

所以,当我运行下面的代码时,在RStudio中,它显示我: “在accountId中出错:找不到对象'accountId'”

重要的是要说我为虚拟测试创建的这些数据(accountId< - 3097258993& containerId< - 8968207)。

有人知道我该怎么做才能解决它?

谢谢!

library(googleAuthR);
library(googlesheets) ## In order to push it to a Google Sheet

## Set the desired scopes for our problem(s)
options(googleAuthR.scopes.selected = c('https://www.googleapis.com/auth/tagmanager.delete.containers',
    'https://www.googleapis.com/auth/tagmanager.edit.containers',
    'https://www.googleapis.com/auth/tagmanager.edit.containerversions',
    'https://www.googleapis.com/auth/tagmanager.manage.accounts',
    'https://www.googleapis.com/auth/tagmanager.manage.users',
    'https://www.googleapis.com/auth/tagmanager.publish',
    'https://www.googleapis.com/auth/tagmanager.readonly'))

## Now authenticate to Google
gar_auth(new_user = T)
## Auth with `googlesheets` as well (since this can be a different account)
gs_auth()
## Define functions for the `googletagmanagerv1` package
## but let's use them directly here
accounts.list <- function() {
    url <- "https://www.googleapis.com/tagmanager/v1/accounts"
    # tagmanager.accounts.list
    f <- gar_api_generator(url, "GET", data_parse_function = function(x) x)
    f()

}

containers.list <- function(accountId) {
  url <- paste0("https://www.googleapis.com/tagmanager/v1/accounts/",accountId,"/containers/")
  # tagmanager.tags.list
  f <- gar_api_generator(url, "GET", data_parse_function = function(x) x)
  f()
}

tags.list <- function(accountId,containerId) {
  url <- paste0("https://www.googleapis.com/tagmanager/v1/accounts/",accountId,"/containers/",containerId,"/tags")
  # tagmanager.tags.list
  f <- gar_api_generator(url, "GET", data_parse_function = function(x) x)
  f()
}


triggers.list <- function(accountId,containerId) {
  url <- paste0("https://www.googleapis.com/tagmanager/v1/accounts/",accountId,"/containers/",containerId,"/triggers")
  # tagmanager.triggers.list
  f <- gar_api_generator(url, "GET", data_parse_function = function(x) x)
  f()
}


variables.list <- function(accountId,containerId) {
  url <- paste0("https://www.googleapis.com/tagmanager/v1/accounts/",accountId,"/containers/",containerId,"/variables")
  # tagmanager.variables.list
  f <- gar_api_generator(url, "GET", data_parse_function = function(x) x)
  f()
}
## We can get the accounts using the `accounts.list()`
## Sample Ids (originating from this blog)
accountId <- 3097258993
containerId <- 8968207

time <- 3;
## A loop for generic use
for (acc_Id in accountId){
  for (con_Id in containerId) {
    container.tags <- tags.list(acc_Id,con_Id)
    container.triggers <- triggers.list(acc_Id,con_Id)
    container.variables <- variables.list(acc_Id,con_Id)

    ## Parameter is a `JSON` (so in `R` this is a `list`), that we will not use for now
    # str(container.tags$tags$parameter)

    ## Objects of interest
    tags <- container.tags$tags
    triggers <- container.triggers$triggers
    variables <- container.variables$variables

    ## Register a new Google Sheet to pass the data
    gs_new(paste("(Auto) GTM Implementation - ",acc_Id,"_",con_Id), verbose = TRUE)
    s_id <- gs_title(paste("(Auto) GTM Implementation - ",acc_Id,"_", con_Id), verbose = TRUE)
    yo.tags <- gs_ws_new(ss=s_id, ws_title="Tags" , input = tags[,1:10], trim = TRUE)
    yo.triggers <- gs_ws_new(ss=s_id, ws_title="Triggers", input = triggers, trim = TRUE)
    yo.variables <- gs_ws_new(ss=s_id, ws_title="Variables", input = variables, trim = TRUE)

    ## Get Google Sheet URL
    # s_id$browser_url
    # > https://docs.google.com/spreadsheets/d/1qibV3BSOtzSlI0vG6JC8xnzqnu6TnpFuxabaYN2vXeI/

  }
  ## This is used to cool off the Google Drive API calls...
  ## Since we only write to one Google Sheet we don't need it in this run.
  # Sys.sleep(time)
}

1 个答案:

答案 0 :(得分:0)

该帖子的作者制作了一个名为googleTagManagerR的软件包,以便更轻松地使用上述内容,project page is here

然后,您可以通过以下功能获取信息:

# install as per its website then...
library(googleTagManagerR)
# auth in browser
gtm_auth()

# get your data
gtm_accounts_list()
gtm_containers_list(accountId = YOUR_ACCOUNT_ID)