0

我发现了一篇文章“(自动)记录您的 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)
}
4

1 回答 1

0

该帖子的作者制作了一个名为 googleTagManagerR 的包,以便更轻松地使用上述内容,其项目页面在这里

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

# 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)
于 2018-05-09T14:44:21.413 回答