Tracking Silverlight-enabled Browsers via Analytics

This post describes how you can use Google Analytics to track users browsing and visiting your site with Silverlight-enabled browsers in the interim... until Google updates its engine to natively track Silverlight versions.

Web analytics tools provide information around browser capabilities amongst several other things, which allows understanding the specific audience for your web site from a technology perspective. I use Google Analytics on my site, and the reports I get include information about Flash availability, and specific versions. I am hoping the analytics tools will soon be updated to cover Silverlight out-of-the-box (hint, hint...). My curiousity around this was triggered by an email thread yesterday, and I wanted to see if I could somehow report Silverlight installs manually to the analytics engine, and then look at the stats through their reporting interface.

After a little poking around, I found that Google Analytics has support for reporting a user-defined field. They have an API that becomes available after you have included their script (urchin.js). The specific method of interest here is __utmSetVar (strange ... naming seems to indicate it's an internal API), but its clearly documented for this purpose. Here is the script you can use on your page:

function onLoad() {
    var version = getSilverlightVersion();
    if (version) { __utmSetVar(version); }
}

function getSilverlightVersion() {
    var version = '';
    var container = null;
    try {
        var control = null;
        if (window.ActiveXObject) {
            control = new ActiveXObject('AgControl.AgControl');
        }
        else {
            if (navigator.plugins['Silverlight Plug-In']) {
                container = document.createElement('div');
                document.body.appendChild(container);
                container.innerHTML= '<embed type="application/x-silverlight" src="data:," />';
                control = container.childNodes[0];
            }
        }
        if (control) {
            if (control.isVersionSupported('2.0')) { version = 'Silverlight/2.0'; }
            else if (control.isVersionSupported('1.0')) { version = 'Silverlight/1.0'; }
        }
    }
    catch (e) { }
    if (container) {
        document.body.removeChild(container);
    }
    return version;
}

Basically this detects the presence of Silverlight, and if its available, it records the version as the value of the user-defined field. Now your analytics reports will have one of three values: "(not set)", "Silverlight/1.0" or "Silverlight/2.0".

<aside>For those wondering, I didn't use Silverlight.js from the SDK and its Silverlight.isInstalled helper. Instead wrote the script myself. The Silverlight helper doesn't cache version info by creating the control once. So if you check for both 2.0 and 1.0, you'll end up creating two plugin instances, which is wasteful. Its certainly possible to have a different implementation of the isInstalled helper that is smarter.</aside>

One gotcha: Google's analytics engine only supports one user-defined variable you can use (not sure why). So if you are already using it, you're out of luck. Alternatively you can combine multiple bits of information into the single variable, and then use the ability to define multiple analytics profiles and associate some regex-based filtering rules per profile to split your combined user-defined field for reporting purposes.

I have added this script to my site for experimentation - will see how it plays out over the next few days. It will be interesting to see a few things: what sort of numbers I get, and what is the makeup of my audience, do the numbers match the overall Silverlight deployment stats I see, and last but not least, hopefully get some sense of satisfacation from seeing the technology I've work on getting out there in the real world. :-)

I should mention that I am no analytics expert. However, I am impressed with the kind of information I now get since I deployed it on my site. Just reading a bit on this last night, the concept at play here is called "segmentation" or visitor classification. There are complete books devoted on analytics such as Google Analytics 2.0. Fascinating! I've added it to my reading list.

If there are other ways of accomplishing this sort of thing, I'd love to hear about them. For example, I also noticed that 'urchinTracker', the same API used to invoke analytics in the first place, has an overload which can be used to track all sorts of events, and user activity on the page (such as clicking on the Silverlight install prompt), and it could be twisted to track particular browser capabilities as well. The nice thing about that possible approach is it doesn't eat up the single user-defined variable.

Update, 4/14/08: Updated the script to work for Silverlight 2.

Posted on Thursday, 10/4/2007 @ 12:16 PM | #Silverlight


Comments

8 comments have been posted.

Nikhil Kothari

Posted on 10/4/2007 @ 2:09 PM
Just realized - Jeff Wilcox from my team published an article on Silverlight integration with Google analytics as well. Specifically his post at http://blogs.msdn.com/jeffwilcox/archive/2007/10/01/using-google-analytics-with-rich-managed-web-applications-in-silverlight.aspx covers the last bit of my post where I was alluding to using urchinTracker to track user actions and events.

anon

Posted on 10/4/2007 @ 7:34 PM
Isn't this a bit arrogant pointing the finger at Google and blatantly assuming they are going to move heaven and earth to satisfy some bunch of developers? Especially when those developers are using bleeding edge MS technologies?
Your title is annoying whereas your article is actually well crafted and would suit any 3rd party chuck of data you need to report on.

Nikhil Kothari

Posted on 10/4/2007 @ 8:15 PM
Anon - I am not sure where you're coming from in your comment.

The title is about a task that a developer/site admin might be interested in doing - track which users coming to their site have silverlight installed, for instance if they want to understand their user base from that perspective... a perfectly reasonable thing to do. It specifically doesn't say anything about Google. It is meant to indicate why the blog post might be interest to the reader.

The process of tracking is done via analytics and Google Analytics is one of the many services that enables this. Currently it doesn't track Silverlight, which is understandable since the technology is early. There is certainly a hope that at some point their analytics engine will look for Silverlight, but there isn't any unrealistic expectation that they will "move heaven and earth" here... instead, until then, the idea is to use other extensible/custom reporting solutions they provide.

Mark

Posted on 10/6/2007 @ 4:49 PM
I think this is a very good post. We use Google Analytics on our web sites and we would like to start deploying some Silverlight applications. It would be very interesting to see what proportion of our visitors have Silverlight enabled. Maybe you could post some of your figures in a future post.

I'm not sure what the post from 'anon' was about. I didn't see any finger pointing or arrogance. GA is clearly a great application and I think Silverlight will be a great technology. You are just showing us a very useful technique for tracking Silverlight enabled users. Of course GA will eventually track Silverlight browsers as part of the core functionality of GA because they want maintain the excellence of the product not because they are pandering to any group of developers.

In the meantine we can use this technique to help us.

Jonas Follesø

Posted on 10/18/2007 @ 1:24 PM
This is awsome. Didn't know you could do that. Going to implement it on my next blog revision.

Robert Wetzlmayr

Posted on 4/14/2008 @ 10:48 AM
May I direct your attention to the fact that throught the code you refer to 'Silverlight/1.0' and 'Silverlight 2.0' while you mention '(not set)', 'Silverlight/1.0', and 'Silverlight/1.1' (sic!) as a potential outcome of the tests.

Nikhil Kothari

Posted on 4/16/2008 @ 9:33 PM
Robert - ok... taken care of now. I had just updated the script, in case someone copy/pasted...

David Hodges

Posted on 6/11/2008 @ 6:56 PM
Nice one Nikhil, just what I was looking for.
Thanks for posting it up.
The discussion on this post has been closed. Please use my contact form to provide comments.