Skip to content

Finding pages missing Google Tag Manager snippet within Google Tag Manager


This time, we’ll be using Google Tag Manager itself, to find pages in our site that may be missing Google Tag Manager. Ok, it may sound strange, but was not able to find any other way to say it .

Basically we’re going to use a custom html tag, to detect is the previous page had loaded Google Tag Manager code.

To achive this we’ll be using a single Custom HTML tag ,a cookie and the onbeforeunload event.

The following flow chart will show you the logic that we’re going to follow to detect the pages that are missing the Google Tag Manager snipper for our site.

So we’ll create a new custom HTML tag (that will be fired for ALL pages default trigger), and then add the following code to it, remember to update the “hostName” variable value to your domain root:

    // Change this to your root domain name
  var hostName = '';
    // On before Unload 
  window.onbeforeunload = function(e) {
  	expire = new Date();
  	expire.setTime(new Date()*1+10*1000);
  	document.cookie = "__ttt="+escape(1) + ";expires="+expire.toGMTString()+ ";domain=."+hostName+";path=/";

    // If user's coming from elsewhere but our domain, exit.  
    if(location.hostname.split('.')[location.hostname.split('.').length -2].length==2)
      var rootDomainName = location.hostname.split('.').slice(-3).join('.');
      var rootDomainName = location.hostname.split('.').slice(-2).join('.');

    function isCookiePresent(name) {
      match = document.cookie.match(new RegExp(name + '=([^;]+)'));
      if (match) return true; else return null;

        'referrer': document.referrer
      document.cookie = '__ttt=; expires=Thu, 2 Aug 2001 20:47:11 UTC; domain=.'+hostName+';path=/';

Ok, that’s all! 

Now we could create a new Event tag that fires when the event equals to “missingtag” to send that data to Google Analytics.

If you’re struggling with some hits being sent from hardcoded tags, I suggest you to take a look to this great tip from @fastbloke  for being able to find them, and you could look to this other old post

If you’re sending the data to Google Analytics, remember to set the event to a nonInteractional hit!


Any feedback, comment or improvement will be really appreciated.



Published inweb analytics


  1. Hi David,

    thanks for your post and sharing this interesting idea.

    It got me thinking about this and i just wanted to add some possible improvement ideas.
    If i got it right you are just checking if the cookie is still present. So some page must have set it within the last 10 seconds. But right know you dont know which page set it.
    Therefore you might skip one page within ten seconds, which is untagged but will not get recognised.
    Also considering for example a mobile page, accessed from a slow network, it might sometimes take longer than ten seconds to load the next page and fire this tag.
    This would result in a false missing tag alert.

    It should be possible to avoid the above by writing the current URL in the cookie value and checking the referrer with the cookie value on the next page. The cookie could be a session cookie then.

    Also there might be some cases where the referrer is not set (HTTPS/HTTP switch), which currently could not be checked then.

    Anyway, i really like the idea, thanks for sharing!


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.