DEV Community

Cover image for Who's on your site?
Joan Awinja Ingari
Joan Awinja Ingari

Posted on

Who's on your site?


Website traffic refers to the web users who visit your site.
Web traffic is measured in visits and is a great way to measure an online business effectiveness at attracting an audience.
visitors come to your site because they are obviously interested in the content in there.

Aside from being able to check the number of times visitor have been to your site, It is also possible to get more details of the visitor by checking their user agent.

So what exactly is a user agent?

Everyone that is browsing the web right now has a user agent. It’s the software that acts as the bridge between you, the user, and the internet.

The User-Agent request header is a characteristic string that lets servers and network peers identify the application, operating system, vendor, and/or version of the requesting user agent.

Example user agent string:

Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36
Enter fullscreen mode Exit fullscreen mode

How Does a User Agent Work?

When your browser (or similar device) loads a website, it identifies itself as an agent when it retrieves the content you’ve requested.

Along with that user-agent identification, the browser sends a host of information about the device and network that it’s on.

This is a set of really important data for web developers since it allows them to customise the experience depending on the user agent that’s loaded the page.

Let's get the user agent!

For this task, we'll use python's flask framework.
you can find the code here:

from flask import Flask, request

app = Flask(__name__)

@app.route('/', methods=['GET'])
def hello():
    data = request.headers.get('User-Agent')
    return data
if __name__ == '__main__':
Enter fullscreen mode Exit fullscreen mode

This displays:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36
Enter fullscreen mode Exit fullscreen mode

Let's breakdown the returned string

User-Agent: <product> / <product-version> <comment>

format for web browsers:

User-Agent: Mozilla/5.0 (<system-information>) <platform> (<platform-details>) <extensions>

Remember: Your results will be unique to your computer and network.

Mozilla/5.0: The user agent application is Mozilla version 5.0. You will notice that most user agents start with a Mozilla Version. This is to says the browser is Mozilla-compatible. For historical reasons, almost every browser today sends it

(Macintosh; Intel Mac OS X 10_15_6): Details of the system in which the browser is running. The operating system is OS X version 10.15.6 (and is running on a Mac).

AppleWebKit/537.36: WebKit is the web browser engine. It is an open source rendering engine developed by Apple.

(KHTML, like Gecko): originally developed for Konquerer on Linux's KDE desktop – added the words “like Gecko” so they'd get the modern pages designed for Gecko, too.
Gecko is a browser engine developed by Mozilla. Gecko is designed to support open Internet standards, and is used by different applications to display web pages.

Chrome/85.0.4183.83 Safari/537.36:The client is Chrome Version 85.0.4183.83 and is based on Safari version 537.36.

Now, lets parse this data

Parsing means taking a stream of text and breaking it into meaningful chunks.

From the user agent string we can get browser, device and os information. Python's regex is the best tool to retrieve this data for us. To achieve this we would have to:

  1. Come up with a list of all browsers, devices and os.
  2. create a file and add lists as so:
browser = [], os = [], device= []
Enter fullscreen mode Exit fullscreen mode
  1. In, add the regex functionality to derive the user agent details.

Lucky for us, there are several open source tools that have been built to help us with this task.

We'll use the appropriately names library user_agent_parser
found here ->

Install it as so: pip install ua-parser user-agents

modify the code to look like this;

from flask import Flask, request
from user_agents import parse

app = Flask(__name__)

@app.route('/', methods=['GET'])
def hello():
    ua_string = request.headers.get('User-Agent')
    user_agent = parse(ua_string)

    data = {
        # Accessing user agent's browser attributes
        "browser": user_agent.browser,
        "browser_version": user_agent.browser.version,
        "browser_version_string": user_agent.browser.version_string,

        # Accessing user agent's operating system properties
        "os": user_agent.os, 
        "os_version": user_agent.os.version,
        "os_version_string": user_agent.os.version_string,

        # Accessing user agent's device properties
        "device": user_agent.device,
        "device_brand": user_agent.device.brand,  
        "device_family": ,
        "device_model": user_agent.device.model

    return data

if __name__ == '__main__':
Enter fullscreen mode Exit fullscreen mode

So what can you do with this user agent data?

The most immediate advantage is it allows developers to customise the experience depending on the user agent that’s loaded the page.

For countries such that are strict when it comes to the use of google analytics and facebook pixels, the user agent can be used to create algorithms that can identify if the same user has visited the site more than once.

It can also tell you the type of devices that are being used to access your website.

In conclusion, user agent technology may be old technology that will soon be phased out, until then we can use it to determine which web version of the website is being served to the user and coming up with tracking algorithms and knowing the types of devices that are being used to access your site. This will hopefully help you make better business decisions that will be profitable to your organisation.


Top comments (0)