lotsofmobiles

List of all Web Enabled Devices and Associated Browsers

Engineering

6/18/2015 5:06 PM

C# .NET Development

Use the 51Degrees .NET API to return meta data about device combinations

Many 51Degrees customers are using the consistent and high quality meta data within 51Degrees to drive related reporting solutions and user interfaces. This blog post contains a short C# code snippet for returning related unique combinations of meta data values. It's intended to show how easy such queries are to produce in .NET with 51Degrees and provide example code for those wishing to access device meta data.

ManyMobiles

Overview

The following code uses a 51Degrees Premium or Enterprise data file to produce a CSV file containing unique combinations of Hardware Vendor, Hardware Model and Browser Name. Other properties available within the data file can also be used. See Property Dictionary for a full list.

Initially the 51Degrees data set is loaded and the properties for vendor, model and browser are retrieved to improve the performance of the subsequent Linq statement. A Linq Select and Distinct state are used with an anonymous class to provide a de-duplicated list from all the Signatures (User-Agents with the irrelevant characters removed - see How Pattern Detection Works) in the data set. The unique list is written to a CSV file.

Source Code Snippet

using (var dataSet = MemoryFactory.Create(deviceDataFile.FullName)) { using (var writer = File.CreateText(Path.Combine( DATA_PATH, "Combinations.txt"))) { // Get the properties for speedier look up. var vendor = dataSet.Properties["HardwareVendor"]; var model = dataSet.Properties["HardwareModel"]; var browser = dataSet.Properties["BrowserName"]; // Get the grouping needed by vendor, model and name. var uniqueCombinations = dataSet.Signatures.Select(s => new { Vendor = s[vendor].ToString(), Model = s[model].ToString(), Name = s[browser].ToString() }).Distinct(); // Write the aggregated data to the output file path. writer.WriteLine("HardwareVendor,HardwareModel,BrowserName"); foreach (var i in uniqueCombinations) { writer.WriteLine(String.Join(",", i.Vendor, i.Model, i.Name)); } } }

Output

A few records from the resulting CSV output are shown below.

HardwareVendorHardwareModelBrowserName
AudiovoxPM8920KITOpenwave
AudiovoxSMT5600Unknown
AudiovoxSMT5600Internet Explorer Mobile
ALCATEL ONE TOUCHOT-665Obigo
ALCATEL ONE TOUCHOT-306Obigo
AcerE310Android
LenovoA288tAndroid
CoolPad8070Android
ZTETU805Android

The example could easily be modified to return different fields, sort the output, or made more efficient by adding indexes to the resulting data sets. The key objective is to show just how easily meta data can be returned from the 51Degrees APIs.