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

Working with some complex Tag Manager tags and triggers in Google Apps Script? Here are some pre-built examples to work from.

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.

Click here for the first code snippet.

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

Click here for the second code snippet.

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.

Comments

There are no comments on this entry.