• How Many Users Block Your Ads? Find Out!

    by  • Last modified:March 10, 2013 • Google Analytics, Piwik • 13 Comments

    Your Visitors Block Ads, But How Many?
    Have you ever been curious to know how many people are blocking ads on your site? I had try to measure this in the past, but today I have a much more elegant solution.

    Why Bother Tracking If People Don’t See Ads?
    One of the reasons I wanted to know how much adblocking was occurring is to make the determination if serving ads was even worthwhile. Sure if you have Adsense, you get a feel for the revenue, but do you know how many extra eyeballs never even see the ads? Maybe 90% of your users block ads? Maybe you need to get creative in your advertising? Maybe you should remove ads altogether if you need a source of revenue?

    Some Caveats About Ad Block Tracking
    If a user is intent on not being tracked, this solution will not work. In other words, if they are using no script or blocking trackers all together, they will not be counted. There are work-arounds for this, but there is a whole new set of problems with tracking noscript referrers. If you are not aware, bots do not read javascript so your log/analytics could get swamped by hits from bots, crawlers and other beings that may or may not be real. Nevertheless, taking this into account and having an adblock number can still give you some interesting insights about your visitors.

    Choose your solution
    No matter your analytics solution, either Google Analytics or Piwik Analytics, below is how you would find out if users are blocking ads.

    First Step: jQuery
    Please make sure you have your Google or Piwik tracking installed and working. You will also need the jQuery Library on your site. The easiest way
    to add this is use the Google hosted jQuery Library. Just add this code to your site:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

    Second Step: Your Ads
    The next step is to mark up your Ads div. If you don’t want to mark up the div, or are not sure how to, just add the below to anywhere on the page.

    <div class="adsense"><p>&nbsp;</p></div>

    Third Step: Tracking – Google Analytics

    For Google Analytics you can do the tracking as an event or a custom variable. Just make sure the div is the same as denoted in this portion of the code: if ($(".adsense"). If you use our default implementation or your div name is “adsense” you are fine.

    Custom Variable Code

    /* Adblock Check */ 
    $(document).ready(checkAds());
    
    function checkAds() {
            if ($(".adsense").height() == "0") {
            document.write("<script> _gaq.push(['_setCustomVar', 5, 'Adblock','Blocked',2]);<\/sc" + "ript>");}    
    		else
    		{
    		document.write("<script> _gaq.push(['_setCustomVar', 5, 'Adblock','UnBlocked',2]);<\/sc" + "ript>");}	
    		}	
    /* End Adblock Check */ 
    

    Here is the Adblock Event Tracking Code:

    <!--Adblock Check-->
    <script>
    $(document).ready(checkAds());
    
    function checkAds() {
            if ($(".adsense").height() == "0") {
            document.write("<script>_gaq.push(['_trackEvent', 'Adblock', 'Blocked', 'true',,true]);<\/sc" + "ript>");}    
    		else
    		{
    		document.write("<script>_gaq.push(['_trackEvent', 'Adblock', 'Unblocked', 'false',,true]);<\/sc" + "ript>");}	
    		}	
    </script>
    <!--End Adblock Check-->
    

    Personally I would use the custom variable option. The benefit of using custom variables is you can easily use it to see behavioural differences of those that block ads vs. those who do not. Only do this if you have the slots in Google Analytics. If you were not aware, the free version of Google Analytics limits you to 5 custom variable slots per session. To account for this, I have set the code to take slot 5. If you need all 5 custom variable slots, use the event tracking. Don’t cry if you have to use the events code. You can still see how Adblock visits affect your traffic by using Advanced Segements. You can see here for more details on how to use Advanced Segments and Events.

    Sometimes, when you are using custom variables, it’s hard to place it within the core Google Tracking Code. If your adblocking check must happen later in the page, after the initial call to _gaq.push(['_trackPageview']);, there are two solutions: One is to use a virtual pageview, which you would filter out of the account (i.e. _gaq.push(['_trackPageview', '/fakepage]); ). The reason you would do this is to reinstantiate the tracker, by causing a tracking GIF request. This in turn will help to deliver the custom variable to your Google Analytics. The other, solution to refire the tracking GIF request is to call an event after you set the custom variable. Below is the sample code to do that.

    <!--GA Adblock Check-->
    <script>
    $(document).ready(checkAds());
    function checkAds() {
    if ($(".adsense").height() == "0") {
    document.write("<script>_gaq.push(['_setCustomVar',5,'Adblock','true',2]); _gaq.push(['_trackEvent','Adblock','true']);<\/sc" + "ript>");
    }    
    else
    {
    document.write("script>_gaq.push(['_setCustomVar',5,'Adblock','false',2]); _gaq.push(['_trackEvent','Adblock','false']);<\/sc"+"ript>");
    }	
    }	
    </script>
    <!--End Adblock Check-->
    

    Calling an event after the custom variable is set is also a preferred method noted by Google in their Custom Variable documentation.

    Third Step: Tracking – Piwik Analytics
    For Piwik Analytics the tracking is done as a custom variable. Just make sure the div is the same as denoted in this portion of the code: if ($(".adsense"). If you use our default implementation or your div name is “adsense” you are fine. Like Google Analytics, Piwik has 5 slots per scope. For this reason, I have selected index 5, to allow you for 4 more indexes.

    Place the below code INBETWEEN var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1); and
    piwikTracker.trackPageView();
    in your Piwik Tracking Code

     {...}
    $(document).ready(checkAds());
    
    function checkAds() {
            if ($(".adsense").height() == "0") {
           document.write("<script>piwikTracker.setCustomVariable(5, 'Adblock', 'True', 'page'); <\/sc" + "ript>");}    
    		else
    		{
    		document.write("<script>piwikTracker.setCustomVariable(5, 'Adblock', 'False', 'page');  <\/sc" + "ript>");}	
    		}	
    {...}
    

    So when done, it will look like this:

    <!-- Piwik --> 
    <script type="text/javascript">
    var pkBaseURL = (("https:" == document.location.protocol) ? "https://localhost/piwik/" : "http://localhost/piwik/");
    document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
    </script><script type="text/javascript">
    try {
    var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
    
    /* Adblock Check */
    $(document).ready(checkAds());
    
    function checkAds() {
            if ($(".adsense").height() == "0") {
           document.write("<script>piwikTracker.setCustomVariable(5, 'Adblock', 'true', 'page'); <\/sc" + "ript>");}    
    		else
    		{
    		document.write("<script>piwikTracker.setCustomVariable(5, 'Adblock', 'false', 'page');  <\/sc" + "ript>");}	
    		}	
    
    /* end Adblock Check */
    
    piwikTracker.trackPageView();
    piwikTracker.enableLinkTracking();
    } catch( err ) {}
    </script><noscript><p><img src="http://localhost/piwik/piwik.php?idsite=1" style="border:0" alt="" /></p></noscript>
    <!-- End Piwik Tracking Code -->
    

    Once you are done you will be able to get some fun data like below:

    Piwik
    adblock tracking Piwik Analytics

    Google
    adblock-unblock-google-analytics

    There you go– now find out how many users actually see you ads and how each type behaves on your site.

    About

    About The Author: Adrian has over 12 years experience in Digital Marketing and Analytics. He currently works as a Marketing Head of Community at Vanilla Forums, a modern forum software platform that allows clients to connect and engage their communities and customers. Adrian has extensive experience with Google and Piwik (Matomo) Analytics. He lives and works in Montreal.