R/gtm_variables_methods.R

Defines functions gtm_variables_update gtm_variables_revert gtm_variables_delete gtm_variables_create gtm_variables_get gtm_variables_list

Documented in gtm_variables_create gtm_variables_delete gtm_variables_get gtm_variables_list gtm_variables_revert gtm_variables_update

#' gtm_variables_list
#'
#' Lists all of the custom Variables of a GTM 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. Defaults to NULL because this will default to the default workspace.
#' @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_variables_list <- function(account_id,container_id,workspace_id,token,verbose=FALSE) {

  require(tidyverse)

  #construct the URL

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

token_status(token)

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

call_content <- content(call,'parsed')

#convert all into one table

cc_tbl <- gtm_df_vars(call_content)

return(cc_tbl)

}

#' gtm_variables_get
#'
#' Get a specific custom Variable of a GTM 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. Defaults to NULL because this will default to the default workspace.
#' @param variable_id The ID of the variable you want to get. Could be NULL if you want to pass the variable name
#' @param token An OAuth token object
#' @param variable_name Defaults to NULL, but you can use this if you don't know the variable ID and you just want to delete by name
#' @param verbose If you want a message letting you know that your results have been successfully retrieved. Defaults to FALSE
#' @export

gtm_variables_get <- function(account_id,container_id,workspace_id,variable_id,token,variable_name=NULL,verbose=FALSE) {

  require(purrr)
  require(tidyverse)

  token_status(token)

  if (is_empty(variable_id) == TRUE) {

    variable_id <- gtm_variables_list(account_id,container_id,workspace_id,token) %>%
      filter(name == variable_name) %>%
      distinct(variable_id) %>%
      pull(variable_id)

  }

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

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

  var <- content(call,'parsed')

  vartype <- var$type

  if (vartype == 'gas') {

    new_tbl <- gtm_var_gas(var)

  } else if (vartype == 'fup') {

    new_tbl <- gtm_var_fup(var)

  } else if (vartype == 'md') {

    new_tbl <- gtm_var_md(var)

  } else if (vartype == 'smm') {

    new_tbl <- gtm_var_lookup_tbl(var)

  }

  return(new_tbl)

}

#' gtm_variables_create
#'
#' Create a new GTM custom variables.
#' @param account_id The ID of the account
#' @param container_id The ID of the container
#' @param workspace_id The ID of the workspace. Defaults to NULL because this will default to the default workspace.
#' @param token An OAuth token object - needs to be set to 'edit containers'
#' @param var_type What kind of variable; options are 'parameter','user_property','lookup_table', and 'ga_settings'
#' @param variable_name The name of the new custom variable. For Google Analytics, provide the GA property ID.
#' @param set_default Do you want to set a default value? Logical vector, but defaults to FALSE
#' @param collect_ad_id Do you want to enable Google Analytics Ad ID features? Defaults to FALSE
#' @param default_value The default value you are setting. Defaults to NULL
#' @param map_input What value you are using for the mapping in the lookup table (only for lookup table). Defaults to NULL
#' @param map_values The key-value pairs you want to map, in a dataframe form. This needs to be provided as two columns, ideally with the first called key and the second called value. Defaults to NULL
#' @export

gtm_variables_create <- function(account_id,container_id,workspace_id,token,var_type,variable_name,set_default=FALSE,collect_ad_id= FALSE,default_value=NULL,map_input=NULL,map_values=NULL) {

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

  token_status(token)

  var_code <- gtm_var_dictionary(var_type,'name')

if (var_code == 'gas') {

  post_body <- gtm_create_gas(variable_name, collect_ad_id)

} else if (var_code %in% c('md','fup','v')) {

  post_body <- gtm_create_var(variable_name,var_code,set_default,default_value)

} else if (var_code == 'smm') {

  post_body <- gtm_create_lookup_tbl(variable_name,set_default,map_input,map_values)

} else {

  stop('Wrong variable type - check the instructions and start again')

}

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

  call$status

}

#' gtm_variables_delete
#'
#' Delete a GTM custom variables.
#' @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 variable_id The ID of the variable to delete. If NULL then the variable list will be called with the variable name
#' @param token An OAuth token object - needs to be set to 'edit containers'
#' @param variable_name Defaults to NULL, but you can use this if you don't know the variable ID and you just want to delete by name
#' @export

gtm_variables_delete <- function(account_id,container_id,workspace_id,variable_id,token,variable_name=NULL) {

  require(purrr)
  require(tidyverse)

  token_status(token)

  variable_id <- gtm_var_id_check(account_id,container_id,workspace_id,variable_id,token,variable_name)

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

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

  call$status

}

#' gtm_variables_revert
#'
#' Revert a GTM custom variable.
#' @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 variable_id The ID of the variable to revert. If NULL then the variable list will be called with the variable name
#' @param token An OAuth token object - needs to be set to 'edit containers'
#' @param variable_name Defaults to NULL, but you can use this if you don't know the variable ID and you just want to revert by name
#' @export

gtm_variables_revert <- function(account_id,container_id,workspace_id,variable_id,token,variable_name=NULL) {

  require(purrr)
  require(tidyverse)

  token_status(token)

  variable_id <- gtm_var_id_check(account_id,container_id,workspace_id,variable_id,token,variable_name)

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

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

  call$status

}

#' gtm_variables_update
#'
#' Update GTM custom variables.
#' @param account_id The ID of the account
#' @param container_id The ID of the container
#' @param workspace_id The ID of the workspace. Defaults to NULL because this will default to the default workspace.
#' @param variable_id The ID of the variable to update. If NULL then the variable list will be called with the variable name
#' @param token An OAuth token object - needs to be set to 'edit containers'
#' @param var_type What kind of variable; options are 'parameter','user_property','lookup_table', and 'ga_settings'
#' @param variable_name The name of the new custom variable. For Google Analytics, provide the GA property ID.
#' @param set_default Do you want to set a default value? Logical vector, but defaults to FALSE
#' @param collect_ad_id Do you want to enable Google Analytics Ad ID features? Defaults to FALSE
#' @param default_value The default value you are setting. Defaults to NULL
#' @param map_input What value you are using for the mapping in the lookup table (only for lookup table). Defaults to NULL
#' @param map_values The key-value pairs you want to map, in a dataframe form. This needs to be provided as two columns, ideally with the first called key and the second called value. Defaults to NULL
#' @export

gtm_variables_update <- function(account_id,container_id,workspace_id,variable_id,token,var_type,variable_name = NULL,set_default=FALSE,collect_ad_id= FALSE,default_value=NULL,map_input=NULL,map_values=NULL) {

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

  var_code <- gtm_var_dictionary(var_type,'name')

  token_status(token)

  variable_id <- gtm_var_id_check(account_id,container_id,workspace_id,variable_id,token,variable_name)

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

  if (is_empty(variable_name) == TRUE) {

    variable_name <- gtm_variables_get(account_id,container_id,workspace_id,variable_id,token) %>%
      distinct(name) %>%
      pull(name)

  }


if (var_code == 'gas') {

  post_body <- gtm_create_gas(variable_name, collect_ad_id)

} else if (var_code %in% c('md','fup','v')) {

  post_body <- gtm_create_var(variable_name,var_code,set_default,default_value)

} else if (var_code == 'smm') {

  post_body <- gtm_create_lookup_tbl(variable_name,set_default,map_input,map_values)

} else {

  stop('Wrong variable type - check the instructions and start again')

}

formatvals <- list(convertNullToValue =
                            list(type = 'template',
                                 value = '(empty)'),
                          convertFalseToValue =
                            list(type = 'boolean',
                                 value = 'false'),
                          convertTrueToValue =
                            list(type = 'boolean',
                                 value = 'true'),
                          convertUndefinedToValue =
                            list(type = 'template',
                                 value = '(empty)')
                          )

post_body$formatValues <- formatvals

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

  call$status

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