Tag Sequencing in Google Tag Manager: Guide

published on 13 November 2024

Tag sequencing in Google Tag Manager (GTM) lets you control when tags fire on your site. Here's what you need to know:

  • Tag sequences have 3 parts: setup, main tag, and cleanup
  • It improves data accuracy and can speed up your pages
  • Set it up in Advanced Settings in GTM
  • Use custom events and dataLayer for complex sequences
  • Always add error handling to custom HTML tags
  • Do monthly tag checks and use Preview mode before publishing

Key benefits: • Better data quality • Improved tag coordination • Smoother data collection • Enhanced user experience

Common issues:

  1. Tags firing out of order
  2. Missing data layer variables
  3. Trigger misfires

Best practices:

  • Use Preview mode religiously
  • Conduct regular tag audits
  • Monitor real-time reports
  • Set up custom alerts
  • Leverage browser dev tools

How to Set Up Tag Sequences

Let's walk through setting up tag sequences in Google Tag Manager (GTM). This process is key for SaaS and eCommerce businesses to nail their data collection and keep their websites running smoothly.

Setting Up Your First Tags

Here's how to kick things off:

  1. Figure out what you want to track. Maybe it's product views, add-to-cart actions, or purchases.
  2. Make a plan. List out all the events and data points you'll keep tabs on.
  3. Set up your GTM container if you haven't already.
  4. Time to create your first tag. Let's use Google Analytics 4 (GA4) as an example:
    • Head to tagmanager.google.com and pick your container
    • Hit "New" to create a tag
    • Choose "Google Analytics: GA4 Configuration"
    • Pop in your GA4 Measurement ID
    • Set the trigger to "All Pages"
    • Give your tag a name (like "GA4 - Page View")
    • Hit "Save"
  5. Test it out. Use GTM's Preview mode to make sure your tag's firing right.

"If you hit a wall and can't track something on your site, ask a developer for help. They can push the data via dataLayer.push." - Simo Ahava, GTM guru

When Tags Go Wrong

Sometimes tags fail. Here's what to do:

  1. Use Preview and Debug mode. It shows you which tags are firing (and which aren't).
  2. Look for error messages in the GTM debug console or browser console.
  3. Double-check your trigger conditions. Are they set up right?
  4. Check tag priority if you've got multiple tags firing on the same event.
  5. Set up error handling for custom HTML tags:
    try {
      // Your tag code here
      onHtmlSuccess(); // Tell GTM it worked
    } catch(e) {
      onHtmlFailure(); // Tell GTM it didn't
    }
    
  6. Use the 'Don't fire' option in tag sequencing settings to stop issues from snowballing.

Adding Cleanup Tags

Cleanup tags keep your data clean. Here's how to set them up:

  1. Create a new Custom HTML tag in GTM.
  2. Write your cleanup code. For example, to reset data layer variables:
    <script>
    dataLayer.push({
      'productName': undefined,
      'productPrice': undefined,
      'userEmail': undefined
      // Add all variables you want to reset
    });
    </script>
    
  3. Set up tag sequencing:
    • Go to your main tag's settings
    • Expand "Advanced Settings"
    • Under "Tag Sequencing", check "Fire a tag after your tag fires"
    • Pick your cleanup tag
  4. Test it thoroughly in Preview mode.

Advanced Tag Sequencing Methods

Tag sequencing in Google Tag Manager (GTM) can be tricky. Let's look at some advanced techniques to help you master this key part of digital analytics.

Working with Async Tags

Async tags are great for speed, but they can mess up your sequencing. Here's how to handle them:

1. Custom Events

Create a chain of events to control when tags fire. For example:

dataLayer.push({'event':'doneWithTagA'});

Set Tag B to fire on this custom event. This makes sure Tag B only fires after Tag A is done, even if they load async.

2. Callbacks

For custom HTML tags, use callbacks:

<script>
try {
  // Your tag code here
  // When done, call:
  onHtmlSuccess();
} catch(e) {
  onHtmlFailure();
}
</script>

This tells GTM when your tag is finished, so the next tag can fire.

Custom HTML Tag Setup

Custom HTML tags are powerful but need careful handling:

1. Use ECMAScript 5

GTM's Custom HTML tag only supports ES5. For modern JavaScript, use:

<script type="text/gtmscript">
// Your ES6+ code here
</script>

This lets modern JavaScript work in GTM.

2. Avoid Global Variables

Always use var for variables:

(function(){
  var myVariable = 'local';
  // Your code here
})();

3. Handle Errors

Always include error handling:

try {
  // Your tag code here
} catch(e) {
  console.error('Custom HTML tag error:', e);
  dataLayer.push({'event': 'customTagError', 'errorMessage': e.message});
}

Making Tags Load Faster

Speed matters for user experience and SEO. Try these:

1. Use Window Loaded Trigger

Instead of Page View, use the Window Loaded trigger. This keeps your tags from slowing down page rendering.

2. Use Timers

Use GTM's Timer trigger for non-critical tags:

dataLayer.push({'event': 'timerFired', 'gtm.timerInterval': 5000});

3. Try Server-Side Tagging

If you can, use GTM's server-side features. This takes work off the user's browser, making things faster.

4. Audit Often

Keep checking and removing tags you don't need. As GTM expert Simo Ahava says:

"Avoid using Custom HTML tags unless it's absolutely necessary."

sbb-itb-38e9f15

Fix Common Problems and Follow Best Practices

Let's tackle some common issues you might face with tag sequencing in Google Tag Manager (GTM) and how to solve them.

Common Tag Sequence Problems

Tags Firing Out of Order

Async loading can mess up your tag order. Here's a quick fix:

Use custom events in your code:

dataLayer.push({'event':'tagAComplete'});

Then, set your next tag to fire on this event. This keeps your tags in line, even with async loading.

Data Layer Variables Missing

Make sure your data layer is ready before GTM loads. If you're seeing undefined values, double-check your setup order.

Trigger Misfires

Complicated triggers can cause unexpected tag fires. Keep it simple and use Preview mode to test thoroughly.

Keep an Eye on Your Tags

Monitoring your tags is key. Here's how:

Use GTM's Preview Mode

Always test in Preview mode before you publish. As GTM expert Simo Ahava says:

"The Preview mode in GTM is your first line of defense against tagging errors. Use it religiously."

Do Regular Audits

Set up monthly tag checks. Tools like Tag Inspector can help automate this. One e-commerce site cut their page load time by 2 seconds after finding and removing extra tags during an audit.

Watch Real-Time Reports

Check your analytics real-time reports to make sure data is flowing right. If something looks off, dig into it right away.

Set Up Custom Alerts

Create alerts in Google Analytics for sudden changes in tag fires. This helps you catch problems early.

Use the Network Tab

Your browser's Network tab is great for debugging. Look for requests with a 200 status code to confirm your data is getting through.

Summary

Tag sequencing in Google Tag Manager (GTM) lets you control the order your tags fire. It's a big deal for SaaS and eCommerce businesses that want to nail their digital analytics and keep their data on point.

Here's what you need to know about tag sequencing:

Tag sequences have three parts: setup, main tag, and cleanup. This setup lets you make tags depend on each other, so one tag finishes before another starts.

Why bother? It makes your data more accurate. You can make sure a tag that grabs user info fires before sending that data to your CRM.

It can also speed up your pages. As GTM expert Simo Ahava puts it:

"The asynchronous loading of JavaScript is taken for granted these days. It's a nice way for browsers to ensure a decent browsing experience even with large, bulky linked assets."

To set up tag sequences, head to Advanced Settings in GTM. You can pick setup and cleanup tags for your main tag. GTM handles the triggers, so don't worry about those.

For trickier sequences, use custom events and the dataLayer. This lets you create a chain of events, so tags fire in order even with async loading.

Always add error handling to your custom HTML tags. It helps you spot and fix issues fast:

try {
  // Your tag code here
  onHtmlSuccess(); // Tell GTM it worked
} catch(e) {
  console.error('Custom HTML tag error:', e);
  onHtmlFailure(); // Tell GTM it didn't
}

Do monthly tag checks to make sure your sequences are working right. Tools like Tag Inspector can help. One e-commerce site cut 2 seconds off their page load time by finding and axing unnecessary tags during an audit.

Always test your tag sequences in Preview mode before you publish. Simo Ahava says:

"The Preview mode in GTM is your first line of defense against tagging errors. Use it religiously."

FAQs

Why are my GTM tags not firing?

GTM tags might not fire for a few key reasons:

1. Trigger issues

Your triggers might be set up wrong. This is super common. Double-check all your trigger settings.

2. Blocking triggers

You might have accidentally set up a trigger that's blocking your tag from firing.

3. Tag sequencing problems

If you've set up tag sequencing, it could mess with when and how your tags fire.

GTM expert Simo Ahava sums it up nicely:

"In a nutshell, here are the possible reasons why a tag in Google Tag Manager is not firing (or fires unexpectedly): Trigger misconfiguration. A blocking trigger is being used. You've configured Tag sequencing in your tag."

Want to figure out what's going on? Use GTM's Preview mode. It's the best way to see why your tags aren't firing like they should.

How do I fix my Google tag issues?

Got Google tag problems? Here's how to fix them:

  1. Open the Tag Diagnostics tool in your Google tag settings.
  2. Go to the Tag quality subsection under Your Google tag.
  3. Check out the tag quality status.
  4. Click to find and fix any issues it spots.

This process helps you spot and fix common tag problems fast. But don't wait for problems to pop up. Check your tag setup regularly to catch issues early.

Digital marketing pro Mia Watson puts it well:

"I can't stress enough how useful the preview mode in GTM is."

So don't forget to use that preview mode. It's a lifesaver when you're trying to figure out what's going wrong with your tags.

Related posts

Read more