简体   繁体   中英

How to exclude tag in Google Tag Manager based on specific script include in HTML

I have to modify our Google Tag Manager container and I'm quite noob in GTM. We have a problem with IE8 and pages that used Fusion Charts. With this combination, I have a javascript error in gtm.js and I've found that it is in a Form Submit Listener. So I thought that I would add an exception on that tag. That exception would be "If isIE8 and hasFusionChartsControl". These two are variables of type "Custom Javascript" in GTM that I have defined like this :

contientFusionCharts :

function() { 
  var file = "fusioncharts.js";
  var scripts = document.getElementsByTagName("script");
  for(var i = 0; i < scripts.length; i++) {
    if (scripts[i].src != undefined && scripts[i].src.substr(scripts[i].src.length-file.length) == file) {
      return true;
    }
  }

  return false;
}

estIE8 :

function() {
  var isIE = !!document.documentMode; // At least IE6
  var rv = -1; // Return value assumes failure.
  if (isIE) {
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
  }

  return (rv > -1 && rv <= 8.0);
}

I have tested those javascript functions and they work well on IE8. Now I have defined a trigger : 我的GTM触发器是“页面查看事件”,在“加载的窗口”上触发并在pageHasFusionChartsControls和isIE8上触发

And now my tag, I only added the exception : 在此处输入图片说明

This look all fine to me but it is still not working. I can certify that this tag is in problem because I have created an exception that look the URL for a specific page and when I browse this specific page, the js error is no longer raised. But I don't want to create an exception specifying specific page since this is a Sharepoint site and users create page as they wish. So I don't want to have to add exceptions everytime they use Fusion Charts controls in their pages. So I thought that looking for a specific html tag that is proper to Fusion Charts would be a good idea and I came to the conclusion that looking for the script include of fusioncharts.js would be a great idea.

If you have an explanation to why it is not working and alternatives solutions, that would be great! Thank you!

For future reference, here's the answer I've received on the google product forum from Simo Ahava :

Hey

Exceptions only block the event you've set them up for. So your exception will ONLY block the "Window Loaded" event. Is your Tag firing on that event? If your Tag is firing on the "All Pages" event, it's using the Page View, and not the Window Loaded event, so you need to change your Exception trigger accordingly to match the event of the Tag.

OR you can just create a global exception, which blocks all events that match the other conditions. You do this by using a Custom Event Trigger, and set the event name to .* (and check RegEx).

http://www.simoahava.com/gtm-tips/block-your-tags-with-trigger-exceptions/

Simo

I've choosen to create the global exception using a Custom Event Trigger with the event name .* (using RegEx) and that worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM