Share on facebook
Share on twitter
Share on linkedin
Share on email

Google Tag Manager JavaScript Variables Are Not So Scary

Google Tag Manager JavaScript Variables Are Not So Scary

Google Tag Manager (GTM) has a lot of out-of-the-box variables you can use to make tracking easier. You can grab CSS from the DOM, you can use a pre-existing data layer value, a YouTube video snippet, a scroll depth, or even customize your own situation on your website. Think of a variable as a Lego block: you can use it to summon any part of your website. 

There are a lot of user-defined GTM variables, including one called the (Global) JavaScript Variable. It’s commonly skipped, looked-at-confusedly, or just ignored. Not a lot of people know how to make it work for their tracking needs.

GTM >> Variables >> User-Defined Variables: New >> (Variable Configuration)

This particular variable will give you a VERY simple interface. There is only one field staring back at you.

So what do you do with this?

What is a Global Javascript Variable?

A Global JavaScript Variable can be a very short or a very long string (or path) of breadcrumbs to indicate a place on the page. Much like traversing the DOM in HTML. The artwork is similar to finding and illustrating CSS Selectors. It can be a couple of characters, or a long breadcrumb path with Hansel and Gretel’s phone number on it (work with me here…).

Where do I find a ‘Global Variable Name’?

You might’ve guessed, but we’re going to find this nugget of information by inspecting the page. It’s helpful if you highlight or hover over the selection of the page you want to target. Open the debugger however your operating system allows (such as right click and inspect page).

Within the debugger, navigate to the “Console” portion:

Scroll to the bottom of the Console, and type “Window” and then hit enter (you may have to hit ‘enter’ twice).

Click the arrow to the left of the word “Window”. This will drop-down everything we have to work with, and show you all of the good stuff.

How do I grab the value?

From here, any value you find (illustrated below in red) can be used as a GTM JS Variable. To capture the ‘Global Variable Name’ in a simple manner, right-click the value location you need. You can then ‘copy property path’ to your clipboard. I recommend pasting it into a Notepad or any sort of ASCII text file processor—this way you can examine the path and make edits if necessary (see below).

Example of grabbing the Global JS Path:

In this case, our “Global Variable Name value” would be copied to our clipboard, and read “document.URL”. 

This is the input you use in GTM:

This will produce a variable value of “http://infotrust.com/” —this value will appear exactly as you see it in red inside of the developer console.

NOTE: Occasionally, your path will have brackets around a number in the string. This is technically correct syntax; however, GTM does not recognize it. For example, if you copy the property path, and it reads “document.activeElement[1]this.that” you would simply remove the brackets, replace them with a period, and your path would read “document.activeElement.1.this.that

How can I use these values?

I’ll be releasing a blog soon on how to use Global JS Variables to do robust tracking. As an example, have you ever wanted to capture (in one click) a navigation selection, the main nav value, and the subnav value all at once? JS Vars can give you the navigation a user clicks, as well as the parent navigation, and even a parent above or child below that. You can even easily target sibling values as well (Hint: it uses a variable on the DOM called “Active Element”—I have found it to be much more reliable than using CSS Selectors for this type of tracking).

We’ll explore this next time and post a link to help you out! Happy tracking, everyone.

Have questions about your data?

The InfoTrust team of data analysts and consultants are here to help.
Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on email
Email

Other Articles You Will Enjoy

Google Analytics 4 Update: February 2022 – InfoTrust’s Strategic Approach & Recommendations

Google Analytics 4 Update: February 2022 – InfoTrust’s Strategic Approach & Recommendations

Introduction Google Analytics 4 (GA4) is a new iteration not only of Google Analytics (GA) as a tool, but also as a way of…

Top Things to Know from InfoTrust + Google’s Virtual Crypto, Finance, and Insurance Summit

Top Things to Know from InfoTrust + Google’s Virtual Crypto, Finance, and Insurance Summit

Financial services make up one of the economy’s most important and influential sectors today and the need for established data governance and analytics practices…

Why Your Healthcare Org Should Upgrade to Google Analytics 4 Right Now

Why Your Healthcare Org Should Upgrade to Google Analytics 4 Right Now

You probably saw the news, but in case you’ve been blissfully unaware, Google just announced that their beloved Universal Analytics (UA) will sunset on…

What I Learned about Digital Analytics from Buying a House

What I Learned about Digital Analytics from Buying a House

I honestly wasn’t sure if I could even buy a house, especially since I didn’t know where to begin—but to my surprise, my career…

3 Reasons You Can’t Trust Automatic Tracking in Google Analytics 4

3 Reasons You Can’t Trust Automatic Tracking in Google Analytics 4

Google Analytics 4 (GA4) has added many new features. One of the most important is called “Enhanced Measurement”. This will automatically track offsite links,…

Current Version of Google Analytics (Universal Analytics) to Sunset in 2023 | What You Need to Know

Current Version of Google Analytics (Universal Analytics) to Sunset in 2023 | What You Need to Know

Summary Google recently announced that Universal Analytics (UA; the current iteration of Google Analytics) will stop processing data in the second half of 2023,…

5 Benefits of Using Google Firebase

5 Benefits of Using Google Firebase

What Is Firebase? Firebase is Google’s Backend-as-a-Service (BAAS) solution for mobile app development. Originally, it was developed and launched in 2011 by Firebase Inc….

Back to Basics: Audit and Implementation

Back to Basics: Audit and Implementation

Wondering what an audit and implementation is or why you should consider conducting one for your organization? Today, we’re going back to the basics…

Google Analytics 4 Update: February 2022 (Part 2) – InfoTrust’s Strategic Recommendation: Transitioning to Google Analytics 4

Google Analytics 4 Update: February 2022 (Part 2) – InfoTrust’s Strategic Recommendation: Transitioning to Google Analytics 4

As a follow-up to our Google Analytics 4 (GA4) February 2022 update, in which we cover some key features of GA4 and address whether…

Get Your Assessment

  • This field is for validation purposes and should be left unchanged.

Talk To Us

Talk To Us

Receive Book Updates

Fill out this form to receive email announcements about Crawl, Walk, Run: Advancing Analytics Maturity with Google Marketing Platform. This includes pre-sale dates, official publishing dates, and more.

Our website uses cookies and may collect user information to provide a good experience. Read our Privacy Policy here.

Leave Us A Review

Leave a review and let us know how we’re doing. Only actual clients, please.