R/gtm_triggers_methods.R

Defines functions gtm_triggers_update gtm_triggers_create gtm_triggers_delete gtm_triggers_get gtm_triggers_list

Documented in gtm_triggers_create gtm_triggers_delete gtm_triggers_get gtm_triggers_list gtm_triggers_update

#' gtm_triggers_list
#'
#' Lists all of the triggers in 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_triggers_list <- function(account_id,container_id,workspace_id,token,verbose = FALSE) {

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

  token_status(token)

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

  wid <- wspace_picker(account_id,container_id,workspace_id,token)

  api_url <- paste(path,wid,'triggers',sep='/')

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

  call_content <- content(call,'parsed')

trigger_tbl <- gtm_df_trigs(call_content)

  verbose_calls(call,call_content,verbose)

  return(trigger_tbl)

}

#' gtm_triggers_get
#'
#' Get a specific trigger from 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.
#' @param token An OAuth token object
#' @param trigger_id The ID of the trigger - can be either the numeric ID or a string of the trigger name (has to be exact!). Passing the numeric ID is the fastest method.
#' @param verbose If you want a message letting you know that your results have been successfully retrieved. Defaults to FALSE
#' @export

gtm_triggers_get <- function(account_id,container_id,workspace_id,token,trigger_id,verbose = FALSE) {

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

  token_status(token)

  api_url <- trigger_id_url(account_id,container_id,workspace_id,trigger_id,token)

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

  call_content <- content(call,'parsed')

  filter_tbl <- tibble()

  for (i in 1:length(call_content$filter)) {

    new_tbl <- tibble(filter_arg0 = call_content$filter[[i]]$parameter[[1]]$value,
                      filter_type = call_content$filter[[i]]$type,
                      filter_arg1 = call_content$filter[[i]]$parameter[[2]]$value,
                      triggerId = call_content$triggerId
    )

    filter_tbl <- bind_rows(filter_tbl,new_tbl)

  }

  call_content$filter <- NULL

  trigger_tbl <- as_tibble(call_content) %>%
    left_join(filter_tbl,by='triggerId') %>%
    tidycols()

  return(trigger_tbl)

}

#' gtm_triggers_delete
#'
#' Delete a specific trigger from 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
#' @param token An OAuth token object
#' @param trigger_id The ID of the trigger - can be either the numeric ID or a string of the trigger name (has to be exact!). Passing the numeric ID is the fastest method.
#' @param verbose If you want a message letting you know that your results have been successfully retrieved. Defaults to FALSE
#' @export

gtm_triggers_delete <- function(account_id,container_id,workspace_id,token,trigger_id,verbose = FALSE) {

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

  token_status(token)

  api_url <- trigger_id_url(account_id,container_id,workspace_id,trigger_id,token)

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

  call_content <- content(call,'parsed')

  verbose_calls(call,call_content,verbose)

}

#' gtm_triggers_create
#'
#' Create a new trigger for the GTM account
#' @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 trigger_name The name of the trigger
#' @param trigger_df A three column dataframe of the values you want to use for your trigger. Must be formatted by input argument (i.e. 'Event Name'), matching method (see GTM list, but for example 'endsWith'), and matching value, i.e. '_click'
#' @param verbose If you want a message letting you know that your results have been successfully retrieved. Defaults to FALSE
#' @export

gtm_triggers_create <- function(account_id,container_id,workspace_id,token,trigger_name,trigger_df,verbose = FALSE) {

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

  token_status(token)

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

  wid <- wspace_picker(account_id,container_id,workspace_id,token)

  api_url <- paste(path,wid,'triggers',sep='/')

  trigger_tbl <- trigger_df %>%
    rename(input = 1,
          method = 2,
          value = 3) %>%
    mutate(input = ifelse(str_detect(input,fixed('{{'))==FALSE,paste('{{',input,'}}',sep=''),input))

  filter_list <- list()

for (i in 1:nrow(trigger_tbl)) {

  extra_filter <-  list(type = trigger_tbl$method[i],
                        parameter = list(list(
                          type = "template",
                          key = "arg0",
                          value = trigger_tbl$input[i]),
                          list(type = "template",
                               key = "arg1",
                               value = trigger_tbl$value[i])))

  filter_list <- list.append(filter_list,extra_filter)

}

  post_body <- list(name = trigger_name,
                  type = 'always',
                  filter = filter_list)

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

  call_content <- content(call,'parsed')

  verbose_calls(call,call_content,verbose)

}

#' gtm_triggers_update
#'
#' Update a specific trigger from 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
#' @param token An OAuth token object
#' @param trigger_id The ID of the trigger - can be either the numeric ID or a string of the trigger name (has to be exact!). Passing the numeric ID is the fastest method.
#' @param trigger_df A three column df of the values you want to use for your trigger. Must be formatted by input argument (i.e. 'Event Name'), matching method (see GTM list, but for example 'endsWith'), and matching value, i.e. '_click'
#' @param new_name The new name that you want to use for your trigger. Defaults to NULL so that you don't change the name of the trigger.
#' @param verbose If you want a message letting you know that your results have been successfully retrieved. Defaults to FALSE
#' @export

gtm_triggers_update <- function(account_id,container_id,workspace_id,token,trigger_id,trigger_df,new_name=NULL,verbose = FALSE) {

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

  token_status(token)

  api_url <- trigger_id_url(account_id,container_id,workspace_id,trigger_id,token)

  if (is_empty(new_name)==TRUE) {

    if (is_empty(workspace_id)==TRUE) {

      workspace_id <- wspace_picker(account_id,container_id,NULL,token)

    }

    new_name <- gtm_triggers_get(account_id,container_id,token,trigger_id,workspace_id) %>%
        distinct(name) %>%
        pull(name)

  }

  trigger_tbl <- trigger_df %>%
    rename(input = 1,
          method = 2,
          value = 3) %>%
    mutate(input = ifelse(str_detect(input,fixed('{{'))==FALSE,paste('{{',input,'}}',sep=''),input))

  filter_list <- list()

for (i in 1:nrow(trigger_tbl)) {

  extra_filter <-  list(type = trigger_tbl$method[i],
                        parameter = list(list(
                          type = "template",
                          key = "arg0",
                          value = trigger_tbl$input[i]),
                          list(type = "template",
                               key = "arg1",
                               value = trigger_tbl$value[i])))

  filter_list <- list.append(filter_list,extra_filter)

}

  put_body <- list(name = new_name,
                  type = 'always',
                  filter = filter_list)

  call <- easy_call('put',api_url,token,put_body)

  call_content <- content(call,'parsed')

  verbose_calls(call,call_content,verbose)

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