Sitemap
Bits and Pieces

Insightful articles, step-by-step tutorials, and the latest news on full-stack composable software development

JavaScript Device Detection

4 min readFeb 24, 2019

--

Press enter or click to view image in full size

As a web developer and data scientist, I love Google Tag Manager. GTM makes it super easy to implement trackers on a website. Information about users like duration on page, page scroll, and amount of clicks can be implemented in GTM with just a few clicks.

After I while some of my clients asked me if it was possible to detect the type of device of the users. For example, they asked me if I could detect whether a visitor uses an Apple iPhone X or a Samsung Galaxy S9. After conducting some research I found out that this is only possible for Android devices. Apple does not like to give this information away in the userAgent string.

To solve this problem I created an JavaScript API. This API gathers some characteristics of the device e.g. screen size and GPU information. By combining these characteristics the API is able to detect the right device. And yes, even for iOS devices! I decided to make this API available for everyone for free. This device detection API can be found here.

This API is a JavaScript implementation to discover a lot of information about user’s device. More specifically, you are able to detect the type of device, name of device, touchscreen information, browser information, and much more. In this post we look at what features are available and how to implement them.

Get Siem Peters’s stories in your inbox

Join Medium for free to get updates from this writer.

Currently, it is possible to detect 13 different device characteristics. However, we are constantly updating our API to detect even more information. Until this moment, the following features can be detected:

  • The type of device
  • Device brand
  • Device name
  • Screen height
  • Screen width
  • Pixel density / Screen Ratio
  • GPU
  • Browser name
  • Browser version
  • OS name
  • OS code name
  • The version of the OS
  • Touchscreen capabilities

Implementation in JavaScript

You can find the JavaScript code for implementing the device detection API here. The API is free and no license key is needed. Place the line below in the <head> tag.

Once included in your HTML code you can access the features above. Each piece of information is stored in a variable. These variables have the following names:

As you see it is very easy to implement the device detection API. You are able to use an unlimited amount of requests for free.

Example of implementation in Google Tag Manager

Let’s say that we want to detect the name of the device on each page. Using this information we want to set an alert which prints the device name. This can be done with just 5 steps in Google Tag Manager. The following steps are needed:

1. Set-up a trigger to fire on each page

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

2. Create a tag and select the trigger we just created

Press enter or click to view image in full size
Press enter or click to view image in full size

3. Choose customer HTML as tag type.

Press enter or click to view image in full size

4. Include the following code. This code can be found here.

Press enter or click to view image in full size

5. Publish to your website and test

Conclusion and future blog posts

The device detection API is a very easy JavaScript tool. This tool enables web developers to easily optimize the UX. For example, the API can be used to route mobile users to a specific page. More of these use-cases will be described in future blog posts. If you have any questions feel free to comment below and if like this post please share it with you dev-friends ;)

--

--

Siem Peters
Siem Peters

Written by Siem Peters

Web-developer & Data Scientist

Bits and Pieces
Bits and Pieces

Published in Bits and Pieces

Insightful articles, step-by-step tutorials, and the latest news on full-stack composable software development