Manipulate the User Agent for Accurate Site Stats

Share this article

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.

Frequently Asked Questions (FAQs) about User Agents

What is a User Agent?

A User Agent is a software that interacts with the internet on behalf of a user. It’s a line of code that your browser sends to a website when you access it. This code contains information about your browser type, operating system, and other relevant details. Websites use this information to provide a suitable version of their pages for your specific browser or device.

Why is my User Agent important?

Your User Agent is important because it helps websites to deliver content that is compatible with your device and browser. For instance, if you’re using a mobile device, the website will provide a mobile-friendly version of their page. It also helps in troubleshooting and analytics, as it provides information about the user’s browsing environment.

How can I find my User Agent?

You can find your User Agent by visiting a website that provides User Agent detection services. These websites will display your User Agent string, which contains details about your browser, operating system, and device. Some of these websites include SitePoint, WhatIsMyBrowser, and DNSChecker.

Can I change my User Agent?

Yes, you can change your User Agent. This is often done for testing purposes, to see how a website appears on different devices or browsers. However, changing your User Agent should be done with caution, as it can affect how websites display on your device.

What information does a User Agent contain?

A User Agent contains information about your browser (like Chrome, Firefox, or Safari), your operating system (like Windows, macOS, or Linux), and your device (like desktop, tablet, or mobile). It may also contain information about the browser version and specific features supported by your browser.

Is my User Agent unique?

While your User Agent contains specific information about your browsing environment, it is not unique. Many users will have the same or similar User Agents, especially if they are using popular browsers or operating systems. However, the combination of your User Agent with other information can make your browsing more identifiable.

Can websites block me based on my User Agent?

Yes, websites can block or restrict access based on your User Agent. This is often done to prevent bots or automated scripts from accessing the website. However, it’s important to note that this is not a foolproof method, as User Agents can be changed or spoofed.

What is User Agent spoofing?

User Agent spoofing is when you change your User Agent to appear as a different browser, operating system, or device. This can be done for various reasons, such as testing website compatibility, bypassing website restrictions, or maintaining privacy.

Is it safe to share my User Agent?

Generally, it is safe to share your User Agent, as it does not contain sensitive information. However, it can provide some information about your browsing environment, which could potentially be used for tracking or profiling.

How can I protect my privacy with regards to User Agents?

To protect your privacy, you can use privacy-focused browsers or extensions that limit the information in your User Agent. You can also use a VPN or proxy to hide your IP address, which can be used in conjunction with your User Agent for tracking. However, it’s important to note that these methods are not foolproof and should be used as part of a broader approach to online privacy.

SitePoint TeamSitePoint Team
View Author

Sharing our passion for building incredible internet things.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form