Skip to content

Instantly share code, notes, and snippets.

@simondahla
Last active January 3, 2016 11:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simondahla/8459388 to your computer and use it in GitHub Desktop.
Save simondahla/8459388 to your computer and use it in GitHub Desktop.
Google Analytics Retargeting and Demographics and Interests reports for AdWords and AdBlock software with regular (Google Analytics Classic) fallback.
/* Add this in the top of your advertising script */
_adblock = false;
<head>
<!-- 1. Difine the Adblock variable -->
<script type="text/javascript">
var _adblock = true;
</script>
<!-- 2. Put your advertising scripts here -->
<script src="advertising.js"></script>
<!-- 3. Track your visitors with Google Analytics Classic -->
<script type="text/javascript">
try {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-Y']);
_gaq.push(['_trackPageview']);
(function(){
var ga, s
if ( !_adblock ) {
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';
s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
_gaq.push(['_setCustomVar',2,'Adblocker','Not Installed',3]);
} else {
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';
s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
_gaq.push(['_setCustomVar',2,'Adblocker','Installed',3]);
}
}());
} catch(e) {}
</script>
</head>

Google Analytics Retargeting with regular fallback.

Change UA-XXXXXX-Y to your own property id.

  1. Define the Adblock variable _adblock to true
  2. Include the Adblock variable set to falsein one of you advertising script files. This file will be blocked by Adblock.
  3. The function chooses different GA solutions depending on if Adblocker is installed or not.

Note 1: To load an extra asset just to set the variable to false may be uneccecery.

Note 2: This will also send custom events to GA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment