How to add Silverlight version detection to Google Analytics

by APIJunkie 15. May 2009 06:35

One of the things that are currently missing from Google Analytics is Silverlight detection. Normally this type of information would appear under the browser capabilities section, the same section that lists Flash and Java versions. While I'm sure this will change eventually, it is something that can be very useful to web sites hosting Silverlight applications today.

In order to bridge this detection hole here is a simple solution that would plug this information into Google analytics.

The solution is based on 2 things:

1. Custom visitor segments  - Google Analytics allows us to create user defined values that can be stored and reported upon.

2. Silverlight JavaScript detection code - Code that allows you to detect the installed Silverlight version on the client's browser.

Awhile back I showed how to detect/report the current Silverlight version in JavaScript. Combining this code with Google analytics code to report user defined values is fairly simple. The following code is a rough example that would do the trick:

////////////////////////////////////////////////////

// Google analytics include files

////////////////////////////////////////////////////

<script type="text/javascript">

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

</script>

////////////////////////////////////////////////////

// Report Silverlight version to Google analytics

////////////////////////////////////////////////////

<script type="text/javascript">

var pageTracker = _gat._getTracker("UA-xxxxx-x");

pageTracker._setVar('SLVersion ' + getSilverlightVersion() );

</script>

////////////////////////////////////////////////////

Notes:

1. Remember to replace UA-xxxxx-x with your Google analytics code in the code example.

2. The information you report to Google analytics will appear under user defined values in the visitors section.

3. You can use Advanced Segments to integrate your user defined values into other reports.

4. There is normally a time delay in Google analytics reporting so it might take a few hours before you start to see the effects of your new code.

Good Luck!

Tags:

Silverlight | How To | Google | Analytics

About the author

Name of author

I was first wounded by x86 assembly, recovered and moved on to C. Following a long addiction to C++ and a short stint at rehab I decided to switch to a healthier addiction so I am now happily sniffing .NET and getting hooked on Silverlight.

I am mainly here to ramble about coding, various API’s, Junkies(me especially) and everything else that happens between coders and their significant other.

  James Bacon