#' gtm_tags_list
#'
#' Lists all of the tags in a GTM container. Returns a list composed of a number of dataframes.
#' @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 verbose If you want a message letting you know that your results have been successfully retrieved. Defaults to FALSE
#' @export
gtm_tags_list <- function(account_id,container_id,workspace_id,token,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,'tags',sep='/')
call <- easy_call('get',api_url,token)
call_content <- content(call,'parsed')
tag_list <- gtm_list_tags(call_content)
verbose_calls(call,call_content,verbose)
return(tag_list)
}
#' gtm_tags_summary
#'
#' A nice summary of all your GTM tags in a single dataframe, to help you find the specific tag information you are looking for.
#' @param gtm_tags_list Your list of GTM tags
#' @export
gtm_tags_summary <- function(gtm_tags_list) {
require(tidyverse)
summary_tbl <- tibble()
for (i in 1:length(gtm_tags_list)) {
tag_tbl <- bind_cols(gtm_tags_list[[i]]$tag_metadata,gtm_tags_list[[i]]$tag_settings) %>%
mutate(index = i)
summary_tbl <- bind_rows(summary_tbl,tag_tbl)
}
summary_tbl <- summary_tbl %>%
rename(tag_name = name)
return(summary_tbl)
}
#' gtm_tags_parameters
#'
#' Get a single table showing the parameters for all of your tags.
#' @param gtm_tags_list Your list of GTM tags
#' @export
gtm_tags_parameters <- function(gtm_tags_list) {
param_tbl <- tibble()
for (i in 1:length(gtm_tags_list)) {
new_param = gtm_tags_list[[i]]$tag_parameters
new_param <- new_param %>%
mutate(tag_id = gtm_tags_list[[i]]$tag_metadata$tag_id,
tag_name = gtm_tags_list[[i]]$tag_metadata$name,
index = i)
param_tbl <- bind_rows(param_tbl,new_param)
}
return(param_tbl)
}
#' gtm_tags_fields
#'
#' Get a single table showing all of the set fields for all of your tags
#' @param gtm_tags_list Your list of GTM tags
#' @export
gtm_tags_fields <- function(tag_list) {
field <- tibble()
for (i in 1:length(tag_list)) {
temp_tag <- tag_list[[i]]
if (nrow(temp_tag$tag_fields_to_set)==0) {
next()
} else {
meta <- temp_tag$tag_metadata %>%
select(account_id:name)
temp <- cbind(meta,temp_tag$tag_fields_to_set)
field <- bind_rows(field,temp)
}
}
field <- field %>%
rename(tag_name = name)
return(field)
}
#' gtm_tags_triggers
#'
#' Get a single table showing all of the triggers for all of your tags
#' @param gtm_tags_list Your list of GTM tags
#' @export
gtm_tags_triggers <- function(tag_list) {
require(purrr)
require(tidyverse)
triggers <- tibble()
for (i in 1:length(tag_list)) {
tag <- tag_list[[i]]
id <- tag$tag_metadata$tag_id
name <- tag$tag_metadata$name
fire <- tag$tag_triggers
block <- tag$tag_blockers
temp <- tibble()
if (is_empty(fire) == FALSE) {
fire <- fire %>%
mutate(trigger_type = 'firing',
tag_id = id,
tag_name = name) %>%
rename(trigger_id = 1)
temp <- bind_rows(temp,fire)
}
if (is_empty(block) == FALSE) {
block <- block %>%
mutate(trigger_type = 'blocking',
tag_id = id,
tag_name = name) %>%
rename(trigger_id = 1)
temp <- bind_rows(temp,block)
}
triggers <- bind_rows(triggers,temp) %>%
select(tag_id,tag_name,trigger_type,trigger_id)
}
return(triggers)
}
#' gtm_tags_get
#'
#' Get a specific tag from a GTM container. Returns a list composed of a number of dataframes.
#' @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 tag_id The ID of the tag - this will work with either a number (faster) or a tag name (slower) - note, the tag name has to be exact
#' @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_tags_get <- function(account_id,container_id,workspace_id,tag_id,token,verbose = FALSE) {
require(httr)
require(tidyverse)
require(purrr)
require(rlist)
token_status(token)
api_url <- tag_id_url(account_id,container_id,workspace_id,tag_id,token)
call <- easy_call('get',api_url,token)
call_content <- content(call,'parsed')
if (is_empty(single_tag$firingTriggerId) == FALSE) {
firing_triggers <- do.call('bind_rows',map(single_tag$firingTriggerId,as_tibble)) %>%
rename(firing_trigger_id = 1)
} else {
firing_triggers <- NULL
}
#set up the table for the blocking triggers
if (is_empty(single_tag$blockingTriggerId) == FALSE) {
blocking_triggers <- do.call('bind_rows',map(single_tag$blockingTriggerId,as_tibble)) %>%
rename(blocking_trigger_id = 1)
} else {
blocking_triggers <- NULL
}
settings <- tibble(index = i)
fields_to_set <- tibble()
parameters <- tibble()
params <- map(single_tag$parameter,paramsfunc)
for (i in 1:length(params)) {
y <- params[[i]]
if (nrow(y) == 1 & ncol(y) == 1) {
settings <- bind_cols(settings,y)
} else if (names(y)[[1]] == 'fields_to_set') {
fields_to_set <- bind_rows(fields_to_set,y)
} else {
parameters <- bind_rows(parameters,y)
}
}
settings <- settings %>%
select(-index)
call_content$firingTriggerId <- NULL
call_content$blockingTriggerId <- NULL
call_content$parameter <- NULL
call_content_values <- as_tibble(call_content) %>%
tidycols()
parameters <- parameters %>%
select(-dataset)
parameter_list <- list(tag_metadata = single_tag_values,
tag_settings = settings,
tag_fields_to_set = fields_to_set,
tag_parameters = parameters,
tag_triggers = firing_triggers,
tag_blockers = blocking_triggers)
verbose_calls(call,call_content,verbose)
return(parameter_list)
}
#' gtm_tags_delete
#'
#' Delete a specific tag 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 tag_id The ID of the tag - this will work with either a number (faster) or a tag name (slower) - note, the tag name has to be exact
#' @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_tags_delete <- function(account_id,container_id,workspace_id,tag_id,token,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='/')
api_url <- tag_id_url(account_id,container_id,workspace_id,tag_id,token)
call <- easy_call('delete',api_url,token)
call_content <- content(call,'parsed')
verbose_calls(call,call_content,verbose)
}
#' gtm_tags_create
#'
#' Create a new tag in the 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 tag_name The name of the tag
#' @param token An OAuth token object
#' @param fields_to_set A tibble of the fields to set, with the first column called 'field' and the second 'value'
#' @param ga_values The GA values that you want to pass, including the GA ID, the event category, event action, and event label
#' @param custom_dimensions The custom dimensions you want to pass, provided as a two-column tibble with the index and the value. Defaults to NULL
#' @param custom_metrics The custom metrics you want to pass, provided as a two-column tibble with the index and the value. Defaults to NULL
#' @param firing_triggers The IDs of the triggers you want to use to fire the tag, passed as an integer or a vector (for multiple triggers). Defaults to NULL
#' @param blocking_triggers The IDs of the triggers you want to use to stop the tag from firing, passed as an integer or a vector (for multiple triggers). Defaults to NULL
#' @export
gtm_tags_create <- function(account_id,container_id,workspace_id,tag_name,token,fields_to_set,ga_values,custom_dimensions = NULL,custom_metrics=NULL,firing_triggers=NULL,blocking_triggers=NULL) {
require(rlist)
require(httr)
require(jsonlite)
require(tidyverse)
require(purrr)
token_status(token)
create_url <- paste('https://www.googleapis.com/tagmanager/v2/accounts',account_id,'containers',container_id,'workspaces',workspace_id,'tags',sep='/')
#generate the tag details to send
tag_list <- list()
tag_list$name <- tag_name
tag_list$type <- 'ua'
tag_list$tagFiringOption = "oncePerEvent"
parameters <- list(list(type = "boolean", key = "nonInteraction", value = "false"),
list(type = "boolean", key = "overrideGaSettings", value = "true"))
track_type <- list(type = "template", key = "trackType", value = str_to_upper('track_event'))
#add the fields to set
f2s <- list(type = "list", key = "fieldsToSet")
fields_to_set <- fields_to_set %>%
rename(field = 1,
value = 2)
new_list <- list()
for (i in 1:nrow(fields_to_set)) {
x <- fields_to_set %>% slice(i)
z <- list(type = "map", map = list(list(type = "template",
key = "fieldName", value = x$field), list(
type = "template", key = "value", value = x$value)))
new_list <- rlist::list.append(new_list,z)
}
f2s$list <- new_list
#add the GA values
ga_values <- ga_values %>%
tidycols()
gaid <- list(type = "template", key = "gaSettings", value = quick_pull(ga_values,'ga_id'))
ec <- list(type = "template", key = "eventCategory", value = quick_pull(ga_values,'event_category'))
ea <- list(type = "template", key = "eventAction", value = quick_pull(ga_values,'event_action'))
el <- list(type = "template", key = "eventLabel", value = quick_pull(ga_values,'event_label'))
#add the dimensions if there are any
if (is_empty(custom_dimensions) == FALSE) {
dimensions <- list(type = "list", key = "dimension")
dims_list <- list()
for (i in 1:nrow(custom_dimensions)) {
x <- custom_dimensions %>%
slice(i)
z <- list(type = "map", map = list(list(type = "template",key = "index", value = quick_pull(x,'index')),
list(type = "template",key = "dimension", value = quick_pull(x,'value')))
)
dims_list <- list.append(dims_list,z)
}
dimensions$list <- dims_list
} else {
dimensions <- NULL
}
#add the custom metrics
if (is_empty(custom_metrics) == FALSE) {
metrics <- list(type = "list", key = "metric")
mets_list <- list()
for (i in 1:nrow(custom_metrics)) {
x <- custom_metrics %>%
slice(i)
z <- list(type = "map", map = list(list(type = "template",key = "index", value = quick_pull(x,'index')),
list(type = "template",key = "metric", value = quick_pull(x,'value')))
)
mets_list <- list.append(mets_list,z)
}
metrics$list <- mets_list
} else {
metrics <- NULL
}
parameters <- list.append(parameters,track_type,f2s,gaid,ec,ea,el,dimensions,metrics)
parameters <- parameters %>%
discard(is.null)
tag_list$parameter <- parameters
#add the triggers to fire
if (is_empty(firing_triggers) == FALSE) {
if (is.numeric(firing_triggers)==TRUE) {
tag_list$firingTriggerId <- as.list(firing_triggers)
} else {
#this is the option to pass triggers by name
temp <- gtm_triggers_list(account_id, container_id, workspace_id, token) %>%
filter(name %in% firing_triggers) %>%
distinct(trigger_id) %>%
pull(trigger_id)
tag_list$firingTriggerId <- as.list(temp)
}
}
#add the triggers to block
if (is_empty(blocking_triggers) == FALSE) {
if (is.numeric(blocking_triggers) == TRUE) {
tag_list$blockingTriggerId <- as.list(blocking_triggers)
} else {
temp <- gtm_triggers_list(account_id, container_id, workspace_id, token) %>%
filter(name %in% blocking_triggers) %>%
distinct(trigger_id) %>%
pull(trigger_id)
tag_list$blockingTriggerId <- as.list(temp)
}
}
call <- easy_call('post',create_url,token,tag_list)
print(call$status_code)
}
#' gtm_tags_update
#'
#' Update a tag in the container
#' @param account_id The ID of the account
#' @param container_id The ID of the container
#' @param workspace_id The ID of the container
#' @param tag_id The ID of the tag to be updated
#' @param tag_name The name of the tag
#' @param token An OAuth token object
#' @param fields_to_set A tibble of the fields to set, with the first column called 'field' and the second 'value'
#' @param ga_values The GA values that you want to pass, including the GA ID, the event category, event action, and event label
#' @param custom_dimensions The custom dimensions you want to pass, provided as a two-column tibble with the index and the value. Defaults to NULL
#' @param custom_metrics The custom metrics you want to pass, provided as a two-column tibble with the index and the value. Defaults to NULL
#' @param firing_triggers The IDs of the triggers you want to use to fire the tag, passed as an integer or a vector (for multiple triggers). Defaults to NULL
#' @param blocking_triggers The IDs of the triggers you want to use to stop the tag from firing, passed as an integer or a vector (for multiple triggers). Defaults to NULL
#' @export
gtm_tags_update <- function(account_id,container_id,workspace_id,tag_id,tag_name,token,fields_to_set,ga_values,custom_dimensions = NULL,custom_metrics=NULL,firing_triggers=NULL,blocking_triggers=NULL) {
require(rlist)
require(httr)
require(jsonlite)
require(tidyverse)
require(purrr)
token_status(token)
update_url <- paste('https://www.googleapis.com/tagmanager/v2/accounts',account_id,'containers',container_id,'workspaces',workspace_id,'tags',tag_id,sep='/')
#generate the tag details to send
tag_list <- list()
tag_list$name <- tag_name
tag_list$type <- 'ua'
tag_list$tagFiringOption = "oncePerEvent"
parameters <- list(list(type = "boolean", key = "nonInteraction", value = "false"),
list(type = "boolean", key = "overrideGaSettings", value = "true"))
track_type <- list(type = "template", key = "trackType", value = str_to_upper('track_event'))
#add the fields to set
f2s <- list(type = "list", key = "fieldsToSet")
fields_to_set <- fields_to_set %>%
rename(field = 1,
value = 2)
new_list <- list()
for (i in 1:nrow(fields_to_set)) {
x <- fields_to_set %>% slice(i)
z <- list(type = "map", map = list(list(type = "template",
key = "fieldName", value = x$field), list(
type = "template", key = "value", value = x$value)))
new_list <- rlist::list.append(new_list,z)
}
f2s$list <- new_list
#add the GA values
ga_values <- ga_values %>%
tidycols()
gaid <- list(type = "template", key = "gaSettings", value = quick_pull(ga_values,'ga_id'))
ec <- list(type = "template", key = "eventCategory", value = quick_pull(ga_values,'event_category'))
ea <- list(type = "template", key = "eventAction", value = quick_pull(ga_values,'event_action'))
el <- list(type = "template", key = "eventLabel", value = quick_pull(ga_values,'event_label'))
#add the dimensions if there are any
if (is_empty(custom_dimensions) == FALSE) {
dimensions <- list(type = "list", key = "dimension")
dims_list <- list()
for (i in 1:nrow(custom_dimensions)) {
x <- custom_dimensions %>%
slice(i)
z <- list(type = "map", map = list(list(type = "template",key = "index", value = quick_pull(x,'index')),
list(type = "template",key = "dimension", value = quick_pull(x,'value')))
)
dims_list <- list.append(dims_list,z)
}
dimensions$list <- dims_list
} else {
dimensions <- NULL
}
#add the custom metrics
if (is_empty(custom_metrics) == FALSE) {
metrics <- list(type = "list", key = "metric")
mets_list <- list()
for (i in 1:nrow(custom_metrics)) {
x <- custom_metrics %>%
slice(i)
z <- list(type = "map", map = list(list(type = "template",key = "index", value = quick_pull(x,'index')),
list(type = "template",key = "metric", value = quick_pull(x,'value')))
)
mets_list <- list.append(mets_list,z)
}
metrics$list <- mets_list
} else {
metrics <- NULL
}
parameters <- list.append(parameters,track_type,f2s,gaid,ec,ea,el,dimensions,metrics)
parameters <- parameters %>%
discard(is.null)
tag_list$parameter <- parameters
#add the triggers to fire
if (is_empty(firing_triggers) == FALSE) {
if (is.numeric(firing_triggers)==TRUE) {
tag_list$firingTriggerId <- as.list(firing_triggers)
} else {
#this is the option to pass triggers by name
temp <- gtm_triggers_list(account_id, container_id, workspace_id, token) %>%
filter(name %in% firing_triggers) %>%
distinct(trigger_id) %>%
pull(trigger_id)
tag_list$firingTriggerId <- as.list(temp)
}
}
#add the triggers to block
if (is_empty(blocking_triggers) == FALSE) {
if (is.numeric(blocking_triggers) == TRUE) {
tag_list$blockingTriggerId <- as.list(blocking_triggers)
} else {
temp <- gtm_triggers_list(account_id, container_id, workspace_id, token) %>%
filter(name %in% blocking_triggers) %>%
distinct(trigger_id) %>%
pull(trigger_id)
tag_list$blockingTriggerId <- as.list(temp)
}
}
call <- easy_call('put',update_url,token,tag_list)
print(call$status_code)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.