# DeviceAtlas Device Detection API #
The DeviceAtlas Device Detection API provides a way to detect devices based on
the HTTP headers. Using the headers, the API returns device information such as
screen width, screen height, is mobile, vendor, model etc.
To see a full list of properties in DeviceAtlas please visit:
https://deviceatlas.com/resources/available-properties .
### Data File ###
The DeviceAtlas API relies on a device data file to function. DeviceAtlas
provides daily data file updates so it is recommended to download the data file
on a regular basis. This can be done manually from your account page or
automated via the https://deviceatlas.com/getJSON page.
For more information please see:
https://deviceatlas.com/resources/getting-the-data
### Dependencies ###
This library does not depend on any third party libraries.
### Library ###
The DeviceAtlas Device Detection API consists of three libraries and each
library has a separate dll file.
#### DeviceAtlas Common (deviceatlas-common-x.x.dll) ####
Contains the shared libraries which are common between the DeviceAtlas APIs.
When using the DeviceApi and DeviceApiWeb, the dll file must be included to
the project.
#### DeviceApi (deviceatlas-deviceapi-x.x.dll) ####
The main DeviceApi that loads the Device data and detects and returns the
properties for a set of request headers or the user-agent. Client-side
properties can be optionally passed to this library to get more accurate
results.
#### DeviceApiWeb (deviceatlas-deviceapiweb-x.x.dll) ####
A small extension to the main API that allows automatic extraction of the
request headers and client-side properties from the request. It is preferred to
use this library for real-time device detections in web applications.
Importing the System.Web namespace is needed. All three dll files are
required for this library to work.
### The 2.x API Interface ###
* This is the new standard interface, common across the DeviceAtlas APIs.
* This interface is intended to be simple to use, as the interface includes
custom objects for getting the detected property set and getting a single
property value.
* Unlike the former interface, this interface is not a set of static methods.
This allows the interface to have more encapsulation, requiring fewer and
simpler methods. This new API is highly maintainable and flexible for future
improvements and new features.
* The DeviceApiWeb extension makes it possible to do device lookups based on the
request or a whole set of HTTP headers. This allows the API to make a more
precise detection and return properties with more accurate values. For example,
some browsers distribute the user-agent information into different headers, so
it is almost impossible to correctly detect the device properties, such as type
and model from the user-agent header itself. With this new interface, the API
is able to overcome these situations.
#### Basic Usage ####
The API can be used as follows:
##### Inside a Web Application Context #####
Create an API instance and load the data file only once on the first request.
It is recommended to instantiate the API inside Application_Start() method
inside Global.asax or Global.asax.cs:
Global.asax:
```csharp
<%@ Application Language="C#" Inherits="Global" %>
```
Global.cs:
```csharp
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// load the data file in a try/catch block as several exceptions may be thrown
try
{
deviceApi = new DeviceApiWeb();
deviceApi.LoadDataFromFile("/path/to/datafile.json");
// store the DeviceApiWeb instance in Application state
Application.Lock();
Application["DeviceApiWeb"] = deviceApi;
Application.UnLock();
}
catch (Exception e)
{
// handle the exceptions related to loading the data file
}
}
}
```
Lookup the device properties when processing requests:
```csharp
/* (1) get the DeviceApiWeb instance from the Application state */
DeviceApiWeb deviceApi = (DeviceApiWeb)Application["DeviceApiWeb"];
/* (2) look up device properties by current request */
Properties properties = deviceApi.GetProperties();
/* (3) use the properties - e.g. detect mobile device */
// if there is a property named "mobileDevice" and the value is true
if (properties.Contains("mobileDevice", true))
{
// example 1: Get the screen width for image optimization
int displayWidth = properties.ContainsKey("displayWidth")?
properties["displayWidth"].AsInteger(): 100;
// example 2: Get the device vendor name
string vendor = properties.ContainsKey("vendor")?
properties["vendor"].AsString(): "";
// example 3: Touch screen optimization
bool useBiggerIcons = properties.Contains("touchScreen", true);
// example 4: Send Geo Location JS to client?
bool supportsGeoLocation = properties.Contains("js.geoLocation", true);
}
```
##### Outside a Web Application Context #####
To lookup the properties by manually passing a user-agent string or HTTP headers
to the API:
Note that the DeviceApiWeb is an extension of DeviceApi so all functionalities
shown below are also available in DeviceApiWeb.
```csharp
// the detection is done based on (HTTP headers) or user-agent string
/* (1) create an instance with default API configs and load the data file */
DeviceApi deviceApi = new DeviceApi();
try
{
deviceApi.LoadDataFromFile("/path/to/data-file.json");
}
catch (Exception x)
{
// handle the exceptions related to loading the data file
}
/* (2) look up device properties by HTTP headers */
IDictionary headers = new Dictionary() {
{"HEADER NAME", "HEADER VALUE"}
};
Properties properties = deviceApi.GetProperties(headers);
/* (2) or look up device properties by user-agent string */
string userAgent = "THE USER AGENT VALUE";
Properties properties = deviceApi.GetProperties(userAgent);
/* (3) use the properties - e.g. detect mobile device */
// if there is a property named "mobileDevice" and the value is true
if (properties.Contains("mobileDevice", true))
{
// example 1: Get the screen width for image optimization
int displayWidth = properties.ContainsKey("displayWidth")?
properties["displayWidth"].AsInteger(): 100;
// example 2: Get the device vendor name
string vendor = properties.ContainsKey("vendor")?
properties["vendor"].AsString(): "";
// example 3: Touch screen optimization
bool useBiggerIcons = properties.Contains("touchScreen", true);
// example 4: Send Geo Location JS to client?
bool supportsGeoLocation = properties.Contains("js.geoLocation", true);
}
```
### Client-side Component ###
In addition to the properties from the data file, properties can be gathered
from the client's browser and used both on the client side and on the server
side. Please see the [ClientSide readme file](README.ClientSide.html) for more
information.
### Usage With Client Side Component ###
In addition to the normal usage, DeviceAtlas has the capability to capture client
side properties and merge them with server side properties.
The "deviceatlas.min.js" file must be included on your webpage in order for it
to detect the client side properties. The client side properties are returned to
the server in a cookie called DAPROPS. The contents of this cookie are
automatically detected with the request headers or can be passed separately,
with Headers or a User-Agent, to get the combined properties back.. Both sets of
properties are used in additional logic to determine other properties such
iPhone and iPad models which are normally not detectable.
#### Inside a Web application context ####
To lookup the properties of the device which sent the request:
The API analyses the request object and will use the client-side properties
if they exist in the cookies.
```csharp
/* Steps (1) and (2) are exactly the same as mentioned previously */
/* (3) use the properties - e.g. detect mobile device */
// if there is a property named "mobileDevice" and the value is true
if (properties.Contains("mobileDevice", true))
{
// example 1: Get the screen width for image optimization
int displayWidth = properties.ContainsKey("displayWidth")?
properties["displayWidth"].AsInteger(): 100;
// example 2: Get the device model name more precisely now
string model = properties.ContainsKey("model")?
properties["mode"].AsString(): "";
// example 3: Touch screen optimization
bool useBiggerIcons = properties.Contains("touchScreen", true);
// example 4: Send Geo Location JS to client?
bool supportsGeoLocation = properties.Contains("js.geoLocation", true);
// example 5: Get the device orientation (degrees)
int deviceOrientation = properties.ContainsKey("bjs.deviceOrientation")?
properties["bjs.deviceOrientation"].AsInteger(): 0;
}
```
#### Outside a Web application context ####
To lookup the properties by manually passing the headers to the API:
Note that the DeviceApiWeb is an extension of DeviceApi so all functionalities
shown below are also available in DeviceApiWeb.
```csharp
// the detection is done based on (HTTP headers) or user-agent string
/* (1) create an instance with default API configs and load the data file
as mentioned previously */
/* (2) look up device properties by HTTP headers */
string clientSide = "PROPERTY1:VALUE1|PROPERTY2:VALUE2|PROPERTY3:VALUE3";
IDictionary headers = new Dictionary() {
{"HEADER NAME", "HEADER VALUE"}
};
Properties properties = deviceApi.GetProperties(headers, clientSide);
/* (2) or look up device properties by user-agent string */
String userAgent = "THE USER AGENT VALUE";
Properties properties = deviceApi.GetProperties(userAgent, clientSide);
/* (#) use the properties - e.g. detect mobile device */
// if there is a property named "mobileDevice" and the value is true
if (properties.Contains("mobileDevice", true))
{
// example 1: Get the screen width for image optimization
int displayWidth = properties.ContainsKey("displayWidth")?
properties["displayWidth"].AsInteger(): 100;
// example 2: Get the device model name more precisely now
string model = properties.ContainsKey("model")?
properties["mode"].AsString(): "";
// example 3: Touch screen optimization
bool useBiggerIcons = properties.Contains("touchScreen", true);
// example 4: Send Geo Location JS to client?
bool supportsGeoLocation = properties.Contains("js.geoLocation", true);
// example 5: Get the device orientation (degrees)
int deviceOrientation = properties.ContainsKey("bjs.deviceOrientation")?
properties["bjs.deviceOrientation"].AsInteger(): 0;
}
```
#### Examples ####
Various examples are included in this package to clearly demonstrate the API
features, usage and some use cases. These examples are very simple and are
heavily commented.
#### Basic Usage ####
Includes six examples. Three simple command line examples which
use DeviceApi to detect and get properties from header sets and client-side
component. These examples show how the headers and client-side components
help getting precise property values from Opera mini browsers and iPhone
devices. Three simple web examples. These examples use the DeviceAtlasWeb and
the request object to automatically detect and get the properties for the request.
Using the client-side-component is shown in these web examples.
#### Redirection ####
This web example uses the DeviceApiWeb to get properties for the current request
and then uses some basic property values to decide which website provides the
most suitable content for the device making the request.
#### Content Adaptation ####
This web example uses the DeviceApiWeb to get properties for the device making
the current request and then uses some basic property values to choose a suitable
template to wrap around the content.
#### Analytics ####
This web example uses the DeviceApi to get properties for user-agents from a
given list. Some properties such as vendor, browser name and device type
are aggregated and the results are displayed as graphs and numbers.
#### Content Targetting ####
This example uses the DeviceApiWeb to detect the device and use some of its
properties to show certain advertisements and download links which may be
related or of interest to the user, considering his/her device.
This is a web example.
Note that in the web examples which use the DeviceApiWeb, the client side
properties are taken into account automatically by the API if the cookie exists
on the browser. This means if the cookie already exists within your browser you
will still see the client side properties in a GetProperties call which has not
included the DeviceAtlas client side component. You can delete the cookie
manually to see the differences between the results from examples which use the
client side component and those that don't.
To run the web application examples, follow these steps:
* Open the Visual Studio project (DeviceApi.sln) and update the data file path in the main .cs.
* Build the project.
* Deploy to your test web server.
* Visit "http://yourwebserver/ExampleName/" to see the results.
Alternatively the .dll can be created by hand via msbuild. Please see the
API doc for further example code on how to use the API.
### Upgrading ###
If you are currently using a DeviceAtlas Enterprise API version prior to 2.0.
Please see the [Upgrade readme file](README.Upgrade.html) for more information.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
_ Copyright (c) 2021 by DeviceAtlas Limited. All rights reserved. https://deviceatlas.com _