Asking for Directions; An Analogy for User Agents
Here's an analogy for how user agents impact content presented, and how things can go wrong.
Your system is a Robot
Imagine your system is a robot which gives directions, and your users tell the robot where they want to go (eg they request a page from your site)
The robot can give two kinds of directions; directions for driving on roads, and directions for walking on footpaths. Both kinds of directions are functionally equivalent; They take the user to the same place. The only difference is in how they're presented. In this analogy, they're our desktop vs mobile layout. Let's say users who are driving are on desktop, and users who are walking are on mobile.
The robot determines what kind of directions to give by looking at the person asking for them. If they're in a vehicle, they get driving directions (eg, they get the desktop version of your site):
If they're not in a vehicle, they get walking directions (eg, they get the mobile version of the site):
Here's the important parts
The user didn't tell the robot what kind of directions they needed, and the robot doesn't actually know. Instead, it's guessing based on a property of the request: Whether or not it can see the user in a vehicle. The appearance of the user or vehicle is, effectively, the user agent. The user can't (easily) change their appearance, so its mostly reliable. Mostly.
How it can go wrong
User agents don't tell the server what kind of directions are needed directly. The car doesn't have "CAR" written on it. The robot is guessing based on what it can see. In part this is good; Instead of having to teach the robot what every single car looks like, we can just tell it what cars look like in general.
This can also cause problems. The robot only knows what a vehicle is if you teach it to do so. Robots aren't sentient; They need rules to describe the world. Let's say you taught the robot that a vehicle is anything with 4 wheels. This works great on cars; They have four wheels and get driving directions. It works for people on foot; They have no wheels and get walking directions. Now what if a user comes along on a motor bike?
The robot interprets what it's seeing incorrectly, and returns walking instructions... Even though a motorbike is a vehicle. The user isn't at fault; They aren't responsible for telling the robot what they're doing. The motorbike isn't at fault; It isn't pretending to be something it's not, nor is it missing anything it should have. The problem is entirely with how the robot determines what kind of device requires what kind of directions.
Can you make it less wrong?
User Agents are unique for every single device, operating system, browser, browser version and library versions. While it's possible to be mostly correct, you're still likely to encounter problems. For instance, say you want to improve your robot. You can't tell it that "anything the driver is sitting inside" is a vehicle, because motorbikes don't enclose the rider. What about "Anything with wheels is a vehicle"? OK, that works for motorbikes, but pedal bikes have wheels too:
Maybe the bike rider is OK with road directions... But skateboards, and Scooters, and unicycles have wheels as well, and most of those either won't want or legally can't drive on the road.
Maybe your robot should look for a motor... But the motor isn't visible, so you'd have to look for a tailpipe instead. Which works great... Except that Electric cars don't have tailpipes, so your robot won't see them as a vehicle!
How to make reliable use of User Agent Sniffing
The simple answer is don't. As shown above, fully accurate user agents sniffing requires you to accomodate all possible devices and situations, in advance. This is a massive task, and likely a waste of time for your team.
Generally speaking, it's considered a better practise to make use of Responsive Design principles.
If you must use User Agent sniffing, we recommend:
- Using an Open Source library, as these are likely to have been created with the widest range of device support
- Provide a means for easily customers to switch between mobile and desktop versions of your site in case you get it wrong
- Provide a unique mobile and desktop URL, so users can bookmark which kind of site they want to visit