<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/12/tracking-air-usage-with-a-cust.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2008://34.34619-</id>
  <updated>2009-11-02T17:13:48Z</updated>
  <title>Comments for Tracking AIR Usage With a Custom User Agent (http://www.insideria.com/2008/12/tracking-air-usage-with-a-cust.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2008://34.34619</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/12/tracking-air-usage-with-a-cust.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=34619" title="Tracking AIR Usage With a Custom User Agent" />
    <published>2008-12-12T01:34:54Z</published>
    <updated>2008-12-12T02:47:53Z</updated>
    <title>Tracking AIR Usage With a Custom User Agent</title>
    <summary>Here&apos;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?</summary>
    <author>
      <name>Andrew Trice</name>
      
    </author>
    
    <category term="Blogs" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[Here's another handy AIR tip that goes hand-in-hand with my previous tip on <a href="http://www.insideria.com/2008/09/air-tip-accessing-application-descriptor.html" target="_blank">accessing application descriptors</a>.   Did you know that you can customize the user-agent setting for your AIR applications?
<br/><br/>
ok... ok...  You might be wondering 1) what is the user agent, and 2) why would I care?
<br/><br/>
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: 
<br/><br/>
<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
Name/Version (optional parameters)</pre>
</code>

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

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4</pre>
</code>

</div></div> 
<br/>
And the IE user agent is: 
<br/><br/>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
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)</pre>
</code>

</div></div> 
<br/>
You can read more specifics about user-agent headers from <a href="http://msdn.microsoft.com/en-us/library/ms537503.aspx" target="_blank">MSDN</a>.
<br/><br/>
In AIR, the default user agent is:
<br/><br/>
<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
Mozilla/5.0 (Windows; U; en-US) AppleWebKit/523+ (KHTML, like Gecko) AdobeAIR/1.1</pre>
</code>

</div></div> 
<br/>

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.
<br/><br/>
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.
<br/><br/>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category1">public</span> <span class="category1">static</span> <span class="category1">function</span> <span class="category1">getVersion</span>() : <span class="category2">String</span>
{
    namespace ns = "<span class="quote">http://ns.adobe.com/air/application/1.0</span>";
    use namespace ns;
 
    <span class="category1">return</span> NativeApplication.nativeApplication.applicationDescriptor.<span class="category2">version</span>.<span class="category2">toString</span>();
}

<span class="category1">public</span> <span class="category1">static</span> <span class="category1">function</span> setUserAgent() : <span class="category1">void</span>
{
    URLRequestDefaults.userAgent += 
    	"<span class="quote"> </span>" +
    	NativeApplication.nativeApplication.applicationID + 
    	"<span class="quote">/</span>" +
    	<span class="category1">getVersion</span>();
}</pre>
</code>

</div></div> 
<br/>
And the user-agent output value is:
<br/><br/>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
Mozilla/5.0 (Windows; U; en-US) AppleWebKit/523+ (KHTML, like Gecko) AdobeAIR/1.1 MyApplicationId/1.0</pre>
</code>

</div></div> 
<br/>
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.  
<br/><br/>
Enjoy!
<br/><br/>
___________________________________<br/>
<strong>Andrew Trice</strong><br/>
Principal Architect<br/>
<a href="http://www.cynergysystems.com" target="_blank">Cynergy Systems<br/>
http://www.cynergysystems.com</a><br/><br/>



]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34619-comment:2048745</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34619" type="text/html" href="http://www.insideria.com/2008/12/tracking-air-usage-with-a-cust.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/12/tracking-air-usage-with-a-cust.html#comment-2048745" />
    <title>Comment from Andrew Westberg on 2008-12-12</title>
    <author>
        <name>Andrew Westberg</name>
        <uri>http://www.flexjunk.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.flexjunk.com">
        <![CDATA[<p>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.</p>]]>
    </content>
    <published>2008-12-12T14:17:47Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34619-comment:2048753</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34619" type="text/html" href="http://www.insideria.com/2008/12/tracking-air-usage-with-a-cust.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/12/tracking-air-usage-with-a-cust.html#comment-2048753" />
    <title>Comment from James on 2008-12-12</title>
    <author>
        <name>James</name>
        <uri>http://www.psyked.co.uk</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.psyked.co.uk">
        <![CDATA[<p>Can you completely rewrite the user agent, instead of just appending to it, if you wanted to?</p>]]>
    </content>
    <published>2008-12-12T16:11:29Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34619-comment:2048762</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34619" type="text/html" href="http://www.insideria.com/2008/12/tracking-air-usage-with-a-cust.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/12/tracking-air-usage-with-a-cust.html#comment-2048762" />
    <title>Comment from Matt Braun on 2008-12-12</title>
    <author>
        <name>Matt Braun</name>
        <uri>http://codehinting.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://codehinting.com">
        <![CDATA[<p>Thanks for the great idea Andrew!</p>

<p>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"</p>]]>
    </content>
    <published>2008-12-12T16:54:25Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34619-comment:2048763</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34619" type="text/html" href="http://www.insideria.com/2008/12/tracking-air-usage-with-a-cust.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/12/tracking-air-usage-with-a-cust.html#comment-2048763" />
    <title>Comment from Andrew Trice on 2008-12-12</title>
    <author>
        <name>Andrew Trice</name>
        <uri>http://www.tricedesigns.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.tricedesigns.com">
        <![CDATA[<p>thanks everyone!</p>

<p>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.</p>

<p>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. </p>

<p>Matt, good point.  The namespace should match whichever version of the sdk you are compiling against.  My example is for the 1.0 sdk.</p>]]>
    </content>
    <published>2008-12-12T17:49:25Z</published>
  </entry>

</feed
