R/gtm_workspaces_methods.R

Defines functions gtm_workspaces_create_version gtm_workspaces_update gtm_workspaces_delete gtm_workspaces_create gtm_workspaces_sync gtm_workspaces_get gtm_workspaces_list

Documented in gtm_workspaces_create gtm_workspaces_create_version gtm_workspaces_delete gtm_workspaces_get gtm_workspaces_list gtm_workspaces_sync gtm_workspaces_update

#' gtm_workspaces_list
#'
#' Get a list of all of the Google Tag Manager workspaces in a specific container
#' @param account_id The ID of the account that you need a workspace list for
#' @param container_id The ID of the container that you need a workspace 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_workspaces_list <- function(account_id,container_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!
  "

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

  token_status(token)

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

  call_content <- content(call,'parsed')

  verbose_calls(call,call_content,verbose)

  roll <- 1:length(call_content$workspace)

  workspace_tbl <- tibble()

  for (i in roll) {

    new_tbl <- as_tibble(call_content$workspace[[i]])

    workspace_tbl <- bind_rows(workspace_tbl,new_tbl)

  }

  workspace_tbl <- workspace_tbl %>%
    tidycols()

  return(workspace_tbl)

}

#' gtm_workspaces_get
#'
#' Get the details of a Google Tag Manager workspaces in a specific container
#' @param account_id The ID of the account
#' @param container_id The ID of the container
#' @param workspace_id The ID of the workspace that you need details
#' @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_workspaces_get <- function(account_id,container_id,workspace_id,token,verbose = FALSE) {

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

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

  token_status(token)

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

  call_content <- content(call,'parsed')

  verbose_calls(call,call_content,verbose)

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

  return(workspace_tbl)

}

#' gtm_workspaces_sync
#'
#' Syncs a workspace to the latest container version by updating all unmodified workspace entities and displaying conflicts for modified entities. Returns a nested list.
#' @param account_id The ID of the account
#' @param container_id The ID of the container
#' @param workspace_id The ID of the workspace that you need to sync
#' @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_workspaces_sync <- function(account_id,container_id,workspace_id,token,verbose = FALSE) {

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

  base <- 'https://www.googleapis.com/tagmanager/v2/'
  path <- paste('accounts',account_id,'containers',container_id,'workspaces',workspace_id,sep='/')
  api_url <- paste(base,path,':sync',sep='')

  token_status(token)

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

  call_content <- content(call,'parsed')

  verbose_calls(call,call_content,verbose)

  return(call_content)

}

#' gtm_workspaces_create
#'
#' Creates a Workspace.
#' @param account_id The ID of the account
#' @param container_id The ID of the container
#' @param workspace_name The name of the new workspace
#' @param token An OAuth token object
#' @param description The description of the new workspace. Optional
#' @param verbose If you want a message letting you know the results of your API call. Defaults to FALSE
#' @export

gtm_workspaces_create <- function(account_id,container_id,workspace_name,token,description=NULL,verbose = FALSE) {

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

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

  call_body <- list(name = workspace_name)

  if (is_empty(description)==FALSE) {

    call_body$description <- description
  }

  token_status(token)

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

  call_content <- content(call,'parsed')

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

  verbose_calls(call,call_content,verbose)

  return(resource_tbl)

}

#' gtm_workspaces_delete
#'
#' Deletes a Workspace.
#' @param account_id The ID of the account
#' @param container_id The ID of the container
#' @param workspace_id The id of the workspace you want to delete
#' @param token An OAuth token object
#' @export

gtm_workspaces_delete <- function(account_id,container_id,workspace_id,token) {

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

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

  call_body <- list(name = workspace_name)

  token_status(token)

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

  call_content <- content(call,'parsed')

  verbose_calls(call,call_content,verbose)

}

#' gtm_workspaces_update
#'
#' Updates a Workspace.
#' @param account_id The ID of the account
#' @param container_id The ID of the container
#' @param workspace_id The ID of the workspace
#' @param new_name The new name of the new workspace. Mandatory.
#' @param token An OAuth token object
#' @param description The description of the new workspace. Optional
#' @param verbose If you want a message letting you know the results of your API call. Defaults to FALSE
#' @export

gtm_workspaces_update <- function(account_id,container_id,workspace_id,new_name,token,description=NULL,verbose = FALSE) {

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

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

  call_body <- list(name = new_name)

  if (is_empty(description)==FALSE) {

    call_body$description <- description
  }

  token_status(token)

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

  call_content <- content(call,'parsed')

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

  verbose_calls(call,call_content,verbose)

  return(resource_tbl)

}

#' gtm_workspaces_create_version
#'
#' Creates a Container Version from the entities present in the workspace, deletes the workspace, and sets the base container version to the newly created version.
#' @param account_id The ID of the account
#' @param container_id The ID of the container
#' @param workspace_id The ID of the workspace
#' @param token An OAuth token object
#' @param name The name of the container version to be created.
#' @param notes The notes of the container version to be created. Defaults to NULL
#' @export

gtm_workspaces_create_version <- function(account_id,container_id,workspace_id,token,name,notes=NULL) {

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

  #not sure this works!

  token_status(token)

  api_path <- 'https://www.googleapis.com/tagmanager/v2'

  cv_parent <- paste('accounts',account_id,'containers',container_id,'workspaces',workspace_id,sep='/')

  cv <- 'create_version'

  cv_url <- paste(paste(api_path,cv_parent,sep='/'),cv,sep = ':')

  call_body <- list(name = name)

  if (is_empty(notes)==FALSE) {

    call_body$notes <- notes
  }


  call <- easy_call('post',cv_url,token,call_body=call_body)

  print(call$status_code)

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