How to Pull Data Relative to the Clicked Element in Google Tag Manager?

Do you know how to get information relative to a click event in Google Tag Manager? With a relative click variable, you will be able to pull in information relative to the interaction you tracked. 

This can give an edge to your analytics with the additional information that you collect. For example, in an eCommerce shop, you can get details such as product name and product price relative to the click on a product page.

In this guide, we will learn the steps to set up a relative click variable using a Custom JavaScript Variable in Google Tag Manager.

An overview of what we will cover in this guide:

So let’s jump in!

What is Data “Relative to the Click Element?”

Data relative to the click element is any data on the web page that is not captured with click variables.

So, welcome back to our demo shop. On this website, I already have Google Tag Manager installed

Now, we want to create a variable relative to a click. So what does this mean? Well, let’s take an example where you click the Add to Cart button on the product page. 

So we already have our click trigger installed and we can take a look at the click event within our preview mode.

When we look at the Variables, we can see the ones that got filled. 

Here, we have our Click Classes and Click Element, which got filled. But, the Click ID and Click Target didn’t get filled. Also, we can see the Click Text is Add to cart

Click Variables of a Click event in Google Tag Manager

Now, this is sufficient if we just want to track the button. But what if we want to track information relative to this button click. So for example, we wanted to track the product price, product name, or the image URL relative to the Add to cart click.

Even if we go to another product and have a completely different set of variables, we should be able to fetch these new variables.

We can do this with a Custom JavaScript variable. So let me show you how to set this up for our product name i.e. we should be able to pull the product name relative to the Add to cart click.

Prerequisites to Pull Data Relative to the Clicked Element

Now as a prerequisite, you will need to have a trigger installed which is a simple click trigger. Here, the Event Type will be All Elements

Let us see how to set this up. First, to create a new trigger, we need to click on the New button. Next, we’ll name the trigger click. 

Then to configure the trigger, click under Trigger Configuration and Choose trigger type as All Elements. You can choose either Just Links or All Elements

Choosing trigger type as All Elements in Google Tag Manager

Under the option This trigger fires on, choose All Clicks without any filters attached to it.

So this will deploy the listener functionality of our trigger. 

Choosing the option to fire the trigger on All Clicks in Google Tag Manager

And then click on the Save button to set up the trigger. This will push data when we click anywhere on the webpage, into the preview, and to the data layer. Now that Google Tag Manager is registering clicks, we need to set up a variable.

As already mentioned, this will be a Custom JavaScript variable. So realistically, you should know a bit about JavaScript and CSS in order to configure this. But for this guide, we’ll show you the step-by-step process. 

The first step is to reload our page and click on the Add to cart button. So we will see a Click Element in the Events menu of our preview mode.

Pulling Data Elements Using Google Developer Tools

Now, this click gets transferred to the data layer along with certain elements. What we want to look at is the GTM element which is an object in the HTML button element. 

So what does this mean? And how can we see this? For this, we need to open up our Developer Tools.

So we go to the Chrome menu and then to More ToolsDeveloper Tools

Opening the Developer Tools of Google Chrome

This takes us to the Elements panel. But, we need to go to the Console panel. We’ll type in the command in the Console pane to see what is stored in this gtm.element.

Firstly, we’ll type google_tag_manager to access the GTM JavaScript object. And then in square brackets with quotation marks, we’ll type in our GTM Container ID.

You can find the GTM Container ID in your preview and debug mode as well. If you already have it stored it will be auto-filled for you in your Developer Tools Console

And then we’ll attach a dot and access the data layer. Here, we type dataLayer (with a capital L). 

Then we type another dot and we’ll get a key from our data layer. So the command will be get(). Inside the parentheses with quotation marks we’ll put in the key which is gtm.element

When we press the enter key we get a return statement.

Commands in the Console panel of Developers Tools to access the data stored in gtm.element

And when we hover over this return statement, we can see on our webpage in the background that our Add to cart button is highlighted. So, this is the element that the trigger recognizes once you click the button. 

The Add to cart button gets highlighted when we hover over the return statement in Developer Tools

What we’ll do now is climb up the DOM tree.

Now, what does this mean? First, we’ll right-click and select Reveal in Elements panel

Right-clicking in the Console panel to select the Reveal in Elements panel option

This takes us to the Elements panel where we can see the DOM tree. What we will do now is climb up the DOM tree and get to our h1 element which is the product_title

In our case, the value of this element is Happy Ninja. This is what we want to pull out. 

Accessing the h1 element of the webpage with the Developer Tools

So how can we do this?

Well, let’s go back to the Console panel. Here, we will enter a command that will climb up the tree i.e. .parentElement, and press the Enter key. So we will get the next parent element to the clicked element. 

Here the next parent element is the form class.

.parentElement command to fetch the next element of the clicked element in the Developer Tools

Now we see that our product title is not yet highlighted because the parent element is actually not within the tree structure that we just climbed. 

So we’ll climb to the next parent element, which would be summary entry.

Next parent element in the DOM tree structure, summary entry-summary

We can enter the previous command again by pressing the up arrow key. So we don’t have to type this again. And then we add .parentElement and press the Enter key.

So we are now on the summary entry class. This encapsulates our h1 as well. 

The next parent element is the summary class in the DOM tree structure

Again, we will right-click and select Reveal in Elements panel. We can see in the DOM tree that we need to search through the children of this parent element to find our product_title.

We can see h1 and product_title as a class. 

Product title as an h1 class in the DOM tree structure

So we can simply choose the CSS selector to select this item. We will go over to the Console panel again and press the up arrow. 

This time we will enter the querySelector() command. And in parentheses and quotation marks, we can put in our CSS selector. 

In our case, it would be h1.product_title.

Let’s try this. When we press the Enter key we get our result back. And we can see that we are now on the right element. 

Reaching the clicked element in the DOM tree structure

And now we want to pull the text that is within this element. We can do this by adding the command .innertext. And then press Enter. 

This will return the product title, i.e. Happy Ninja in our case.

The Console returns the text of the product title that is within the element

Setting Up a Custom Javascript Variable

So we have now completed our command, and we can copy it into a Custom JavaScript variable. 

To do this, we’ll go under User-Defined Variables, and click on the New button to create a new variable. We’ll name it cjs-Product Title.

Now, click under Variable Configuration and Choose variable type as Custom JavaScript.

Choosing variable type as Custom JavaScript in Google Tag Manager

It’s important to know that Custom JavaScript needs to have a function that returns a value.

A Custom JavaScript variable must return a value

So we will write a function that returns a value. And then we can copy and paste our command from the developer console and click on the Save button.

Copying the command from the developer Console and then Save the variable

Testing the Relative Click Variable

Let’s Refresh our preview and debug mode. 

Refreshing the preview and debug mode in Google Tag Manager

We will then go back to our webpage, reload it and click on the Add to cart button. When we do this, we can see that the cjs-Product Title is Happy Ninja in the Variables menu. 

cjs-Product Title is Happy Ninja under the Variables menu of Google Tag Manager preview mode

Now, let’s try this out on a different product. So we will go to the next product and click on the Add to cart button. But under the Variables menu, the cjs-Product Title is undefined.

cjs-Product Title is undefined under the Variables menu of Google Tag Manager preview mode

Let’s see why this is undefined.

Debugging the Relative Click Variable

This can happen sometimes if the HTML markup has changed. It may also happen if there are some more elements in the product, or if it’s marked up differently. So let’s see how to debug this. 

First, we need to enter our last command with the up arrow key and press Enter. This returns an error warning.

Cannot read property ‘innerText’ of null error in the Console panel of developer tools

So let’s first get rid of our innerText HTML command. And when we press Enter it returns a null value. 

This means we can’t fetch anything from h1 as well. So let’s move further and delete h1.product_title

This takes us to our second parent element. But this element doesn’t encapsulate our product title either. So we will go back further and remove the second .parentElement command.

Okay, we are now at the Add to cart element which is our clicked element. 

So we need to put in another parent element and keep adding further to see if this encapsulates our product title. 

And once we find it we can again enter .querySelector and .h1.product_title.

Alright, so here we go. Let’s fetch the .innerText of the product title and press Enter. 

The Console returns the inner text of the product title in developer tools

So let’s try this out again. We will copy this command and paste it into the Variable Configuration of Google Tag Manager.

Pasting the command from the developer Console and then Save the variable

We will then Refresh our preview and debug mode and reload our webpage. 

On this page, let’s click on the Add to cart button and we can see that the cjs-Product Title variable under the Variables menu is ‘Ship Your Idea’

cjs-Product Title is Ship Your Idea under the Variables menu of Google Tag Manager preview mode

Hence, this is correct. Now, let’s go back to our old product, add it to the cart and we should be able to see the cjs-Product Title as Happy Ninja

Similarly, we can check it for other products as well.

Thus, we can say that we have successfully adjusted our relative click variable to pull out the correct product name relative to the click on the webpage. 

You can use this technique in different circumstances. You just need to know how you can pull out the right values from the DOM tree using .parentElement and .queryselector. 

Now that we have this variable setup, we can use it in our triggers or also in Tag deployments directly inside of customization Tags like the Facebook Pixel or the Google Analytics event Tag. 

And I’m sure you will be able to set this up by yourself. 

Now, in the end, don’t forget to Submit the version and give it a Version Name so that the changes can go live on your website. Then, this tracking will be deployed to all your users.

Submit the version to save all changes in Google Tag Manager

Summary

This is how you can set up relative click variables. A relative click variable will give you a more precise picture of user behaviour on your website.

And if you’re interested you can also set it up with scroll tracking or element visibility trigger. 

While this is done using a Custom JavaScript variable, we also have a guide on the Top 10 Google Tag Manager Variables. And if you need help to get started on Google Tag Manager, you can sign up for our free beginners’ guide:

Let us know in the comments below if this is something that you use often. Where might you use this technique on your own website?

JOIN US!

Master Data & Analytics with Measuremasters

Exclusive Courses & Workshops | Ongoing Troubleshooting | Support Resources, Tools & much more
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
eddie
eddie
7 months ago

what if I have more than one title, lets say in a footer

now it's time to

Start measuring like a master

Itching to jump into the world of MeasureMasters? This is what you have to look forward to.