Marcos Pérez Gude
Marcos Pérez Gude

Reputation: 22158

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.

Upvotes: 10

Views: 3793

Answers (3)

Kevin Grande
Kevin Grande

Reputation: 1

In wordpress you can deactivate them installing the following plugin:

GTM4WP

When you configure the plugin it's in Security section.

Then you can deactivate: Google Analytics Tag (classic, legacy)

This is what I can contribute to. Greetings

Upvotes: 0

Marco
Marco

Reputation: 1172

As far as i tested my best hypothesis is that it ll only load ga.js if your container needs it.

The only case i was able tu find is when you use a "Google Analytics classic" Tag (Which are the oldest version) instead of the "Universal Analytics" tag.

I've also tried creating and using dataLayer variables v1 but it doesn't seems to need ga.js for this.

If you can find more cases please share them since this is actually pretty interesting and even when GTM has almost no effect on site performance it's something to have in mind.

Upvotes: 3

lossleader
lossleader

Reputation: 13495

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",

Upvotes: 6

Related Questions