Google Analytics Retargeting for AdWords and AdBlock software

Just a quick post about the retargeting (remarketing) option in Google Analytics. July '12 Google launched a new feature in Google Analytics that made it possible to retarget a specific group of visitors. You only need to change this line in the Google Analytics Tracking Script from:

ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

To:

ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';

The script will be loaded from the doubleclick domain and a third party cookie is set. That cookie can be used to target visitors on other sites that have been on your site.

Beware: AdBlock

If you want to do this, please be aware of the effect an adblocker will have on your tracking. Almost all of the AdBlock tools I know will block the script that is fetched from doubleclick. That means the Google Analytics tracking won't fire, and you can't measure those visitors. So you need a workaround to keep your data as reliable as possible.

Solution 1

The first solution I saw (originally created by Yoray Sanela) was this one:

(function() {
  var ga = document.createElement('script'); 
  ga.type = 'text/javascript';
  ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

  setTimeout(function(){
    if (typeof _gat == 'undefined') {
      var ga = document.createElement('script');
      ga.type = 'text/javascript'; 
      ga.async = true;
      ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    }
  }, 1000); 
})();

The script tries to load the GA functions from the doubleclick domain, and after 1 second it checks whether the functions are loaded. If not: fetch the script from the google-analytics.com domain.

I don't know if this solution is the best one. It detects for a 100% if the script is loaded, but it's based on a timeout. Is 1 second enough to load the doubleclick file? If not, the default script is loaded but the doubleclick script will override it when it's eventually loaded.

Solution 2

Another solution I saw was this one:

<script>
var adblocking = true; 
</script>
<script src="/advertising.js"></script>
<script> 
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-1']);
_gaq.push(['_trackPageview']);

(function(){
  var s
  ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  if (!adblocking) { ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'; }
  else { ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; } 
  s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>

The content of advertising.js would be:

adblocking = false;

It seems that almost 98% of all Adblockers block the "advertising.js" file based on it's name. So if it's blocked the GA code will load the GA functions from google-analytics.com otherwise it will load the Doubleclick version.

Based on several tests this solution looks like the best one. It could happen you don't track all visitors, but you will get the retargeting function to ease the pain ;)

A special word for the Ghostery plugin: if someone uses that plugin: you're screwed. It simply blocks all communication to the google-analytics.com domain. The best solution is workaround that is to implement the mobile version of the Google Analytics tracking script. That scripts sends the tracking pixels via your own server and will not be blocked.

Other people that experimented with other solutions?

Click to activate social bookmarks

 
  • Anna Lewis

    Hi Andre, thanks very much for sharing some possible solutions. I've recently implemented the second one on a site through tweaking Yoast's Analytics for WordPress plugin. Is it right to see a message in Analytics saying: Warning: You have created at least one remarketing list, but have not made the necessary changes to your tracking code.

    I expect this is because the code is not exactly as they expect it to be, but will it still work?

    • http://andrescholten.net/ André Scholten

      Yes, it will still work, Google can only detect the normal code.

  • Justin Walmsley

    this looks like a great hack. Would it be worth just running the normal GA script first and seeing if this a real problem, or just implement it from the beginning?

    • http://andrescholten.net/ André Scholten

      It could never hurt to implement the normal script to see what your data does after implementing the above ones.

  • Phil Pearce

    You have a typo in the above ‘script’ should be 'script'. Thanks.

    • http://andrescholten.net/ André Scholten

      Thanks Phil, a very annoying one, corrected it.

  • http://weblog.bergersen.net/ Jarle Dahl Bergersen

    Thank you for great work. Will not loose out on tracking a lot of visitors now thanks to you :)

  • Willem Paling

    We've got another solution here: http://www.analyticsresults.com/2013/03/doubleclick-remarketing-and-adblock-our.html

    I prefer it because it doesn't rely on setTimeOut and doesn't require any external javascript, which is great for working on client sites — it can be implemented entirely in Google Tag Manager.

  • Pingback: About to make the jump to the DoubleClick version of the Google Analytics script? Read this first.()

  • Ilsevd

    Quite late to join in on the discussion, but I'm looking to integrate the doubleclick.net code.
    After some research, I found a few more recent sources claiming the adblock issue should be resolved by now. Do you know any more details about this/could you confirm this?

    • http://andrescholten.net/ André Scholten

      I've read that also, a few adblockers claimed they wouldn't block the analytics script, but I haven't tested it yet.

  • Mavis Flixar Ohrum

    I didn't know this before, but with site extensions on your Adwords ads you can give people several options to visit your page, or, just offer a bunch of different ad copy in the form of links that all point to the same place. Do you have sitelinks setup, and actively gaining impressions/clicks in your account? If that sounds tricky, it isn't as difficult as you might imagine. You can get help by just giving Simon a ring, his number is 888-648-5526.

  • Bole

    Is there a solution for users who do not use ad blocker (dc.js gets loaded) but block third-party cookies? In my test case, no data gets sent to Google (no hits recorded).

    • http://andrescholten.net/ André Scholten

      Hi Bole, I tested it also, and in my case the tracking is done properly. Only the third-party cookie isn't set. I see the hits being send to Google even when I disabled 3rd party cookies?

      • Bole

        Thanks Andre. I made mistake, its corrected now, and everything works great!