Background of Adblocking with Google Analytics
A while ago I shared some code for webmasters who wanted to know how many of their users were blocking ads. The code I created was for Piwik and Google Classic analytics. As more users make their way to using the Universal Analytics, this code did not work. Below is a complete rewrite of the code for Google Analytics Universal. It does however require a couple more steps. Follow them along and you will have accurate Adblock number shortly.
Setting up decoy file
The first step is to create a file called “adcheck.js”, but to be honest you can call it whatever you want. Inside, paste the below:
document.write('<div id="faker"><p> </p></div>');
Save it, and load it to your sever. This file will test if adblock is on or not.
Adding the Google Analytics Tracking
Now that you have the file on your server, you just need to update the below code with the path of where the “adcheck.js” is located (as noted by the comments).
The above code can be placed anywhere on the page below your Google Analytics tracking code.
The last step is to add the below to your CSS
After you got it working, you will be able to see results under content in the events window. Below is an example of what you will see. As you can see it will show you how many ads were blocked. I prefer the pie view:
To make getting these results faster, this file has all you need to get started.
A note of caution
With Universal Analytics you have much more flexibility, but writing code makes it more complex as each setup will now be different. Therefore as it stands now, the above code will only track number of pageviews with ads blocks, not the number of users blocking ads. To do this you need to create a “custom dimension” in the backend of your Google Analytics admin. This is beyond the scope of this article. In short, I would use the “user scope”, fill in the parameters and add above code as appropriate below. Here is an example of what that looks like
var adblock = 'true'; ga('set', 'dimension1', 'adblock');
You can read more about custom dimensions here. This portion has not been tested, but should work.
One final word