R/gtm_variables_create_methods.R

Defines functions gtm_create_var gtm_create_gas gtm_create_lookup_tbl gtm_variables_create_params

Documented in gtm_create_gas gtm_create_lookup_tbl gtm_create_var gtm_variables_create_params

#' gtm_variables_create_params
#'
#' Create a new GTM custom parameter 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. Defaults to NULL because this will default to the default workspace.
#' @param variable_name The name of the new custom variable
#' @param set_default Do you want to set a default value? Logical vector, but defaults to FALSE
#' @param default_value The default value you are setting
#' @param verbose If you want a message letting you know that your results have been successfully retrieved. Defaults to FALSE
#' @export

gtm_variables_create_params <- function(account_id,container_id,workspace_id,token,variable_name,set_default=FALSE,default_value=NULL,verbose=FALSE) {

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

  token_status(token)

if (set_default == FALSE) {

  post_body <- list(name = variable_name,
             type = "md",
             parameter = list(list(type = 'boolean',
                              key = 'setDefaultValue',
                              value = 'false'),
                          list(type = 'template',
                               key = 'eventType',
                               value = 'CUSTOM'),
                          list(type = 'template',
                               key = 'key',
                               value = variable_name)
             )
  )

} else {

  post_body <- list(name = variable_name,
             type = "md",
             parameter = list(list(type = 'boolean',
                              key = 'setDefaultValue',
                              value = 'true'),
                          list(type = 'template',
                               key = 'eventType',
                               value = 'CUSTOM'),
                          list(type = 'template',
                               key='defaultValue',
                               value= default_value),
                          list(type = 'template',
                               key = 'key',
                               value = variable_name)
             )
  )
}

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

  call$status

}

#' gtm_create_lookup_tbl
#'
#' Convert a data frame into a list format to be used to create a lookup table
#' @param variable_name The name of the new custom variable
#' @param set_default Do you want to set a default value? Logical vector, but defaults to FALSE
#' @param default_value The default value you are setting
#' @param map_input The variable input that you will be mapping from
#' @param map_values The key-value pairs that you will use to map from and to; note, if the data frame has more than two columns this will automatically use the first two columns
#' @export

gtm_create_lookup_tbl <- function(variable_name,set_default,map_input,map_values) {

map_values <- map_values %>% select(1:2) %>% rename(key = 1,value=2)

sublist <- unname(Map(function(key,value) list(type = "map",
                                               map=list(
                                                 list(type="template",
                                                      key ="key",
                                                      value = key),
                                                 list(type = "template",
                                                      key = "value",
                                                      value = value))), map_values$key, map_values$value))

res <- list(name = variable_name,
            type = 'smm',
            parameter = c(list(
              list(type = 'boolean',
                   key = 'setDefaultValue',
                   value = ifelse(set_default == TRUE,'true','false')),
              list(type = 'template',
                   key = 'input',
                   value = paste('{{',map_input,'}}',sep=''))),
              list(list(type = "list",
                        key = "map",
                        list=sublist))))

return(res)

}

#' gtm_create_gas
#'
#' Make the list to pass to the GTM API for a Google Analytics Settings variable
#' @param variable_name The value of the GA web property to send data to
#' @param collect_ad_id Do you want to enable Google Analytics Ad ID features? Defaults to FALSE
#' @export

gtm_create_gas <- function(variable_name,collect_ad_id) {

  gas <- list(name = 'Google Analytics Settings',
             type = 'gas',
             parameter = list(list(type = 'boolean',
                              key = 'collectAdID',
                              value = ifelse(collect_ad_id == FALSE,'false','true')),
                          list(type = 'template',
                               key = 'trackingId',
                               value = variable_name)
             )
  )

  return(gas)
}

#' gtm_create_var
#'
#' Make the list to pass to the GTM API for a parameter, user property or data layer variable
#' @param variable_name The value of the GA web property to send data to
#' @param var_code Either 'fup', 'md' or 'v'
#' @param set_default Do you want to set a default value? Logical vector, but defaults to FALSE
#' @param default_value Do you want to set a default value? Logical vector, but defaults to NULL
#' @export

gtm_create_var <- function(variable_name,var_code,set_default=FALSE,default_value=FALSE) {

  require(rlist)

  post_body <- list(name = variable_name,
                    type = var_code)

  par <- list(list(type = 'boolean',
                    key = 'setDefaultValue',
                    value = ifelse(set_default == FALSE,'false','true')),
                    list(type = 'template',
                          key = ifelse(var_code == 'v','name','key'),
                          value = variable_name),
                    list(type = 'template',
                        key = ifelse(var_code=='v','dataLayerVersion','eventType'),
                        value = ifelse(var_code=='v',dl_version,'CUSTOM'))
                    )

if (set_default == TRUE) {

    pardef <-  list(type = 'template',
                    key='defaultValue',
                    value= default_value)

    par <- list.append(par,pardef)

  }

  post_body$parameter <- par

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