javascriptgoogle-analyticsgoogle-tag-manageruniversal-analytics

Google Tag Manager includes both legacy Analytics (GA) and Universal Analytics (UA) scripts


I'm making better performance and load time our online shop, and we use Google Tag Manager on it. But the script that includes google tag manager also loads Google Analytics (legacy ga.js) and Universal Google Analytics (analytics.js) by default. I don't need both of them, but if it's necessary I only need Universal Google Analytics.

So why is google tag manager including both scripts?

As a coding resume, this is the tag manager inclusion:

<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXX');</script>

-

On network tab, it appears the inclusion and after it, it loads automatically both scripts:

Google Tag Manager automatic inclusion

-

If I analyze the Google Tag Manager script (gtm.js) I see the following:

else if (!a) {
    var N = c["60"] ? ".google-analytics.com/u/ga_debug.js" : ".google-analytics.com/ga.js";
    a = !0;
    u(x("https://ssl", "http://www", N, r), O, c["66"])
}

And this:

if (!a) {
    var M = b["60"] ? "u/analytics_debug.js" : "analytics.js";
    b[""] && !b["60"] && (M = "internal/" + M);
    a = !0;
    bb(x("https:", "http:", "//www.google-analytics.com/" + M, d && d.forceSSL), function() {
        var a = $a();
        a && a.loaded ||
            b["66"]();
    }, b["66"])
}

-

So google tag manager is including both scripts. I can assume Universal Google Analytics, but legacy ga.js script why??

Can I avoid the inclusion of both or only legacy ga.js scripts?

Thank you.

Edit

I started a bounty because I need an explanation of why this happens, and if it's possible a way to avoid this behavior.


Solution

  • GTM automatically loaded neither of these scripts.

    The tags you have defined in your published container load the relevant script to fire if a previous tag firing hasn't loaded it.

    For example, the first Tag loads classic GA only if it is fired, while the last Tag loads UA when it fires and is fired at every page load: enter image description here

    You can determine which of your Tags actually fire by putting your client into preview's debug mode, and if a script is still puzzling you can export your container and look through its definitions (or custom JS) to find the tags that relate to scripts:

    enter image description here

                "tagId": "7",
                "name": "classic track event",
                "type": "ga",
    
                "tagId": "1",
                "name": "page track",
                "type": "ua",