Javascript basics for Google Tag Manager



90% of the Javascript I use in Google Tag Manager comes down to 3 simple concepts. After building some pretty extensive tracking systems, I have noticed something interesting. Marketers often avoid Javascript in GTM like it is radioactive. But here is the truth: you do not need to become a developer to harness its power.
Most marketers hit these common frustrations. They cannot track a specific button. The form tracking is not detailed enough. Custom triggers are not working. They need a developer for everything. So to help you remove the frustration, I have distilled Javascript for Google Tag Manager into three building blocks that anyone can learn.
Finding Things with querySelector
Think of querySelector as Google search for your webpage. You tell it what to find, and it finds it.
1 document.querySelector('.pricing-button')
It is that simple. Just like you use a search bar to find content, 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 change anything in the codebase.
Watching for Actions with addEventListener
This is like having a security camera on your website. You tell it what to watch for, and it reports back when it happens.
1 addEventListener('click', function(e) { 2 // your tracking code here 3 })
Do not let the syntax intimidate you. This is just saying "watch for clicks and do something when they happen." Once you internalize this pattern, you can track form field interactions, monitor video engagement, capture scroll patterns, and log document downloads. All without touching the site's source code.
Sending Data with dataLayer.push
This is your message delivery system. Think of it as sending an email to GTM. You package up what happened and hand it off.
1 dataLayer.push({ 2 'event': 'user_action', 3 'action_type': 'engagement' 4 })
Once you push data to the dataLayer, GTM can pick it up and route it wherever it needs to go. Google Analytics, Facebook, your attribution platform, wherever. It is the universal adapter between what happens on your site and what your marketing stack needs to know about.
Why These Three Concepts Are All You Need
From my experience, these concepts have not changed in years. They work across all browsers. They are incredibly reliable. And you can learn them in an afternoon. The impact I have seen marketers create after learning these basics is significant. Developer dependencies drop dramatically. User journey data gets dramatically more detailed. Implementation of tracking ideas goes from weeks to hours.
Here is what I wish someone had told me as a marketer: JavaScript in GTM is not about becoming a programmer. It is about adding a powerful tool to your marketing toolkit. Find things, watch for actions, and send data. That is the whole game.
Related: Google Tag Manager Essentials: A Comprehensive Guide and Tools of the Trade.
