GTAG loads twice - When google optimize is active

Silver Contributor
Silver 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

2 REPLIES 2

GTAG loads twice - When google optimize is active

Tealium Employee

Hi @nicolarohner 

I think the Tealium TAG template for the GTAG implementation is focused on a "Tealium Deployment" context, hence the conditions check if the existing "gtag" is deployed by "utag".

Like per your suggestion, you can check the global scope by adding your additional conditions.

Alternativelly, you can simplify the conditions by removing the "utag" context making the solution more elegant and easier to read.

Original:

if ( 
     s[i].src &&                                         // checks if it has a src attribute
     s[i].src.indexOf("gtag/js") >= 0 &&                 // checks if the src attribute contains "gtag/js"
     s[i].id &&                                          // checks if it has a id attribute
     s[i].id.indexOf("utag") > -1                        // checks if its deployed by tealium ("utag" always present on id of tags deployed by tealium) 
    ) {
          return true;
}

Suggested:

if ( 
     s[i].src &&                                         // checks if it has a src attribute
     s[i].src.indexOf("gtag/js") >= 0 &&                 // checks if the src attribute contains "gtag/js"
     s[i].id &&                                          // checks if it has a id attribute
     s[i].id.indexOf("utag") > -1 ||                     // checks if its deployed by tealium ("utag" always present on id of tags deployed by tealium) 
s[i].src && // checks if it has a src attribute
s[i].src.indexOf("gtag/js") >= 0 && // checks if the src attribute contains "gtag/js" ) { return true; }

Alternative:

if ( 
     s[i].src &&                                         // checks if it has a src attribute
     s[i].src.indexOf("gtag/js") >= 0 &&                 // checks if the src attribute contains "gtag/js"
    ) {
          return true;
}

 

 

 

 

GTAG loads twice - When google optimize is active

Silver Contributor
Silver Contributor

Hi @ruipedromachado ,

thank for the explanation and the alternative approach, sounds logic for me.

Thanks & Regards
Nicola

Public