Google Tag Manager – how we use it


Stop Copy-Pasting Your Tracking Codes: A Better Approach to Google Tag Manager

If you ask the average digital marketing agency to install a new tracking pixel, the process usually looks like this:

  1. Copy the code provided by the advertising platform.
  2. Open Google Tag Manager (GTM).
  3. Paste the code exactly as-is into a Custom HTML tag.
  4. Set it to fire on “All Pages.”
  5. Publish.

It works, right? Technically, yes. But over time, this “copy-paste” mentality leads to bloated containers, sluggish page load times, and messy data collection.

At [Your Agency Name], we handle GTM a little differently. We don’t just paste code; we engineer it. By taking a few extra minutes to clean, format, and strategically deploy tracking scripts, we protect our clients’ website performance while ensuring their data is pristine.

Here is a look behind the curtain at how we optimise tracking codes—and how you can do it, too.

1. Trimming the Fat: The <noscript> Fallacy

Almost every tracking script you receive (Google Ads, Meta, Metrica, LinkedIn) comes with two parts: a <script> block and a <noscript> block.

The <noscript> block contains an invisible image pixel designed to track users who have JavaScript disabled in their browsers.

The Problem: Google Tag Manager relies heavily on JavaScript to function. If a user has JavaScript disabled, your GTM container won’t load, which means your Custom HTML tag won’t fire anyway. Including a <noscript> block inside a GTM Custom HTML tag is entirely useless—it’s just dead weight that bloats your container size.

When we implement tags, the very first thing we do is strip out the HTML comments and the <noscript> fallbacks.

2. Formatting for Humans

Ad platforms usually minify their tracking codes—squishing them into a single, unreadable block of text to save a few bytes. While computers don’t mind reading this, it makes debugging a nightmare for developers and marketers down the line.

We take the time to un-minify the configuration settings of the tag.

Before (Standard Yandex Metrica Snippet):

HTML

<script type="text/javascript"> (function(m,e,t,r,i,k,a){ m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)}; m[i].l=1*new Date(); for (var j = 0; j < document.scripts.length; j++) {if (document.scripts[j].src === r) { return; }} k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a) })(window, document,'script','https://mc.yandex.ru/metrika/tag.js?id=12345678', 'ym'); ym(12345678, 'init', {ssr:true, webvisor:true, clickmap:true, ecommerce:"dataLayer", referrer: document.referrer, url: location.href, accurateTrackBounce:true, trackLinks:true}); </script> <noscript><div><img src="https://mc.yandex.ru/watch/12345678" style="position:absolute; left:-9999px;" alt="" /></div></noscript>

After (Our Optimised Implementation):

HTML

<script type="text/javascript"> (function(m,e,t,r,i,k,a){ m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)}; m[i].l=1*new Date(); for (var j = 0; j < document.scripts.length; j++) {if (document.scripts[j].src === r) { return; }} k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a) })(window, document,'script','https://mc.yandex.ru/metrika/tag.js?id=12345678', 'ym'); ym(12345678, 'init', { ssr:true, webvisor:true, clickmap:true, ecommerce:"dataLayer", referrer: document.referrer, url: location.href, accurateTrackBounce:true, trackLinks:true }); </script>

By breaking out the initialisation object, anyone auditing the account can immediately see what features are enabled—like the dataLayer integration for e-commerce or the webvisor feature for session recording.

3. Trigger Discipline: Protecting Page Speed

Perhaps the biggest sin in GTM management is firing heavy scripts on “All Pages” (Page View).

Let’s look at the Metrica script above. It includes webvisor:true, which records user sessions (mouse movements, clicks, scrolling). Session recording scripts (like Webvisor, Hotjar, or Clarity) are notoriously heavy. If you load them the millisecond the page starts rendering, they will compete with your actual website content for bandwidth.

The result? A slower website, frustrated users, and a hit to your Core Web Vitals (which hurts your SEO).

Instead of the default “All Pages” trigger, we strategically delay non-essential, heavy tracking scripts by using the Window Loaded trigger. This tells the tag: “Wait until the visual page, images, and core functionality have completely finished loading for the user, and only then start tracking their mouse movements.”

4. Connecting Directly to the Source (The Data Layer)

Finally, a sophisticated GTM setup doesn’t rely on scraping the website’s HTML to figure out what a user bought.

In our optimised snippet above, you’ll notice ecommerce:"dataLayer". We intentionally ensure that tracking scripts are built to listen directly to the website’s data layer—a hidden stream of structured data that passes exact transaction values, item IDs, and currencies directly to GTM. This guarantees 100% accuracy in ROI reporting, completely insulated from site redesigns or changing button colors.

The Bottom Line

Google Tag Manager is an incredibly powerful tool, but it is not a magic bullet. Treating it like a simple “paste it and forget it” dumping ground leads to poor performance and fragile data.

By cleaning the code, structuring it for humans, utilising intelligent triggers, and leveraging the data layer, we ensure our clients get world-class analytics without sacrificing a millisecond of website speed.

Want to know if your Tag Manager container is weighing your site down? Reach out to the team at Egret Digital for a comprehensive tracking audit.