Leadtosale

Roll out web analytics at scale pt 2: more complicated triggers and tags in Google Tag Manager

Background

This blog continues our series on rolling out web analytics at scale using Google Apps Script. If you haven’t seen part one, go back and read it! The aim of part 2 is to show you some more complicated trigger and tag examples you will likely need when rolling out your bulk containers.

More complicated triggers

Custom event

This example is a custom event trigger that fires on all dataLayer events called ‘someEvent’.

var trigger1 = call(function() {
return TagManager.Accounts.Containers.Workspaces.Triggers.create({
'name': 'Custom Event Something',
'type': 'customEvent',
'customEventFilter': [{
"type": "EQUALS",
"parameter": [{
"type": "TEMPLATE",
"key": "arg0",
"value": "{{_event}}”
                },
                {
"type": "TEMPLATE",
"key": "arg1",
"value": "someEvent"
}
            ]

          }]
        },
        workspacePath)
  });

Link clicks

This example is an email link click trigger.

var trigger2 = call(function() {
return TagManager.Accounts.Containers.Workspaces.Triggers.create({
'name': 'Link Click',
'type': 'linkClick',
'filter': [
{
'type': 'contains',
'parameter': [
{
'type': 'template',
'key': 'arg0',
'value': '{{Click URL}}’
              },
              {
'type': 'template',
'key': 'arg1',
'value': 'mailto:'
}
            ]
          }
      ]
          },
          workspacePath)
      });

More complicated tags

Google Analytics event tags

This example is an event tag that sends the category, action and label as ‘blah’ and a value of 0.

var tag1 = call(function() {
return TagManager.Accounts.Containers.Workspaces.Tags.create({
'name': 'UA Event Tag',
'type': 'ua',
'liveOnly': false,
'parameter': [
{
'type': 'template',
'key': 'trackingId',
'value': params.analyticsPropertyId
},

          {
'type': 'template',
'key': 'trackType',
'value': 'TRACK_EVENT'
},
          {
"type": "template",
"key": "eventCategory",
"value": "blah"
},
          {
"type": "template",
"key": "eventAction",
"value": "blah"
},
          {
"type": "template",
"key": "eventLabel",
"value": "blah"
},
          {
"type": "template",
"key": "eventValue",
"value": "0"
},
          {
"key": "fieldsToSet",
"type": "list",
"list": [{
"type": "map",
"map": [{
"type": "template",
"key": "fieldName",
"value": "cookieDomain"
},
                  {
"type": "template",
"key": "value",
"value": "auto"
}
                ]
              }]
          }
        ],
        ‘firingTriggerId’: [trigger3.triggerId]
      },
      workspacePath)
});

Custom HTML tags

Custom HTML tags are a bit of a pain, especially as Apps Script doesn’t seem to recognise ` as a multi line string. Also, the tags also often have single quotes instead of double quotes within them, meaning Apps Script thinks the string is starting or finishing there. My workaround would be put the tag in a multi line string in the console, and a function to make it one line, with line breaks as text and only use double quotes.

function addSlashes( str ) {
return (str + '').replace(/'/g, '\"').replace(/\n/g, '\\n');
}

var newTag = addSlashes(`


`);

console.log(newTag);

So now you’ve pasted this into a (single quote) string, it should like something like this.

var tagValue2 = ‘\n

\n

\n\n’;

        var tag2 = call(function() {
return TagManager.Accounts.Containers.Workspaces.Tags.create({
'name': 'Facebook Pixel',
'type': 'html',
'liveOnly': false,
'parameter': [{
'key': 'html',
'type': 'template',
'value': tagValue1
}, ],
                ‘firingTriggerId’: [trigger1.triggerId, trigger2.triggerId]
              },
              workspacePath)
        });

Hopefully someone who is actually a developer has a better way to handle this.

Last words & up next

Generally these will cover most tracking scenarios – event bubbling issues usually stop the form submit trigger working properly and you have to rely on custom HTML tags.
If you’re ever struggling with creating certain tags or triggers, check out the way they’re constructed by building them in the UI and exporting the container.

I hope you’ve found this useful – our next post will cover creating and configuring Google Analytics properties automatically.

Commentary

There are no comments on this entry.

Comment as a guest Required Fields ✱

What do you want to be called?
How do we contact you?
Where are you commenting from?
Do you have a website to share?
Please keep it kind, brief and courteous.
Extra stuff we can do!
Supermetrics