Tuesday, 6 May 2014

Tridion Client-Side Device Detection

The SDL Tridion Context Engine gives you contextual information that you can use to optimise your web experience for different devices - in other words tweak and tune your web site for mobile.

In my previous blog post, I gave a quick overview of our context repository and mentioned that the context repository can give us device properties like screen width and height.

However, if you really want to know width and height of the users' device or browser - then we are often better off discovering it directly from the browser. The user might zoom in or out, they might rotate the screen or they might use a different screen.

The SDL Tridion documentation covers deploying the discovery resolver, in which it says you can add the following to your page:

<script type="text/javascript" src="js/discover-min.js"></script>

This JavaScript file simply discovers a collection of device properties, such as width, height and pixel ratio and uses these over and above those defined in the repository. As with any other context properties these are placed in the Tridion claim store, ready for your use.

SDL Mobile Context Repository Updates

As a quick summary the Context Engine module in SDL Tridion is responsible for resolving context that it places in the ADF claim store so that as a web developer you can optimise the experience based on these contextual properties.

You might be aware that context is resolved on the server based on the information sent by the client. We use a few context resolvers that use the information to enhance the context - these include:

User Agent Resolver
Algorithmic analysis of the user agent.
Discovery Resolver
Resolution of properties discovered by the JavaScript run on the client.
Expression resolver
Evaluation of expressions to resolve properties that are functions of other context properties.
Context Repository Resolver
Resolving properties from a repository of context profiles.

It's the context repository that I'd like to cover in this blog and in particular the context repository updates that are part of the SDL Mobile offering. The context repository is often referred to as a device database since that is the primary usage - however for now I'll continue to refer to it as the context repository, since it has the opportunity to be used way beyond that of device resolution.

The context repository resolver scans the repository using the information sent by the client (e.g. user agent HTTP header) and matches this against a collection of profiles that we've defined ... and continue to define. The context repository resolution is quite a cunning resolution, for example, it allows various aspects of the context - device, browser, OS - to be resolved independently and using trust settings so we can provide intelligent defaults as well as specific overrides. Now the cunningness is just a curiosity and not something we need to delve into ... what it really means to us is that we can manage the repository efficiently, so that as a user of SDL Mobile you get more accurate device resolutions.

Now since we are constantly reviewing new devices coming on to the market, we need to provide updates to the context repository to you. To pick up these updates you will need to be a licensed SDL Mobile user and you will need to contact SDL customer support to whitelist the IP address that you'll be using to pick up these updates. Note that you can update the context repository on one machine that is whitelisted and then distribute to all your servers - so, for example, you can do it once in a dev environment, QA the update and push out to test / production servers AND you don't need to give external web access permission to your production servers to get the update.

To create the context repository, open a command shell and change to the bin directory of your Tridion .NET application or WEB-INF folder of your web application and run:

java -Xms1028m -jar lib/cwd_engine.jar http://context.sdl.com/seed

The command creates a new subfolder called repository. If you need to distribute this repository to other servers in your environment you can copy this folder onto your licensed servers.

As I mentioned earlier we provide updates to this context repository which you can pick up by repeating the above process, except using the http://context.sdl.com/update URL instead:

java -Xms1028m -jar lib/cwd_engine.jar http://context.sdl.com/update

We typically provide updates to the repository every few weeks, but we sometimes do it more often if needed. We intend to provide reports on the updates we provide so that you can make an informed choice of when you should update.

It's interesting to note that the resolvers, other than the context repository resolver, do provide an added resilience to the resolution of some of the context properties. For example the discover resolver will often give you width and height of the device and browser and the user agent resolver will also give you browser and OS version numbers - both independently of the context repository resolution.

The repository on the other hand gives you that extra context resolution resilience, providing more assurance of quality. Think of the repository going the whole nine yards.