User Agent Parsing: Detect Browsers and Devices
User-Agent strings are sent by browsers with every HTTP request. Parsing them helps identify the browser, operating system, and device type - useful for analytics, troubleshooting, and optimization.
What is a User-Agent?
A User-Agent string identifies the client software making an HTTP request:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
This tells us:
- Browser: Chrome 120
- OS: Windows 10/11
- Device: Desktop
- Engine: Blink
Why Parse User-Agents?
1. Analytics
Understand your users’ browsers and devices for better optimization.
2. Troubleshooting
Debug browser-specific issues reported by users.
3. Content Optimization
Serve different content for mobile vs desktop.
4. Security
Detect bots, scrapers, and suspicious clients.
Using Our User Agent Parser
Our User Agent Parser provides instant parsing:
- Paste a User-Agent string in the input
- Click “Parse” or press Enter
- View results including browser, OS, and device
- Try sample UAs to explore different formats
Parse Your Own UA
Click “My UA” to parse your current browser’s User-Agent.
Common User-Agent Patterns
Chrome on Windows
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Safari on macOS
Mozilla/5.0 (Macintosh; Intel Mac OS X 14_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Safari/605.1.15
Firefox on Linux
Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0
iPhone Safari
Mozilla/5.0 (iPhone; CPU iPhone OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Mobile/15E148 Safari/604.1
Googlebot
Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
Browser Detection
Major Browsers
| Browser | Identifier |
|---|---|
| Chrome | Chrome/ |
| Firefox | Firefox/ |
| Safari | Safari/ (without Chrome) |
| Edge | Edg/ |
| Opera | OPR/ or Opera/ |
| IE | MSIE or Trident/ |
Detection Order
Always check in the right order to avoid false positives:
if (ua.includes('Edg/')) browser = 'Edge';
else if (ua.includes('OPR/')) browser = 'Opera';
else if (ua.includes('Chrome/')) browser = 'Chrome';
else if (ua.includes('Firefox/')) browser = 'Firefox';
else if (ua.includes('Safari/')) browser = 'Safari';
Operating System Detection
Common Patterns
if (ua.includes('Windows NT 10')) os = 'Windows 10/11';
else if (ua.includes('Mac OS X')) os = 'macOS';
else if (ua.includes('Android')) os = 'Android';
else if (ua.includes('iPhone') || ua.includes('iPad')) os = 'iOS';
else if (ua.includes('Linux')) os = 'Linux';
Device Type Detection
function getDeviceType(ua) {
if (/Mobi|Android|iPhone/i.test(ua)) return 'Mobile';
if (/Tablet|iPad/i.test(ua)) return 'Tablet';
if (/bot|crawler|spider/i.test(ua)) return 'Bot';
return 'Desktop';
}
Code Examples
JavaScript
function parseUserAgent(ua) {
const result = {
browser: { name: '', version: '' },
os: { name: '', version: '' },
device: { type: 'Desktop' }
};
// Browser detection
if (ua.includes('Edg/')) {
result.browser.name = 'Edge';
result.browser.version = ua.match(/Edg\/(\d+)/)?.[1];
} else if (ua.includes('Chrome/')) {
result.browser.name = 'Chrome';
result.browser.version = ua.match(/Chrome\/(\d+)/)?.[1];
} else if (ua.includes('Firefox/')) {
result.browser.name = 'Firefox';
result.browser.version = ua.match(/Firefox\/(\d+)/)?.[1];
}
// OS detection
if (ua.includes('Windows')) {
result.os.name = 'Windows';
} else if (ua.includes('Mac OS X')) {
result.os.name = 'macOS';
} else if (ua.includes('Android')) {
result.os.name = 'Android';
result.device.type = 'Mobile';
} else if (ua.includes('iPhone')) {
result.os.name = 'iOS';
result.device.type = 'Mobile';
}
return result;
}
Python
import re
def parse_user_agent(ua):
result = {
'browser': {'name': '', 'version': ''},
'os': {'name': '', 'version': ''},
'device': {'type': 'Desktop'}
}
# Browser detection
if 'Edg/' in ua:
result['browser']['name'] = 'Edge'
match = re.search(r'Edg/(\d+)', ua)
result['browser']['version'] = match.group(1) if match else ''
elif 'Chrome/' in ua:
result['browser']['name'] = 'Chrome'
match = re.search(r'Chrome/(\d+)', ua)
result['browser']['version'] = match.group(1) if match else ''
# Device type
if any(x in ua.lower() for x in ['mobile', 'android', 'iphone']):
result['device']['type'] = 'Mobile'
return result
Best Practices
1. Use Feature Detection
Instead of browser detection, detect features:
// Avoid
if (browser === 'Safari') {
// Safari-specific code
}
// Better
if ('webkitURL' in window) {
// Feature-specific code
}
2. Don’t Rely on Exact Versions
User-Agent strings can be spoofed or modified.
3. Consider Privacy
GDPR and other regulations may apply to UA data collection.
4. Keep Updated
Browser versions and UA formats change frequently.
Bot Detection
Common bot patterns:
const botPatterns = [
'googlebot', 'bingbot', 'slurp', 'duckduckbot',
'baiduspider', 'yandexbot', 'sogou', 'exabot',
'facebot', 'facebookexternalhit', 'twitterbot',
'linkedinbot', 'pinterest', 'crawler', 'spider', 'bot'
];
function isBot(ua) {
const lowerUA = ua.toLowerCase();
return botPatterns.some(pattern => lowerUA.includes(pattern));
}
Conclusion
User-Agent parsing provides valuable insights about your users’ browsers and devices. Use our User Agent Parser for quick, accurate parsing.
Remember: User-Agent strings can be spoofed, so use this information carefully and consider feature detection when possible.