by Doug


Implementation of Google Analytics

More GTM debugging power – include the dataLayer in your GA events SAFELY! (update)

Hopefully you’re using the (relatively new) gtm.pageError event to track JavaScript exceptions on your site auto-magically?

Yes?  Good.  Here’s an example tag and rule to consider if you’re not:


Right – got that.  Now I want to give you some more POWER to help your debugging.  Knowing the source of the exception if the source was another script is all well and good but what else was happening on the page in terms of the dataLayer when the exception happened?

When the exception fires, you can use a similar tag to the standard exception recording tag but instead you can record your whole dataLayer using this sweet little macro:

Forget the less-than-perfect naming convention on this macro – I’ve renamed it for clarity.  Here’s an example output from the tag. I forced the tag to fire by manually pushing an event onto the dataLayer:

(Javascript Exception*dataLayer*[{“gtm.start”:1397464198223,”event”:”gtm.js”},{“event”:”gtm.dom”},{“event”:”gtm.load”},{“event”:”debug”}])8(33!testcv33*36!testcv36)9

Here’s the data in GA:

I can see how such a tag could be used in many areas during development and testing. It may also have value to ops. Make sure you don’t inadvertently put any PII data in the dataLayer though!

One more thought – What if the exception was caused by a malformed dataLayer? This may have occurred to you already of course but if a malformed dataLayer caused the javascript exception in the first place, the dataLayer won’t be visible…or even exist and so no error reporting tag will fire in GTM.

There are two types of people when it comes down to malformed dataLayers:

  1. Those who have experienced a malformed dataLayer
  2. Those who will experience a malformed dataLayer

See this example: DUH!Wrapping any script in a try/catch block will enable semantic exceptions to be caught programatically….but not syntax exceptions.

So, if your dataLayer instantiation ‘barfs’ then you’ll never know. The exception will be thrown but as no container has fired, the gtm.pageError event will not fire. How do you know the tree has fallen if you’re not in the woods to see or hear it?I suggest using this technique to mop up issues when you’re pre-populating your dataLayer:

script type=”text/javascript”;
dataLayer = [{‘error example’,’This is a demonstration of an unescaped value breaking a dataLayer for the reader’s benefit’}]
window.dataLayer = window.dataLayer || [{
‘gtm.errorMessage’:’dataLayer: failure’,

See this in action
here You’re welcome!

Call this after the pageview using the hitCallBack and your dataLayer failures will be recorded.


Leave a comment

Your email address will not be published.

ConversionWorks is now Media.MonksVisit us at