How to use Macros in Google Tag Manager

A use case for using macros in Google Tag Manager

By Rob Johnson

Sat May 17 2014

11 mins e-commerce

I’ve been using Google Tag Manager for a number of months and have been thoroughly enjoying deploying tag changes on my sites instantly without having wait for a production deploy. Also, having the power to select what tags fire on what pages is a huge plus for me. It removes the need for development resources and gives me greater control in the delivery of my tags; all of which are delivered asynchronously. Pretty smart.

One tool available in Google Tag Manager, that I’ve found extremely useful recently, are macros. Macros can mean and be used in a lot of different ways and so, initially, I was a little confused as to how macros could be used in Google Tag Manager.

Below is one of the many use cases that illustrates how you can use macros to minimize code on your site and deliver much greater functionality to your web applications.

Use Case: Deliver a unique offer to users who click through to a page via an email campaign

If you’re using Google Analytics and sending emails to customers. Chances are you’re already including utm_campaign in your email URL’s to track your campaigns. An example URL may be

https://example.com/example-product?&utm_source=example.com&utm_medium=email&utm_campaign=abandon_cart

To grab the value of the query key or parameter create a new macro in GTM with the macro type as URL and the component type as Query. Then enter in the query key which, in this example, is

utm_campaign

Macros in Google Tag Manager

This will then assign the value of utm_campaign from the URL, which in this case will be abandon_cart, to the macro

This macro value will now be available as a variable for code on any page where the parameter exists in the URL. Nifty.

Pass the macro into the code

We can then initiate a popup modal via javascript with a simple check

if ( == “abandon_cart”) {

// offer code for popup modal

}

There are lots of other use cases that include passing these variables into other tracking services etc. The great thing about GTM is that it enables you to be creative with minimal development knowledge.

Feel free to reach out via Twitter if you have any questions.