GTAG loads twice - When google optimize is active

Bronze Contributor
Bronze Contributor

I am currently testing a A / B test with google optmize, I noticed that gtag is loaded twice in some cases. I looked around in the template and discovered the "hasgtagjs" function. Among other things, this checks whether an id for utag exists. Sometimes this is not the case, for example when google optimize is already loaded (directly in source code and with it gtag.js) but apparently utag does not have an ID yet.


u.hasgtagjs = function() {
      window.gtagRename = window.gtagRename || "" || "gtag";
      if (utag.ut.gtagScriptRequested) {
        return true;
      var i,
        s = document.getElementsByTagName("script");
      for (i = 0; i < s.length; i++) {
        if (
          s[i].src &&
          s[i].src.indexOf("gtag/js") >= 0 &&
          s[i].id && s[i].id.indexOf("utag") > -1
|| s[i].src && s[i].src.indexOf("gtag/js") >= 0
        ) {
          return true;



My suggestion would be to add an additional "or" condition to this existing for-loop and just check if gtag.js has already been loaded.
My question: Could this be problematic in certain situations if you only check for gtag.js? (you always check whether utag exists and if not use the "only gtag.js condition")

Cheers Nicola


GTAG loads twice - When google optimize is active

Employee Emeritus
Hi @nicolarohner

You are correct. Our hasgtagjs method is used with Tealium implementation of gtag, and does not consider any other implementations of gtag outside our platform. This is intentional as it is inherently risky to rely on external source for our tag logic to work so that is the default.

If you feel that you need to change this logic, you can do so by using a Javascript Code extension (scoped to the gtag) that modifies the functionality:

u.hasgtagjs = function() { /* your modified version here */ }