Jquery and Google Tag Manager

前端 未结 3 1269
无人共我
无人共我 2020-12-20 17:39

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

相关标签:
3条回答
  • 2020-12-20 17:48

    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.

    0 讨论(0)
  • 2020-12-20 17:58

    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.

    0 讨论(0)
  • 2020-12-20 18:04

    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:

    1. controlling the order of loading of scripts
    2. employing a hack such as using a setInterval loop until jQUery is available:
    var func = function() {
        if (jQuery) {  
            clearInterval(timer);
            // do your stuff
        }
    }
    var timer = setInterval(func, 1000);
    
    0 讨论(0)
提交回复
热议问题