pathfinder digital

Technical Best Practices for GTM Tracking

28 May 2025 | Johari Lanng

Let's get practical. If you're building a website and you're not thinking about how you'll track what people are doing on it - clicks, form submissions, cart actions, etc. - you're essentially creating a very attractive mystery box. It's like opening a shop and never checking the till. Did anyone buy anything? Did they even look at the shelves? Who knows?

So, let's address that. Let's talk about the technical steps you (or your developer) can take at the very beginning of a website build that will make Google Tag Manager (GTM) tracking much easier down the line. Yes, this will save you hours of pain, debugging, awkward workarounds, and tracking setups that only function if the stars align.

Load the HTML with All the IDs You Need

Let's start with the basics. When you're building your HTML, add IDs and data attributes to the elements you know you'll want to track. Do not rely on CSS selectors like .fancy-button > div:nth-child(2). That approach is fragile.

Here's why:

DO THIS INSTEAD:

<button id="cta-subscribe" data-action="subscribe-newsletter">
    Subscribe
</button>

Now you can simply instruct GTM: "When someone clicks the element with ID cta-subscribe, fire an event." Easy, fast, and reliable.

Also, use data-* attributes to help organize tracking:

<a href="#" data-gtm-click="main-nav-link" data-label="pricing">
    Pricing
</a>

That's significantly easier than decoding the complex CSS structure of your header menu.

Keep Forms Basic

Fancy forms with loaders, animations, and auto-updating fields may look impressive. However, do not compromise usability and tracking for aesthetics.

Here's the key rule: Ensure your forms are standard HTML <form> elements that submit data in the traditional manner.

Why?

DO THIS:

<form id="contact-form" action="/submit" method="POST">
  <input type="text" name="name" required>
  <input type="email" name="email" required>
  <button type="submit">Send</button>
</form>

AVOID THIS:

$('.form').on('submit', function(e) {
  e.preventDefault();
  sendFormWithMagicAjax();
});

If you absolutely must use JavaScript (for validation, AJAX, etc.), at least include a custom event or dataLayer.push() when the form is submitted so GTM can recognize it.

Avoid SPA Frameworks Like the Plague

This may be controversial, but it is important: If your priority is marketing, tracking, and GTM functionality - avoid Single Page Applications (SPAs).

React is popular. Vue is elegant. Angular... has its fans. But SPAs alter how websites behave. GTM expects full page loads. SPAs rely on JavaScript to change content dynamically without reloading the page.

Why SPAs complicate tracking:

You will spend significantly more time configuring GTM to work properly.
And what do you get in return? A slight increase in page speed - a benefit that is far outweighed by the ability to implement reliable, effective tracking that powers better marketing, clearer reporting, and more confident business decisions.

Unless your website is your product (e.g. Gmail, Trello, Notion), use server-rendered pages or lightweight frameworks like Alpine.js or HTMX for minimal interactivity.

Many buttons do not perform traditional navigation - they trigger modals, filters, or scripts. If these elements lack a clear ID or purpose, tracking them becomes challenging.

What to do:

Examples:

<a href="#pricing" id="nav-pricing" data-gtm-click="nav-link" data-label="Pricing">
    Pricing
</a>

<button id="btn-add-to-cart" data-product-id="1234">
    Add to Cart
</button>

These small details make GTM tracking straightforward and accurate.

Use dataLayer Pushes for Dynamic Interactions

GTM triggers cannot always capture everything - especially when using modals, AJAX, or JavaScript-based toggles. In these situations, push an event into the dataLayer.

dataLayer.push({
  event: 'signupComplete',
  userPlan: 'pro',
  signupMethod: 'email'
});

Then in GTM, set up a custom event trigger for signupComplete - and just like that, you are tracking your funnel.

Delay or Block Interactivity Until the DOM Is Ready

Avoid attaching click listeners before the page is fully loaded. GTM tags may not be initialized yet, and clicks may go untracked.

Use:

document.addEventListener("DOMContentLoaded", function() {
  // your listeners here
});

Ensure any critical functionality executes only after window.dataLayer is available.

Be Predictable with Class and ID Naming

Develop a consistent naming pattern. This will help both you and any future developers or analysts.

Examples:

Consistency allows you to:

Build with GTM in Mind, Always

Whenever you add a new section or feature, consider:

Incorporate tracking considerations into your build process from the beginning. It is significantly easier to implement tracking markers during development than to retrofit them afterward.

Use a Data Layer Spec Early

Even if you are not ready to implement it immediately, having a data layer specification is extremely valuable. Think of it as a blueprint for your GTM setup.

Example:

{
  "event": "productView",
  "product": {
    "id": "abc123",
    "name": "Widget 3000",
    "price": 29.99,
    "category": "Widgets"
  }
}

Share this with your developer and request that it be fired when someone views a product. This results in clean, structured data - ideal for GA4, advertising, and reporting.

Do Not Hide Elements That Should Be Visible to GTM

If GTM cannot detect it, it cannot track it.

Avoid:

Instead:

In Conclusion...

Building your website with GTM in mind is not just a technical preference - it is a significant strategic advantage. It saves time and stress, improves collaboration with marketing teams, and most importantly, ensures your data is accurate and actionable.

So: load IDs where appropriate, keep forms conventional, avoid SPAs unless necessary, and treat the dataLayer as a valuable tool.

If you're in the process of building or rebuilding your website and want it to support robust tracking from the start, get in touch with us. We can help you avoid common pitfalls and set things up correctly from day one.

Let's make your site not just functional - but insightful and effective.

Contact Us