Highlighted

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
Spoiler
|| 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