Manos Parzakonis

(Auto) Document your Google Tag Manager implementation

Contents

Intro

You might already noticed that I am a huge fan of documentation. The sweat spot I am a fool for is automation, and this is what I covered in the Create a Google Analytics implementation doc using R post. Today, we will generalize this into pulling the Google Tag Manager configuration and feeding it into a Google Sheet.

Obviously, this is only a subset of the things we can do with the Google Tag Manager API since practically we only work on a read.only mode. However, we can update, delete and create Google Tag Manager objects in a similar fashion.

Preparation

library(googleAuthR);library(googlesheets)## In order to push it to a Google Sheet
## Set the desired scopes for our problem(s)
options(googleAuthR.scopes.selected=c('https://www.googleapis.com/auth/tagmanager.delete.containers','https://www.googleapis.com/auth/tagmanager.edit.containers','https://www.googleapis.com/auth/tagmanager.edit.containerversions','https://www.googleapis.com/auth/tagmanager.manage.accounts','https://www.googleapis.com/auth/tagmanager.manage.users','https://www.googleapis.com/auth/tagmanager.publish','https://www.googleapis.com/auth/tagmanager.readonly'))## Now authenticate to Google
gar_auth(new_user=T)## Auth with `googlesheets` as well (since this can be a different account)
gs_auth()

Next, let’s define the functions that will perform the requests to the Google Tag Manager API. Note, that you can use the autoGoogleAPI to create an R package but it is fairly simple to just define the functions using the generic template provided in googleAuthR.

DISCLAIMER : I am a bit lazy to define a more elegant parsing of the f() in order to get more stuff out of the parameter, so we’ll have a list() within this column and we will process it when needed.

Execution

Now, we can use a for() loop to get all containers under an account into a separate Google Sheet. Since the example runs for the GTM of this blog the loop reduces to a single pass.

## We can get the accounts using the `accounts.list()`
## Sample Ids (originating from this blog)
accountId<-65691containerId<-71466time<-3;## A loop for generic use
for(acc_IdinaccountId){for(con_IdincontainerId){container.tags<-tags.list(acc_Id,con_Id)container.triggers<-triggers.list(acc_Id,con_Id)container.variables<-variables.list(acc_Id,con_Id)## Parameter is a `JSON` (so in `R` this is a `list`), that we will not use for now
# str(container.tags$tags$parameter)
## Objects of interest
tags<-container.tags$tagstriggers<-container.triggers$triggersvariables<-container.variables$variables## Register a new Google Sheet to pass the data
gs_new(paste("(Auto) GTM Implementation - ",acc_Id,"_",con_Id),verbose=TRUE)s_id<-gs_title(paste("(Auto) GTM Implementation - ",acc_Id,"_",con_Id),verbose=TRUE)yo.tags<-gs_ws_new(ss=s_id,ws_title="Tags",input=tags[,1:10],trim=TRUE)yo.triggers<-gs_ws_new(ss=s_id,ws_title="Triggers",input=triggers,trim=TRUE)yo.variables<-gs_ws_new(ss=s_id,ws_title="Variables",input=variables,trim=TRUE)## Get Google Sheet URL
# s_id$browser_url
# > https://docs.google.com/spreadsheets/d/1qibV3BSOtzSlI0vG6JC8xnzqnu6TnpFuxabaYN2vXeI/
}## This is used to cool off the Google Drive API calls...
## Since we only write to one Google Sheet we don't need it in this run.
# Sys.sleep(time)
}

End result

Now, we have a Google Sheet populated with the basics of the implementation that we can munge further to make it more presentable.