I am having some problems implementing Google Tag Manager on my website. I have a Google code, and when I try it on my site, it makes a lot of javascript co
Simo Ahava on the Google Tag Manager forums answered this type of question:
Why jQuery might not be defined
Basically, he says that GTM does not load jQuery by default.
Even if your page does load it, the GTM tag that uses it might get triggered prior to the on page loading of jQuery.
You can either make your jQuery code wait for the library to be loaded or load it within the custom HTML tag.
I finally found the solution, as it says in an other question, jQuery was injected by the googletagmanager and it was configured by the Web Agency that configured the google tag manager.
So if you have the same problem, call your Web Agency...
And if you have configured yourself the tag manager, check in your settings on Google Tag Manager interface.
Like it has been said before, GTM might load before the page's jQuery loads. That means that if you use jQuery in the script injected via GTM, it might generate a "Uncaught ReferenceError: jQuery is not defined", depending on the browser resource loading strategy.
That said, you must guarantee jQuery is loaded before the GTM script. You can do this by either:
setInterval
loop until jQUery is available:var func = function() {
if (jQuery) {
clearInterval(timer);
// do your stuff
}
}
var timer = setInterval(func, 1000);