I recently installed Jetpack by WordPress, solely because I wanted to use Site Stats, and even that I wanted mostly so that I could see my site traffic in the WordPress iPhone app. I quickly realized that Site Stats was tracking my own internal page views as I browsed around skyverge.com, throwing off my true page view/visitor numbers.

Site Stats comes with some built in ability to exclude site traffic, but I found I needed to tweak it to get exactly the behavior I wanted: only my traffic excluded and no one else’s. The issue and my solution are discussed in the remainder of this article.

Excluding Traffic from Site Stats… So Close

By default Jetpack Site Stats won’t record traffic from logged-in visitors. You can verify this is enabled by clicking the Jetpack top menu item, and then clicking ‘Configure’ under ‘Wordpress.com Stats’:

This will bring you to the Site Stats settings page, which oddly doesn’t seem to be linked to from the main Site Stats dashboard. But regardless, from here you can ensure the box next to ‘Count the page views of registered users who are logged in.’ is unchecked:

So far, and perhaps for a lot of blogs out there, so good, as this will filter out traffic by any logged-in visitors. The problem for me is that this filters logged-in user traffic indiscriminately and as an ECommerce site running WooCommerce I could have any number of visitors logged in as Customers, who’s traffic I most assuredly would want to track. What I really need is a way to exclude only my traffic, as the site’s Administrator. Also since I’m generally, though not always, logged in it would be nice to have a failsafe to always exclude page views from my computer regardless of logged-in state.

A Better Way to Exclude Traffic

My solution is to write a small custom plugin that uses a two-fold approach:

  1. Exclude traffic from any logged in administrator
  2. Exclude traffic from any browser with a certain cookie set

The code to make this happen is short and sweet, here’s essentially what the important lines look like:

function site_stats_traffic_filter() {
  global $current_user;

  if ( ( isset( $_COOKIE['ga_exclude'] ) && $_COOKIE['ga_exclude'] ) ||

       ( isset( $current_user->roles ) && in_array( 'administrator', $current_user->roles ) ) ) {

    remove_action( 'wp_footer', 'stats_footer', 101 );
    remove_action( 'wp_head', 'stats_add_shutdown_action' );
  }
}
add_action( 'template_redirect', 'site_stats_traffic_filter' );

As you can see, if the cookie named ‘ga_exclude’ is set, or the current user is an administrator, the Jetpack actions to include the tracking code are removed. All that remains after installing this plugin is to visit the Site Stats settings page and check the box to ‘Count the page views of registered users who are logged in’ since this plugin takes care of removing the Administrator users from that count. Then, the optional but recommended step of setting the ‘ga_exclude’ cookie in all commonly used browsers. One way to set this cookie is with a simple HTML page that you host on your site, with some javascript like the following:

function createCookie(name,value,days) {

  if (days) {

    var date = new Date();

    date.setTime(date.getTime()+(days*24*60*60*1000));

    var expires = "; expires="+date.toGMTString();

  } else var expires = "";

  document.cookie = name+"="+value+expires+"; path=/";
}

createCookie('ga_exclude',1,365*2);

createCookie function courtesy of one of my favorite reference sites: quirksmode.

Step 1 Download the Plugin

If you’d like to implement this method for your own blog, please feel free to download and install my plugin:

Jetpack Site Stats Exclude

After activating, any visits while you’re logged in as an administrator will be excluded.

Step 2 Exclude Browser (Optional, recommended)

Create and host a page on your server containing the JavaScript above, then visit it from any browsers you use or otherwise want excluded from your stats. You can use the script I use on this very site as a starting point: ga_exclude.html

Published by Justin Stern

Justin is one of our co-founders, and is our resident overengineer. He likes to write developer tutorials and make black magic happen in our plugins. He thinks that writing code is a lot easier than writing words.

14 Comments

  1. Thanks for this. You should consider adding it to the wordpress plugin repo as lots of people are asking about this sort of thing.

  2. Well, I just independently worked through this problem (a wrong choice of keywords meant I missed your post), and came up with the non-cookie side of the solution as a patch to JetPack itself.

    I’m just wondering whether you took up ninja foodstuff’s suggestion and packaged it up as a plugin? I’m going to take a stab at getting someone on the JetPack team to add it as a default, but having a plugin to point at in the mean time would be good!

    • Hey Ben, well it is packaged as a plugin and downloadable right from this page, under the ‘Download the Plugin’ heading. What I didn’t bother doing was to submit it to the WordPress Repository as I wasn’t sure whether there was much benefit of submitting such a small plugin, but perhaps it would be worth it. Thanks for stopping by, sorry you missed the post the first time around!

  3. Thanks for this Justin, I installed/activated the plugin .. but I don’t know how to configure it .. or does it automatically start excluding admin traffic?

    • Hey DG, there’s no configuration for the plugin. If you’re logged in as an administrator, it will exclude your visits. If you want it active even when you’re not logged in, you’d want to create a page on your server that includes that JavaScript snippet above to set the ga_exclude cookie, and visit it with any browsers you want exxcluded. You can use the script that I use on this very site as a starting point: ga_exclude.html

      • I’ve looked at this html file and doesn’t seem to have anything other than a title, color, and body for a simple message. Is that all the file is suppose to be? Or is there suppose to be more?

  4. Awesome, will try now. If it doesn’t work I’ll be back to write a nasty note. Therefore, please accept my heartfelt thanks!

  5. Man, where have you been all my life. LOL. Nah, but for real, this is a must needed plugin and it works great.

    Thanks

  6. I have had the chance to try this yet, but the part when ya mentioned having to check the box saying to track logged in users; does this mean it won’t track any non-logged in traffic then?

    Another idea; wouldn’t it be easier to hack in something simpler? i.e. add a text field in the settings of IP’s to ignore, user can enter their IP(s) in it, then just something like:

    (pseudo-code)

    $current_ip = $_SERVER[‘REMOTE_ADDR’];

    loop through the array of entered IP’s
    if (ip[x] == $current_ip) {
    // one of the IP’s entered matches the current ip, so don’t add tracking code;
    }

  7. Hey Justin,

    Thanks a bunch. I had no problem when i was running on blogspot, where the Google Analytic has its own “don’t track my own views” box, with your plugin, it allows me to know the traffic of my site and without getting bigheaded by the fact that i was the one coming back and forth my site to do some updates :))
    Cheers!!

    • Very happy to hear it! It’s one of those things that becomes less and less important as your site gains traction, but I remember in the early days when you really want to know every time a new visitor shows up, it was something I liked to have set up. Best of luck with your blog!

  8. Looks like something that has been implemented, as the settings have now become more broad by being able to individually select Admins, Users, Editors etc. I’m curious though, just how accurate and genuine are the figures recorded by Jetpack’s site stats?

    • Hey Adam, you’re correct, it looks like they’ve made some improvements to jetpack over years which is great. I don’t know whether there’s anything published on the accuracy of their analytics, any of these analytics services are going to have some degree of uncertainty/error and must be taken with a grain of salt, but that doesn’t really diminish their value, especially as your site grows and you gain more traffic and are dealing with bigger numbers. And you can certainly use a couple: say both Jetpack as well as Google Analytics to get a couple of different readings for comparison.

Hmm, looks like this article is quite old! Its content may be outdated, so comments are now closed.