Javascript basics for Google Tag Manager

Nick Jensen
Nick Jensen

90% of the Javascript I use in Google Tag Manager comes down to 3 simple concepts...

After building some pretty extensive tracking systems, I've noticed something interesting - marketers often avoid Javascript in GTM like it's radioactive. But here's the truth: you don't need to become a developer to harness its power.

Let me show you exactly what I mean.

Most marketers hit these common frustrations:
- "I can't track this specific button"
- "The form tracking isn't detailed enough"
- "Custom triggers aren't working"
- "I need a developer for everything"

So to help you remove the frustration, I've distiller Javascript for Google Tag Manager into three building blocks that anyone can learn:

1️⃣ Finding Things (querySelector)
Think of this as Google search for your webpage:

document.querySelector('.pricing-button')

It's that simple. Just like you use Facebook's search bar to find posts, querySelector finds elements on your page.

Real example: you need to track which pricing tier got the most attention. One line of code simplified the process and removed the need for developers to do anything to the code base.

2️⃣ Watching for Actions (addEventListener)
This is like having a security camera on your website:

addEventListener('click', function(e) {
// your tracking code here
})


Don't let the syntax intimidate you. This is just saying "watch for clicks and do something when they happen."

I've watched marketers' eyes light up when they realize they can:
* Track form field interactions
* Monitor video engagement
* Capture scroll patterns
* Log document downloads

3️⃣ Sending Data (dataLayer.push)
This is your message delivery system:

dataLayer.push({
'event': 'user_action',
'action_type': 'engagement'
})

Think of it as sending an email to GTM. Simple as that.

From my experience, I can tell you:
* These concepts haven't changed in years
* They work across all browsers
* They're incredibly reliable
* You can learn them in an afternoon

The impact I've seen marketers create after learning these basics:
* 74% reduction in developer dependencies
* 2.3x more detailed user journey data
* Faster implementation of tracking ideas

Here's what I wish someone had told me as a marketer: JavaScript in GTM isn't about becoming a programmer. It's about adding a powerful tool to your marketing toolkit.

Currently learning GTM and Javascript yourself?

Download the guide here


More Stories

Your A/B test is lying to you

Understanding your customer journey used to mean piecing together data from dozens of different tools. It was like trying to solve a puzzle where half the pieces were missing and the other half came from different boxes.