Home  >  

Tracking AIR Usage With a Custom User Agent

Author photo
| | Comments (4)
AddThis Social Bookmark Button
Here's another handy AIR tip that goes hand-in-hand with my previous tip on accessing application descriptors. Did you know that you can customize the user-agent setting for your AIR applications?

ok... ok... You might be wondering 1) what is the user agent, and 2) why would I care?

The user agent is a string value that is attached to the headers of a HTTP request when requesting data from a web server. The user agent value is used to identify the application that is requesting the data. The user-agent header is made up of tokens that identify application names and versions. The typical format is:

Name/Version (optional parameters)

The optional parameters would be used to identify additional information about the client application. For example, on my machine the Firefox user agent is:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4

And the IE user agent is:

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; WWTClient2)

You can read more specifics about user-agent headers from MSDN.

In AIR, the default user agent is:

Mozilla/5.0 (Windows; U; en-US) AppleWebKit/523+ (KHTML, like Gecko) AdobeAIR/1.1

Of course this can also be customized... You can customize the user agent so that your air applicaiton identifies itself through HTTP headers. If you include the application name and version, you can use it to track what applications and versions are consuming your data services or public API. You'll be able to measure what percentage of services are consumed by browser-based applications versus AIR applications, and you can even track what versions of your AIR application people are using.

Below you'll find a snippet that will append the AIR application's applicationId and version number to the AIR application's user agent, which is appended to the headers of every service request.

public static function getVersion() : String
{
    namespace ns = "http://ns.adobe.com/air/application/1.0";
    use namespace ns;
 
    return NativeApplication.nativeApplication.applicationDescriptor.version.toString();
}

public static function setUserAgent() : void
{
    URLRequestDefaults.userAgent += 
    	" " +
    	NativeApplication.nativeApplication.applicationID + 
    	"/" +
    	getVersion();
}

And the user-agent output value is:

Mozilla/5.0 (Windows; U; en-US) AppleWebKit/523+ (KHTML, like Gecko) AdobeAIR/1.1 MyApplicationId/1.0

Notice that the "MyApplicationId/1.0" value is appended to the end of the string. These values are retrieved from the AIR application descriptor file. With this, you could track distribution of your application versions, or the percentage of Flex/website users versus AIR/desktop users.

Enjoy!

___________________________________
Andrew Trice
Principal Architect
Cynergy Systems
http://www.cynergysystems.com


Read more from Andrew Trice. Andrew Trice's Atom feed

Comments

4 Comments

Nice elegant solution. In theory, I think this could also add a simple layer of security if your server-side was designed to only accept certain user-agents. It's not strong security by any means, but it would add an extra layer to the onion.

James said:

Can you completely rewrite the user agent, instead of just appending to it, if you wanted to?

Matt Braun said:

Thanks for the great idea Andrew!

Worth noting is that the namespace in your code sample's getVersion method should probably be changed for anyone doing AIR 1.5 applications to "http://ns.adobe.com/air/application/1.5"

Andrew Trice said:

thanks everyone!

Andrew, good point. This could be a good way add additional security, however it is very easy to circumvent, so it shouldn't be the only security measure.

James, yes, I believe it can be completely overwritten. However, you should try and keep it in the token/version format to maintain standards compliance.

Matt, good point. The namespace should match whichever version of the sdk you are compiling against. My example is for the 1.0 sdk.

Leave a comment


Tag Cloud

Question of the Week: Dream App

If you had an unlimited budget and unlimited resources what application would you build and why would you build it?

Answer

Latest Features

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.