Exclude Your Traffic From Jetpack Site Stats April 22 2012

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 foxrunsoftware.net, 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:


add_action( 'template_redirect', 'site_stats_traffic_filter' );



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' );

  }

}

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

The post Exclude Your Traffic From Jetpack Site Stats appeared first on SkyVerge.