#' gtm_containers_list
#'
#' Get a list of all of the Google Tag Manager containers in a specific account
#' @param account_id The ID of the account that you need a container list for
#' @param token An OAuth token object
#' @param verbose If you want a message letting you know that your results have been successfully retrieved. Defaults to FALSE
#' @export
gtm_containers_list <- function(account_id,token,verbose = FALSE) {
  require(neugelbtools)
  require(httr)
  require(tidyverse)
  require(purrr)
  "
  NOTE: The documentation suggests that you can use a pageToken to paginate your requests, but it doesn't provide any more detail about how it works.
  So I have left it out!
  "
  token_status(token)
  api_url <- paste('https://www.googleapis.com/tagmanager/v2/accounts/',account_id,'/containers',sep='')
  call <- easy_call('get',api_url,token)
  call_content <- content(call,'parsed')
  verbose_calls(call,call_content,verbose)
  container_tbl <- tibble()
  for (i in 1:length(call_content$container)) {
    new_tbl <- as_tibble(call_content$container[[i]]) %>%
      unnest(usageContext)
    if (length(new_tbl$domainName) == 1) {
      new_tbl <- new_tbl %>%
        unnest(domainName)
    } else if (is_empty(new_tbl$domainName) == TRUE | length(new_tbl$domainName) < 1) {
      new_tbl$domainName <- NA
    } else {
      new_tbl$domainName <- str_c(new_tbl$domainName,collapse=', ')
    }
    container_tbl <- bind_rows(container_tbl,new_tbl)
  }
  container_tbl <- container_tbl %>%
    tidycols() %>%
    usage_context_fix()
  return(container_tbl)
}
#' gtm_containers_get
#'
#' Get a data frame of information on a specific GTM container
#' @param account_id The ID for the GTM account where the desired container sits
#' @param container_id The ID for the GTM container that you want to see
#' @param token An OAuth token object
#' @param verbose If you want a message letting you know that your results have been successfully retrieved. Defaults to FALSE
#' @export
gtm_containers_get <- function(account_id,container_id,token,verbose=FALSE) {
  require(neugelbtools)
  require(httr)
  require(tidyverse)
  token_status(token)
  api_slug <- 'https://www.googleapis.com/tagmanager/v2'
  #check to see if the container id is the public ID
  if (str_detect(str_to_upper(container_id),'GTM')==TRUE) {
    #I don't know why this is necessary, but it is
    conid <- container_id
    account_list <- gtm_containers_list(account_id,token) %>%
      filter(public_id == conid)
  cid <- account_list[['container_id']]
  path <- paste('accounts',account_id,'containers',cid,sep='/')
  } else {
    path <- paste('accounts',account_id,'containers',container_id,sep='/')
  }
  api_url <- paste(api_slug,path,sep='/')
  call <- GET(api_url,
              add_headers(Authorization = paste("Bearer", token$credentials$access_token)),
              encode = 'json')
  call_content <- content(call,'parsed')
  verbose_calls(call,call_content,verbose)
  containers_tbl <- as_tibble(call_content) %>%
    unnest(usageContext) %>%
    tidycols() %>%
    select(-path) %>%
    usage_context_fix()
  return(containers_tbl)
}
#' gtm_containers_create
#'
#' Create a new Google Tag Manager container from scratch
#' @param account_id The ID for the GTM account where the desired container sits
#' @param container_name The name of the new container
#' @param usage_context The type of container you want to create. Valid values are: 'web', 'android', 'ios'
#' @param token An OAuth token object
#' @param domain_name Domain name(s) that you want to associate with the container. Optional.
#' @param notes A string of notes that you want to write to the container. Optional.
#' @param verbose If you want a message letting you know that your container has been successfully created. Defaults to FALSE
#' @export
gtm_containers_create <- function(account_id,container_name,usage_context,token,domain_name=NULL,notes=NULL,verbose=FALSE) {
  require(neugelbtools)
  require(httr)
  require(tidyverse)
  require(purrr)
  token_status(token)
  "
  Problem: only creates legacy containers (as far as I can tell)
  Also, domain name and notes are not perfect
  "
  #check the usage context is correct
  uc <- str_to_lower(usage_context)
  if (!str_to_lower(uc) %in% c('web','android','ios')) {
    stop('Error: Incorrect usage context; it must be one of "web", "android", or "ios"')
  }
  #create the post url
  api_url <- paste('https://www.googleapis.com/tagmanager/v2/accounts/',account_id,'/containers',sep='')
  #create the list with required components
  call_body <- list(accountId = account_id,
                    name = container_name,
                    usageContext = list(usage_context))
  #check for the optional components
  if (is_empty(domain_name) == FALSE) {
    if (class(domain_name) == 'list') {
      call_body$domainName <- domain_name
    } else if (class(domain_name) == 'character') {
      call_body$domainName <- list(domain_name)
    } else {
      stop('Error: If you want to include a domain name (or names), please provide it/them as a list or a character vector')
    }
  }
  if (is_empty(notes) == FALSE) {
    call_body$notes <- notes
  }
  call <- easy_call('post',api_url,token,call_body = call_body)
  call_content <- content(call,'parsed')
  verbose_calls(call,call_content,verbose)
}
#' gtm_containers_delete
#'
#' Delete a Google Tag Manager container
#' @param account_id The ID for the GTM account where the desired container sits
#' @param container_id The ID for the GTM container that you want to see
#' @param token An OAuth token object
#' @param verbose If you want a message letting you know that your container has been successfully created. Defaults to FALSE
#' @export
gtm_containers_delete <- function(account_id,container_id,token,verbose=FALSE) {
  require(neugelbtools)
  require(httr)
  require(tidyverse)
  require(purrr)
  token_status(token)
  api_slug <- 'https://www.googleapis.com/tagmanager/v2'
  path <- paste('accounts',account_id,'containers',container_id,sep='/')
  api_url <- paste(api_slug,path,sep='/')
  call <- easy_call('delete',api_url,token)
  call_content <- content(call,'parsed')
  verbose_calls(call,call_content,verbose)
}
#' gtm_containers_update
#'
#' Update an existing GTM container
#' @param account_id The ID for the GTM account where the desired container sits
#' @param container_id The id of the container to be updated
#' @param container_name The new name for the container
#' @param usage_context The type of container you want to create. Valid values are: 'web', 'android', 'ios'
#' @param token An OAuth token object, should be set to 'edit containers'
#' @param domain_name Domain name(s) that you want to associate with the container. Can be a list or a character vector
#' @param notes A string of notes that you want to write to the container
#' @param verbose If you want a message letting you know that your container has been successfully created. Defaults to FALSE
#' @export
gtm_containers_update <- function(account_id,container_id,container_name,usage_context,domain_name,notes,token,verbose=FALSE) {
  require(neugelbtools)
  require(httr)
  require(tidyverse)
  require(purrr)
  token_status(token)
  "
  Problem: only creates legacy containers (as far as I can tell)
  "
  #check the usage context is correct
  uc <- str_to_lower(usage_context)
  if (!str_to_lower(uc) %in% c('web','android','ios')) {
    stop('Error: Incorrect usage context; it must be one of "web", "android", or "ios"')
  }
  if (str_detect(str_to_upper(container_id),'GTM')==TRUE) {
    #I don't know why this is necessary, but it is
    conid <- container_id
    account_list <- gtm_containers_list(account_id,token) %>%
      filter(public_id == conid)
    if ('domain_name' %in% names(account_list)) {
      account_list <- account_list %>%
        select(-domain_name) %>%
        distinct()
    }
  cid <- account_list[['container_id']]
  path <- paste('accounts',account_id,'containers',cid,sep='/')
  } else {
    path <- paste('accounts',account_id,'containers',container_id,sep='/')
  }
  #create the post url
  api_url <- paste('https://www.googleapis.com/tagmanager/v2',path,sep='/')
  #create the list with required components
  call_body <- list(accountId = account_id,
                    name = container_name,
                    usageContext = list(uc),
                    notes = notes)
  #check for the optional components
  if (class(domain_name) == 'list') {
      call_body$domainName <- domain_name
    } else if (class(domain_name) == 'character') {
      call_body$domainName <- list(domain_name)
    } else {
      stop('Error: If you want to include a domain name (or names), please provide it/them as a list or a character vector')
    }
  call <- easy_call('put',api_url,token,call_body = call_body)
  call_content <- content(call,'parsed')
  verbose_calls(call,call_content,verbose)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.