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') + '';


ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + '';

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://') + '';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

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') + '';
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 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:

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

var s
ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
if (!adblocking) { ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + ''; }
else { ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + ''; }
s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

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


14 thoughts on “Google Analytics Retargeting for AdWords and AdBlock software

  1. 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?

  2. 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?

  3. Quite late to join in on the discussion, but I'm looking to integrate the 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?

  4. 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.

  5. 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).

    1. 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?

Comments are closed.