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