JaredDev Logo

Google Tag Manager Events Not Firing (and one possible fix)

2017-11-29

I've recently been on a project that included integrating Google Tag Manager. Part of that integration included setting up several custom events for datapoints the client wants collected. During the integration I made a mistake that had me scouring the internet for a couple of days looking for instances of possible issues.

The issue and fix...

When working with the dataLayer you have to initialize the variable in the head tag. The common recommendation for initialization is to have a simple script tag at the very top of the head tag like...

<script> dataLayer = []; </script>

The problem with this being your mechanism for variable initialization is that if you put it in the wrong spot...random bad things will start happening.

My recommendation for initialization is below...

<script> dataLayer = dataLayer || []; </script>

...this provides one simple benefit, but it's a big one. If you place your tag too low on the page inadvertently like I did nothing breaks! With the above syntax the initialization will essentially do nothing if the variable as already been set. If it hasn't then it will follow the normal initialization.

Conclusion

If you have issues with your Google Tag Manager events check where you are initializing the dataLayer variable. If you don't need to manually set any values on the dataLayer this unfortunately won't be of any help!

Anyway, I hope this can help someone out there. I had read about the need to make sure the variable was declared before the Tag Manager code, but nothing mentioning what can happen if you make that mistake...the answer is ALL the bad things can happen!

And again, I'd really just recommend switching the syntax that you use for initialization to the code I used above. There aren't any negatives that I can think, but it could easily save you from a nightmare!