• About Us
  • Blog
  • Basket
  • Account
  • Sign In
  •  

Blog

Gain Valuable Insights From Web Logs With Device Detection

Published on Friday, September 12, 2014

Gain Valuable Insights From Web Logs With Device Detection

Use a few lines of Java and device detection to analyse web logs

The Objective

Analyse user agent strings stored in your log file to:

  • See how many visitors use mobile devices.
  • Gain deeper understanding of your users from over 150 device properties such as physical screen size, input method, and device price.
  • Augment your analytics system with additional information.

Prerequisites

This demo uses a pre-formated log file with one user agent string per line. Depending on your log file you may wish to add code that would extract user agents from the log file records. You also need to download a Java distribution of 51Degrees device detector and include the relevant packages in to your project.

Implementation

The general logic is as follows: Open file and start reading user agents line by line performing detection for each user agent. As mentioned earlier, for this demo we are interested in 'IsMobile', 'PlatformName', 'ScreenInchesDiagonalRounded' and 'LayoutEngine' properties. The console output of this program consists of lists for Layout Engines, OS and Screen Inches Diagonal Rounded with corresponding count values for each entry.

The output we expect to see is similar to this:


Total User Agents: 1000001.
Of which 498919 are mobile.
---- Layout Engine ----
- Engine: Lumi used in 32 devices.
- Engine: Blink used in 51705 devices.
...
- Engine: U3 used in 2636 devices.
---- Platform Name (Operating System) ----
- OS: Windows Phone used in 1304 devices.
- OS: Unix used in 12 devices.
...
- OS: iOS used in 145068 devices.
- OS: BlackBerry OS used in 10191 devices.
---- Screen Sizes In Inches ----
- Screen Size in Inches: 22 observed in 39 devices.
- Screen Size in Inches: 46 observed in 7 devices.
...
- Screen Size in Inches: 3 observed in 26982 devices.
- Screen Size in Inches: 4 observed in 212979 devices.


The important bits in this application are:

//Create Provider object with Lite data file.
p = new Provider();

br = new BufferedReader(new FileReader(AGENTS_FILE));

while ((line = br.readLine()) != null) {
    //Read in next user agent and detect.
    match = p.match(line);

    //Incrementing devices read count.
    count++;
    //If device is mobile.
    if(match.getValues("IsMobile").toBool() == true)
        mobile++;
    //Process LayoutEngine.
    temp = match.getValues("LayoutEngine").toString();
    processlayoutEngine(temp);

    //Process Operating System (PlatformName) if possible.
    if (isPremium)
    {
        //Get PlatformName property and process it.
        temp = match.getValues("PlatformName").toString();
        processOperatingSystem(temp);
        //Get ScreenInchesDiagonalRounded property and process it.
        temp = match.getValues("ScreenInchesDiagonalRounded").toString();
        processScreenInchesDiagonalRounded(temp);
    }      
}

In this code we create a new Provider and open file that contains user agents. Then we start reading in user agents line-by-line and for each user agent detection is performed. Count of processed user agents gets incremented and if device is mobile the relevant variable also gets incremented. Then Platform Name, the Layout Engine and ScreenInchesDiagonalRounded get passed no to the relevant functions described below.

/**
 * Function performs a check for LayoutEngine in LE list. If LayoutEngine
 * already exists count for that engine gets incremented. Otherwise a new
 * LayoutEngine is added with count 1.
 * @param layoutEngineName - string containing LayoutEngine to check.
 */
public static void processlayoutEngine(String layoutEngineName)
{
    if (layoutEngines.containsKey(layoutEngineName))
    {
        Integer temp = layoutEngines.get(layoutEngineName) + 1;
        layoutEngines.put(layoutEngineName, temp);
    } else {
        Integer initial = 1;
        layoutEngines.put(layoutEngineName, initial);
    }
}

All functions dealing with processing properties have a similar implementation. The result is stored in the relevant HashMap with String for Key and Integer for count. Arguably this is not the most efficient implementation but it serves the purpose of this demo.

Some device properties like 'PlatformName' and 'ScreenInchesDiagonalRounded' are only available in Enterprise or Premium data files. The code is able to run with both Lite and Premium data. You may want to consider Test-Driving out Enterprise or Premium data files. The only difference when using Premium or Enterprise data is in the way we initialise Provider object:

//If PREMIUM_DATA_FILE not set or empty use default data file.
if (PREMIUM_DATA_FILE != null && !PREMIUM_DATA_FILE.isEmpty())
  {
    p = new Provider(StreamFactory.create(PREMIUM_DATA_FILE));
    isPremium = true;
  } else {
    //Create Provider object with Lite data file.
    p = new Provider();
  }

Try Premium Data Download Source Code

Comments (0)

Documents to download

Mike
>

Products Team

Other posts by Products Team
Contact author

Name:
Email:
Subject:
Message:
x

Tags

.NET 2013 2014 4G 51Degrees 5G A.C.Roma A7 ABI Acer Affiliate Marketing Alcatel Amazon AMP Analysis Analytics Android Android 5.0 Lollipop Android Kitkat Android Lollipop Android Media Stick Apache API Apple Apple TV Archos Asha Asian Market ASP.NET Asus Australia Big Data Black Friday Blackberry Blink Browser C C# Case Study CeBIT CES Chrome Cloud CMS combinations Comparison Competition CoolPad COTW Cron CSS3 Data Data Blog Data File Data Model Daydream Denver Design Desire Eye Desktop Detection Developers Device Device Data Device Detection Device Intelligence Device Popularity Device property Device Types Device Use Display DoCoMo Doogee DotNetNuke Download Drupal Email EReader E-Reader Ericsson Evaluation Event Examples EXPLAY Rio Facebook feature Firefox Firefox OS Fly Foundation Framework France Galaxy S3 Galaxy S5 Galaxy Tab A Galaxy Tab A 8.0 Galaxy Tab A 9.7 Germany git repositories Global Google Google Daydream GSMA HAProxy Hardware Hisense HTC HTC ONE MAX HTC OS HTML5 HTTP Huawei HUAWEI. UPDATE HUDL Huwaei IBC Icemobile Prime 4.0 IE IFA IIS Image Optimiser Image Optimizer India Infographic Ingeniux Internet usage iOS iOS 7 iOS 8 ipad iPhone iPhone 6 IsEmailBrowser IsWebApp Italy Japan Java Javascript Jolla Kentico Keynote Kindle Kindle Fire Kindle Fire HD Leagoo Lenovo LG Location Log File Analysis LTE Lumia Map Memory Meta Data Mi 4S Micromax Microsoft Miia Style Mobile Mobile Analysis Mobile Analytics Mobile Devices Mobile Marketing Mixer Module Motorola MVC4 MWC MWC 2017 MWC16 MyPhone Native Native Apps NET New Release News News Letter Nexus Nexus 6 Nexus 9 NFC NGINX Nokia Non-Mobile NVIDIA Omate On7 OnePlus 5 Opera Opera Mini Operating System Optimisation OS OSX 10.10 OTA Panasonic Patent PC Pebble Performance phablet phone PHP Poland Presentation Press Release Price Band PRIV PS4 Python QMobile QR Codes Redirection Research Reseller Responsive Images RESS Review reviews RIM Ringmark RWD Samsung Scala Screen Screen resolution Screen Size SEO Server Set Box Set Top Box Sharepoint Shark 1 SHIFT phones Sitecore SLUSH Smart TV Smartphone Smartphones Smartwatches Snapdragon Sony Sony Xperia Spain Swedish Beers Symbian Tablet Tablets Tesco Testing Tips Top 5 TOTW TV UDS UK Umbraco Update updates US User Agent UserAgent User-Agent Vendors Version 3 VoLTE VR Wearable Web Web Apps WebKit WebMatrix White Paper Widgets Widnows WiFi Wiko Wileyfox Windows Windows Phone Xbox XBox One Xiaomi Xperia Xperia z Yosemite Z10 ZenFon 2 ZOPO ZTE