Skip to main content

Google Tag Manager auto-event tracking - useful Tags, Macros and Rules for Google Analytics events

Mark Rochefort25 November 20133 min read
Google Tag Manager auto-event tracking - useful Tags, Macros and Rules for Google Analytics events
			The recent introduction of <a href="http://analytics.blogspot.co.uk/2013/10/no-code-required-auto-event-tracking.html" target="_blank" rel="noopener noreferrer">auto-event tracking</a>&nbsp;for <a href="http://www.google.com/tagmanager/" target="_blank" rel="noopener noreferrer">Google Tag Manager</a>&nbsp;was a welcome piece of news for many of us keen to remove custom jQuery from within the container. But it quickly became apparent that, to make real use of the auto-event tracking feature, a few hoops needed to be jumped through - to get things just as one might expect. Over the last few weeks, we've started to repeat certain implementations - particularly for GA event tracking - and so I thought I'd share these here. The three common GA events being - outbound link tracking, file download tracking and form submission tracking.&nbsp;In putting together these, I came across &nbsp;a&nbsp;<a href="http://www.fourthsource.com/web-development/google-tag-manager-series-4-tips-tricks-setting-google-analytics-implementation-google-tag-manager-16156" target="_blank" rel="noopener noreferrer">similar recent post from Rachel Sweeney</a>&nbsp;(thanks Rachel!) - you will notice some overlap:

Macros

Macro Name: getDomain Macro Type: URL Component: hostname (tick strip "www") What this does:  Returns hostname without www (there's already a default one called "url hostname" - leave this as it will also come in handy, although you might want to rename it to getFullDomain or something)

Macro Name: getFilename
Macro Type: Custom JavaScript
Custom JavaScript:

function() { var fileName = {{gtm.elementUrl}}.split('?').shift().split('/').pop(); return fileName.toLowerCase(); }

What this does:  Removes query strings, searches for the last slash, keeps everything after it. Returns a clean document filename and extension.

Macro Name: getFiletype
Macro Type: Custom JavaScript
Custom JavaScript:

function() { var extension = (/[.]/.exec({{getFilename}})) ? /[^.]+$/.exec({{getFilename}}) : undefined; return extension; }

What this does:  Uses our getFilename macro and some regex to grab file extension and return.

Macro Name: isOutboundUrl
Macro Type: Custom JavaScript
Custom JavaScript:

function(){ return ( ({{gtm.elementUrl}}.match(/^https?\:/i)) && ({{gtm.elementUrl}}.indexOf("{{getDomain}}")<0) ); }

What this does:  Returns true if link starts with http or https AND link does not contain current hostname (sub-domains included) - otherwise returns false.

Macro Name: gtm.elementUrl
Macro Type: Auto-Event Variable
Variable Type:
Element URL
What this does:  The value is determined by reading the 'gtm.elementUrl' key from the data layer. If populated by an Auto-Event, the result will be the 'href' or 'action' attribute of the DOM element that triggered the event, depending on the type of element.

Macro Name: gtm.elementId
Macro Type: Auto-Event Variable
Variable Type:
Element ID
What this does:  The value is determined by reading the 'gtm.elementId' key from the data layer. If populated by an Auto-Event, the result will be the 'id' attribute of the DOM element that triggered the event.

RULES

Rule Name: event is form submit Conditions:{{event}} equals gtm.formSubmit

Rule Name: event is link clicked to download file
Conditions:
{{gtm.elementUrl}} matches RegEx (ignore case) .(txt|svg|png|jpg|eps|vsd|mov|avi|wmv|zip|rar|exe|pdf|doc*|xls*|ppt*|mp3)$
AND
{{event}} equals gtm.linkClick

Rule Name: event is link clicked to outbound
Conditions:
{{event}} equals gtm.linkClick
AND
{{isOutboundUrl}} equals true

TAGS

Tag name: GA auto-events - Form submitted Tag type: Google Analytics Track Type: Event - category: Form- action: submitted: {{gtm.elementId}}- label: on: {{url}}Firing rules: event is form submit

Tag name: GA auto-events - Download links
Tag type: Google Analytics
Track Type: Event

  • category: Download
  • action: type: {{getFiletype}}
  • label: file: {{getFilename}}
    Firing rules: event is link clicked to download file
    Notes: bug in GTM currently prevent these macros from being used on their own in event tracking fields. Adding a string prefix (like “type: “) seems to fix that

Tag name: GA auto-events - Outbound links
Tag type: Google Analytics
Track Type: Event

  • category: Outbound
  • action: link: {{gtm.elementUrl}}
  • label: on: {{url}}
    Firing rules: event is link clicked to outbound

Suggested content

Measurelab awarded Google Cloud Marketing Analytics Specialisation

At the start of the year, if you’d asked us whether Measurelab would be standing shoulder to shoulder with Europe’s biggest consultancies by September, we would've been surprised. Not because we don't believe in ourselves, but because these things feel so distant - until suddenly, they’re not. So, here it is: we’ve been awarded the Marketing Analytics Services Partner Specialisation in Google Cloud Partner Advantage. What’s the big deal? In Google’s own words (with the obligatory Zs): “Spec

Will Hayes11 Sept 2025

BigQuery AI.GENERATE tutorial: turn SQL queries into AI-powered insights

BigQuery just got a major upgrade, you can now plug directly into Vertex AI using the new AI.GENERATE function. Translation: your analytics data and generative AI are now best friends, and they’re hanging out right inside SQL. That opens up a whole world of new analysis options for GA4 data, but it also raises some questions: * How do you actually set it up? * What’s it good for (and when should you avoid it)? * Why would you batch the query? Let’s walk through it step by step. Step 1: H

Katie Kaczmarek3 Sept 2025

How to start forecasting in BigQuery with zero training

If you’d told me five years ago that I’d be forecasting product demand using a model trained on 100 billion time points… without writing a single line of ML code… I probably would’ve asked how many coffees you’d had that day ☕️ But its a brand new world. And it’s possible. Let me explain What is TimesFM? TimesFM is a new foundation model from Google, built specifically for time-series forecasting. Think of it like GPT for time, instead of predicting the next word in a sentence, it predicts t

Katie Kaczmarek14 Jul 2025