Skip to content

Instantly share code, notes, and snippets.

@mstraughan86
Last active September 8, 2017 03:54
Show Gist options
  • Save mstraughan86/41139268e4fed8814e981b719bcec67d to your computer and use it in GitHub Desktop.
Save mstraughan86/41139268e4fed8814e981b719bcec67d to your computer and use it in GitHub Desktop.
OAuth 2.0 Playground - GTM Scripts
- Temporary development location, simply copy and paste the stuff you need to do for the moment.
- Eventual plan is to have a master script to copy into g.spreadsheet and have all commands in a menu.
https://developers.google.com/oauthplayground/
- I find these in the url when I am logged in. Don't know where else to look.
Account ID = *****
Container ID = *****
//////////
Create Variables:
https://developers.google.com/tag-manager/api/v1/reference/accounts/containers/variables/create
POST https://www.googleapis.com/tagmanager/v1/accounts/*****/containers/*****/variables
{
"name": "test_name2", //REPLACE
"type": "v",
"parameter": [
{
"type": "integer",
"key": "dataLayerVersion",
"value": "2"
},
{
"type": "boolean",
"key": "setDefaultValue",
"value": "false"
},
{
"type": "template",
"key": "name",
"value": "test_name_inside2" //REPLACE
}
]
}
//////////
// Remove Variables:
https://developers.google.com/tag-manager/api/v1/reference/accounts/containers/variables/delete
https://developers.google.com/tag-manager/api/v1/reference/accounts/containers/variables/list
// Ok, so we need a list of IDs, right?
GET https://www.googleapis.com/tagmanager/v1/accounts/*****/containers/*****/variables
// Script to get the ID's into CSV so we can actually read this:
var fs = require('fs');
var response = {}; // Paste response into here.
var array = response.variables;
var top = "\"GTM Variable Name\",\"ID\""+"\n";
var csv = top;
array.forEach((row)=>{
var name = "\""+row.name+"\"";
var id = "\""+row.variableId+"\"";
csv = csv + name+','+id+'\n';
})
fs.writeFile("gtm-var-ids.csv", csv, function(err) {
if(err) {
return console.log(err);
}
});
// Open up the CSV into a spreadsheet application and find out which IDs you want to delete.
// You must delete them one by one.
DELETE https://www.googleapis.com/tagmanager/v1/accounts/****/containers/****/variables/****
// Ran into some problems,
// You cannot delete a variable that has a reference in a TRIGGER or a TAG.
// We must clean up TAGS first, then TRIGGERS, then VARIABLES.
// OOPS! Learn the hard way.
//////////
Create Triggers:
https://developers.google.com/tag-manager/api/v1/reference/accounts/containers/triggers/create
POST https://www.googleapis.com/tagmanager/v1/accounts/***/containers/***/triggers
{
"name": "Click - Login", // Replace
"type": "click",
"filter": [
{
"type": "cssSelector",
"parameter": [
{
"type": "template",
"key": "arg0",
"value": "{{Click Element}}"
},
{
"type": "template",
"key": "arg1",
"value": ".gtme-h-login" // Replace
}
]
}
]
}
//////////
Create Tags:
https://developers.google.com/tag-manager/api/v1/reference/accounts/containers/tags/create
POST https://www.googleapis.com/tagmanager/v1/accounts/***/containers/***/tags
{
"name": "Test - Header - Logo", // Replace
"type": "ua",
"firingTriggerId": [
"25" // Replace
],
"liveOnly": false,
"parameter": [
{
"type": "boolean",
"key": "nonInteraction",
"value": "false"
},
{
"type": "boolean",
"key": "overrideGaSettings",
"value": "true"
},
{
"type": "template",
"key": "eventCategory",
"value": "Header" // Replace
},
{
"type": "template",
"key": "trackType",
"value": "TRACK_EVENT"
},
{
"type": "template",
"key": "eventAction",
"value": "Navigation" // Replace
},
{
"type": "template",
"key": "eventLabel",
"value": "Logo" // Replace
},
{
"type": "template",
"key": "trackingId",
"value": "{{gaProperty}}"
}
],
"tagFiringOption": "oncePerEvent"
}
@mstraughan86
Copy link
Author

Just an FYI to enable Built-In Variables:
GTM:
Variables
Built-In Variables
Configure
Select "Click Element"
Save!

Now I can use "Click Elements"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment