Manipulate the User Agent for Accurate Site Stats

SitePoint Team
Share

If you implement any sort of hit monitoring or tracking on your Website, you probably don’t want to include any of your own hits. I check each of my sites on a daily basis to ensure they’re all up and running and that no weird errors have occurred. However, this seriously skews the site tracking I use.

Ordinarily, you could set a "self-specific" cookie. Then, when the tracking script was called, you could simply check to see if that cookie existed, and, if it did, exit the tracking script.

However, if, like me, you’re frankly scared of cookies on the grounds that they:

  1. are difficult to test with,
  2. are called something silly and
  3. aren’t 100% reliable

…and you aren’t fortunate enough to have a static IP address on your Internet connection, you may prefer the alternative method we’re about to discuss.

Furthermore, many Webmasters exclude from tracking certain browsers that are known not to work on their Websites. For example, many sites block any non-Internet Explorer or Netscape browsers, such as Mozilla’s excellent new Firebird and a whole heap of others. Being able to manipulate the user agent to fool the Website into thinking we’re using Internet Explorer 6.0 when we’re really running Firebird 0.7 could be quite handy!

The User Agent

Every browser identifies itself via something called the "user agent". Put simply, this is nothing more than a string of characters containing various bits of info. A typical user agent might look like this:

Mozilla 4.0(compatible, MSIE6.0; Win NT 5.1)

This tells us that the browser is based on Mozilla 4.0 (the majority of browsers are based on Mozilla), is Internet Explorer 6, and also, that the computer is Windows NT 5.1 (a.k.a. Windows XP). A similar user agent for Mozilla’s Firebird browser could look like this:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.5) Gecko/20031007 Firebird/0.7

This information is always available to the server from the browser. We can access these details using a simple bit of PHP:

$agent=$_SERVER["HTTP_USER_AGENT"];

Or, in ASP:

sUserAgent= Request.ServerVariables("HTTP_USER_AGENT")

I prefer using a server-side language for this task over, for example, Javascript, as Javascript can be turned off by the user, which can potentially cause the whole script to fail. For more information on language specific implementations, read the relevant manual or ask at the SitePoint Forums.

Modifying the User Agent on Your Computer

The process by which we can manipulate the user agent is browser- and operating system-specific. I’ll describe the process for the more common browsers on a Windows XP system. The methods will be virtually identical for other Windows-based systems, and reasonably simple on Linux or Mac machines.

First of all, we need to decide on what we’re going to change the User Agent to. All we need to do is append to the existing user agent some sort of unique string with which we can identify ourselves. Pick something really unique, that’s not particularly sensitive (i.e. not a word you use as a password, for example). I’m going to use "weirdbeardmt" — my user ID on SitePoint Forums.

Mozilla and Mozilla Firebird

Fortunately, some kind person has already written an extension to build this functionality directly into Mozilla and Mozilla Firebird. It can be downloaded here.

The extension installs a menu item that allows you to add a custom user agent and do on the fly switching. This is also ideal for testing (and supremely easy if you need to change your user agent to gain access to a blocked Website). For testing, you’ll want to use a custom user agent. Your updated user agent will end up looking something like this:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.5) Gecko/20031007 Firebird/0.7 StumbleUpon/1.901 weirdbeardmt

In case that extension is mysteriously unavailable, all this does is alter the prefs.js file found within the working directory of Mozilla. A quick search for "Agent" should hunt it out for you.

Internet Explorer

Unfortunately, modifying the user agent in IE needs to be a registry hack! If you’re not used to playing with the registry, then it’s advisable that you don’t proceed further with this example, or, at least, that you make a backup before you start. Navigate to:

HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionInternet Settings

Look for the user agent string, and double click the name to adjust it. If you’re using a custom Internet Explorer "skin", such as Netcaptor, then it is also possible to add to your user agent by going to:

HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionInternet Settingsuser agentPost Platform

You can then add a new string there. Your new user agent will look something like this:

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Netcaptor 7.2.1; weirdbeardmt)

Opera 6/7

The functionality to switch user agents is built into Opera (File > Preferences > Network), but Opera doesn’t allow custom agents to be specified. Unfortunately, there is no way around this (as I am assured by the CTO of Opera!).

Using the Ol’ Switcheroo

Now that we have our new custom user agent, how are we going to use it? Well, all we really need to do is search for the existence of the identifier (weirdbeardmt) in the user agent. So, to my tracker script, which is included on every page of each of my Websites, I simply add the following:

<?php 
if(strpos($_SERVER["HTTP_USER_AGENT"],"weirdbeardmt")!=FALSE) {

// continue with the tracking script

}
?>

In ASP, this would appear as follows:

<% 
If Not InStr(Request.ServerVariables("HTTP_USER_AGENT"),"weirdbeardmt") > 0 Then
 'continue with your tracking script
End If
%>

And that, as they say, is that. The only drawback to this technique is that your custom user agent will be delivered to every Web page you visit. This doesn’t make a huge amount of difference, except that a site admin could easily find your "secret word" and use it to identify you. Provided, however, the identifier is nothing sensitive, this amounts to no more than a minor inconvenience.