Easily exclude internal analytics tracking
Excluding internal traffic from analytics can be very important in achieving accurate tracking data. Depending on the size of an organization and the number of employees that will be referencing or working on the site as well as the number of visits a the site usually gets, this may be especially important.
For a long time, easily excluding personal traffic has been difficult for many website managers. In this post I 'm going to explain how to easily exclude any internal traffic from analytics.
In order to make sure all of my personal traffic is included from my website, I came up with the following solution:
Dynamic inclusion of analytics tracking code
In the global PHP include used for the footer of my website, instead of simply including Google Analytics' tracking code, I used PHP to conditionally include it:
<?php
if($isbjornenki == 'yes') {
echo '<!-- hiding google analytics tracking code -->';
} else { ?>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ?
"https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost +"google-
analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-1350938-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
<?php } ?>
This will obviously only work if the site has already set the $isbjornenki variable depending or not the visitor is me. In order to determine if it is me or not, I employ two separate methods, explained below.
Exclusion based on dynamic IP
First and foremost, I took advatange of the existing functionality of my own static IP solution and expanded it to save the most recent dynamic IP address of my home internet connection to a restricted directory on my website. Now, all I have to do is check if the IP address in this file matches the IP address of the device requesting the file. This way, whenever I am working from home, all my traffic will automatically be excluded from Google Analytics. In my header PHP include, I added:
$homeip = @file_get_contents('/restricted_directory/latest_home_ip');
if(getenv('REMOTE_ADDR') == $homeip) {
$isbjornenki = 'yes';
}
Exclusion based on a cookie
For whenever I'm working from somewhere other that home, which happens quite a lot, I created a cookie based method of setting the $isbjornenki variable to 'yes'. This way I can visit a restricted page that automatically sets a cookie that lasts as long as I want (or until I clear my cookies) then redirects me back to the regular site. This code is also very simple.
The restricted page that sets a cookie and redirects:
<?php
setcookie("isbjornenki_cookie", 'yes', time()+1209600); /* 2 weeks */
header("Location: http://www.bjornenki.com/");
?>
Expanding the header PHP include to check for a cookie as well as the access IP:
$homeip = @file_get_contents('/restricted_directory/latest_home_ip');
if(getenv('REMOTE_ADDR') == $homeip || $_COOKIE['isbjornenki_cookie'] == 'yes') {
$isbjornenki = 'yes';
}

Comments
Anonymous, Nov. 3, 2009 at 10:40 pm Anonymous, Oct. 16, 2009 at 2:45 am