0

I am trying to develop a way to tell if ghostery is blocking google doubleclick ad scripts from loading. I don't want to use a listener, I want to just be able to tell if that script or url is blocked. I came up with this which always errors out but I can differentiate the error messages. Hoping for a better solution than a broken ajax call.

I just want to report how many users not seeing ads because they are using an ad script blocker enabled.

  $.ajax({
    url: 'http://www.googletagservices.com/tag/js/gpt.js',
    type: 'GET',
    error: function(d) {
      console.log('error', d, '/error');
      if(d.statusText != 'error') {
        console.log('ghostery enabled');
      } else {
        console.log('script is not blocked');
      }
    }
  });
user1572796
  • 1,057
  • 2
  • 21
  • 46
  • http://stackoverflow.com/a/14296176/33822 – willoller Jan 09 '15 at 01:00
  • right, but there is no crossbrowser solution for an on error event attached to the script tag. I am only setting a tracking variable if the script is blocked not if it loaded successfully. – user1572796 Jan 09 '15 at 01:03
  • no there is no `onerror`. This solution uses `onload` which I think would work for your situation - it will **not** fire if the script is blocked (or 404). – willoller Jan 09 '15 at 01:14
  • yeah but then I would need a listener for the onload function which I am trying to avoid – user1572796 Jan 09 '15 at 01:17
  • thanks for the response though – user1572796 Jan 09 '15 at 01:27
  • Just record 2 events: one for page loaded, and one for script load. Then your report would be SUM(pages - scripts) and you would know how many were blocked. – willoller Jan 09 '15 at 05:44
  • I ended up putting onload and onerror events on the script, then in onload also did a check that gpt isn't blank. This seems to work for ghostery, disconnect.me, then loaded a ads.js file to detect Ad Block. – user1572796 Jan 09 '15 at 23:19

2 Answers2

0

Looks like this method is really the only thing out there right now without using intervals or timeouts,

<script src="http://www.googletagservices.com/tag/js/gpt.js" onload="callback()" onerror="error()"></script>

function callback() {
  // in this case you will need to verify the gpt object isn't blank for some ad blockers in chrome
}

function error() {
  // most script blocking ad blockers will end up here.  All script blocking ad blockers (ghostery, disconnect) I have seen in firefox end up here.
}
user1572796
  • 1,057
  • 2
  • 21
  • 46
0

Try a timeout with a function test to check if the global var set by the script is loaded. IE;

function loadExtScript(src, test, callback) {
  var s = document.createElement('script');
  s.src = src;
  document.body.appendChild(s);

  var callbackTimer = setInterval(function() {
    var call = false;
    try {
      call = test.call();
    } catch (e) {}

    if (call) {
      clearInterval(callbackTimer);
      callback.call();
    }
  }, 100);
}

To use it, something like:

this.loadExternalScript('www.domain.com/script.js', function () {
    return (typeof window.myscriptobject === 'undefined');
}, onScriptFailedToLoad);

you'll be loading the script externally via javascript, but you can test if it's global var has been set. If it hasn't by the end of the timeout then you can assume it failed to load. Note though that this is by no means a perfect solution and a longer timeout would be a safer bet, but still not ideal.

tdc
  • 5,174
  • 12
  • 53
  • 102