R/gtm_accounts_methods.R

Defines functions gtm_accounts_update gtm_accounts_get gtm_accounts_list

Documented in gtm_accounts_get gtm_accounts_list gtm_accounts_update

#' gtm_accounts_list
#'
#' Get a list of the Google Tag Manager accounts that you have access to
#' @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_accounts_list <- function(token,verbose = FALSE) {

  require(neugelbtools)
  require(httr)
  require(tidyverse)

  "
  NOTE: The documentation suggests that you can use a page_token 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 <- 'https://www.googleapis.com/tagmanager/v2/accounts'

  call <- easy_call('get',api_url,token)

  content <- content(call,'parsed')

  verbose_calls(call,call_content,verbose)

  roll <- 1:length(content$account)

  accounts_tbl <- tibble()

  for (i in roll) {

    new_tbl <- as_tibble(content$account[[i]])

    accounts_tbl <- bind_rows(accounts_tbl,new_tbl)

  }

  accounts_tbl <- accounts_tbl %>%
    tidycols() %>%
    select(-path)

  return(accounts_tbl)

}

#' gtm_accounts_get
#'
#' Get a data frame of information on a specific GTM account
#' @param account_id The GTM account ID that you want information on
#' @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_accounts_get <- function(account_id,token,verbose=FALSE) {

  require(neugelbtools)
  require(httr)
  require(tidyverse)

token_status(token)

  api_url <- paste('https://www.googleapis.com/tagmanager/v2/accounts',account_id,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)

  accounts_tbl <- as_tibble(call_content) %>%
    tidycols()

  return(accounts_tbl)

}

#' gtm_accounts_update
#'
#' Update a specific GTM account.
#' @param account_id The GTM account ID that you want to update
#' @param name The new name of the account that you want to assign. Mandatory.
#' @param token An OAuth token object
#' @param share_data The new value for sharing data with Google - a logical value so this is set to either TRUE or FALSE. Optional
#' @param verbose If you want a message to let you know that your account has been successfully updated. Defaults to FALSE
#' @export

gtm_accounts_update <- function(account_id,name,token,share_data=NULL,verbose=FALSE) {

  require(httr)
  require(tidyverse)
  require(purrr)

token_status(token)

  api_url <- paste('https://www.googleapis.com/tagmanager/v2/accounts',account_id,sep='/')

  update_list <- list(accountId = account_id,
                      name = name)

  if (is_empty(share_data)==FALSE) {

    if (share_data %in% c(TRUE,FALSE)) {

      update_list$shareData <- share_data

    } else {

      stop('You need to set share_data to either TRUE or FALSE')

    }

  }

  call <- easy_call('put',api_url,token,call_body=update_list)

  call_content <- content(call,'parsed')

  verbose_calls(call,call_content,verbose)

}
neugelb/gtmr documentation built on June 25, 2020, 10:06 a.m.