Published: January 9, 2024

When operating a website like the one you are visiting (www.danyelkoca.com) it is crucial to get insights about people visiting your site.
You can't improve what you don't measure.
Peter Drucker
One important insight about your users is the device and browser they are using while they are visiting your site.
Why? This information can help you improve your site by examplary actions below:
Now let's deep-dive into the code!
In order to identify the device, we use the window.navigator.userAgent function in JavaScript.
This function returns a string as below:
console.log(window.navigator.userAgent);
// >> Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
The result is what I see on my computer, which is a MacBook Pro used with Chrome Browser.
Let's analyze the output:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/120.0.0.0 Safari/537.36We see that this function not only outputs the device
Macintosh; Intel Mac OS X 10_15_7but also returns a bunch of other information like
Mozilla/5.0, AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36For now, we focus on the part of the output that signifies the device
Macintosh; Intel Mac OS X 10_15_7It gets it right that Im accessing this website through a MacBook, however there is no information about the model of this MacBook (E.g. Air, Pro)
Moreover it gets multiple things wrong. My MacBook is not Intel chip (it is M1) and it is not using OSX 10.15.7.
So, when using this function to determine the device, only infer the device, as model of the device and OS running on the device can be misleading.
Using the userAgent string, we can write a regex in order to classify the user's device as mobile or desktop, based on this StackOverflow answer.
let userAgentString = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
function deviceDetection(userAgentString) {
let device = 'Desktop';
if (
/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(
userAgentString
)
) {
device = 'Mobile';
}
return device;
}
console.log(browserDetection(userAgentString));
// >> Desktop
Now let's focus on the part of the output that signifies the browser
Mozilla/5.0, AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36Browser detection thorugh userAgent is notoriously convoluted as this output itself includes 3 browsers:
According to MDN Web Docs below table can be used to infer about the browser using UserAgent string:
| Engine | Must contain | Must not contain |
|---|---|---|
| Firefox | Firefox/xyz | Seamonkey/xyz |
| Seamonkey | Seamonkey/xyz | |
| Chrome | Chrome/xyz | Chromium/xyz or Edg.*/xyz |
| Chromium | Chromium/xyz | |
| Safari | Safari/xyz | Chromium/xyz or Edg.*/xyz |
| Opera 15+ (Blink-based engine) | OPR/xyz | |
| Opera 15+ (Presto-based engine) | Opera/xyz |
So let's write a regex to definethe browser based on the conditions provided by MDN.
let userAgentString = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
function browserDetection(userAgentString) {
let browser = 'unknown';
if (/Firefox/.*/.test(userAgentString) && !/Seamonkey/.*/.test(userAgentString))
browser = 'Firefox';
else if (/Seamonkey/.*/.test(userAgentString)) browser = 'Seamonkey';
else if (
/Chrome/.*/.test(userAgentString) &&
!/Chromium/.*/.test(userAgentString) &&
!/Edg.*/.*/.test(userAgentString)
)
browser = 'Chrome';
else if (/Chromium/.*/.test(userAgentString)) browser = 'Chromium';
else if (
/Safari/.*/.test(userAgentString) &&
!/Chromium/.*/.test(userAgentString) &&
!/Edg.*/.*/.test(userAgentString)
)
browser = 'Safari';
else if (/OPR/.*/.test(userAgentString)) browser = 'Opera 15+ (Blink-based engine)';
else if (/Opera/.*/.test(userAgentString)) browser = 'Opera 15+ (Presto-based engine)';
return browser;
}
console.log(browserDetection(userAgentString));
// >> Chrome
We have succesfully classified my browser, which was indeed Chrome. However, the attentive eyes will realize that my userAgent string also satisfied Safari's conditions. Since Chrome condition is tested before Safari; the browser is classified as Chrome, but further testing is needed whether testing for Chrome before Safari always yields the expected results.
By using above 2 functions - deviceDetection, browserDetection - you
can now succesfully classify the device and the broser of your users. Note that
window.navigator.userAgent
is
compatible with all modern browsers, so you can safely use it. Any questions, feel free to let me in below
comment section.
Happy hacking!
Leave comment
Comments
There are no comments at the moment.
Check out other blog posts

2025/07/07
Q-Learning: Interactive Reinforcement Learning Foundation

2025/07/06
Optimization Algorithms: SGD, Momentum, and Adam

2025/07/05
Building a Japanese BPE Tokenizer: From Characters to Subwords

2024/06/19
Create A Simple and Dynamic Tooltip With Svelte and JavaScript
2024/06/17
Create an Interactive Map of Tokyo with JavaScript

2024/06/14
How to Easily Fix Japanese Character Issue in Matplotlib

2024/06/13
Book Review | Talking to Strangers: What We Should Know about the People We Don't Know by Malcolm Gladwell

2024/06/07
Most Commonly Used 3,000 Kanjis in Japanese

2024/06/07
Replace With Regex Using VSCode

2024/06/06
Do Not Use Readable Store in Svelte

2024/06/05
Increase Website Load Speed by Compressing Data with Gzip and Pako

2024/05/31
Find the Word the Mouse is Pointing to on a Webpage with JavaScript

2024/05/29
Create an Interactive Map with Svelte using SVG

2024/05/28
Book Review | Originals: How Non-Conformists Move the World by Adam Grant & Sheryl Sandberg

2024/05/27
How to Algorithmically Solve Sudoku Using Javascript

2024/05/26
How I Increased Traffic to my Website by 10x in a Month

2024/05/24
Life is Like Cycling
2024/05/19
Generate a Complete Sudoku Grid with Backtracking Algorithm in JavaScript

2024/05/16
Why Tailwind is Amazing and How It Makes Web Dev a Breeze

2024/05/15
Generate Sitemap Automatically with Git Hooks Using Python

2024/05/14
Book Review | Range: Why Generalists Triumph in a Specialized World by David Epstein

2024/05/13
What is Svelte and SvelteKit?

2024/05/12
Internationalization with SvelteKit (Multiple Language Support)

2024/05/11
Reduce Svelte Deploy Time With Caching

2024/05/10
Lazy Load Content With Svelte and Intersection Oberver

2024/05/10
Find the Optimal Stock Portfolio with a Genetic Algorithm

2024/05/09
Convert ShapeFile To SVG With Python

2024/05/08
Reactivity In Svelte: Variables, Binding, and Key Function

2024/05/07
Book Review | The Art Of War by Sun Tzu

2024/05/06
Specialists Are Dead. Long Live Generalists!

2024/05/03
Analyze Voter Behavior in Turkish Elections with Python

2024/05/01
Create Turkish Voter Profile Database With Web Scraping

2024/04/30
Make Infinite Scroll With Svelte and Tailwind

2024/04/29
How I Reached Japanese Proficiency In Under A Year

2024/04/25
Use-ready Website Template With Svelte and Tailwind

2024/01/29
Lazy Engineers Make Lousy Products

2024/01/28
On Greatness

2024/01/28
Converting PDF to PNG on a MacBook

2023/12/31
Recapping 2023: Compilation of 24 books read

2023/12/30
Create a Photo Collage with Python PIL

2024/01/19
Anatomy of a ChatGPT Response