Tracking Silverlight (1, 2 and 3) support in Google Analytics

Half a year ago I blogged about tracking Silverlight support in Google Analytics. I’ve had a lot of reactions on how to track Silverlight support. In my original article I made use of virtual page views to track Silverlight support. But there are different options available to track Silverlight support.

Tracking through Virtual Page Views

When you make use of Virtual Page Views to track Silverlight support you will get inconsistencies in your Page Views. You will get twice as much Page Views as there really are, not the best solution I think.

Tracking through Events

We can also track information in Google Analytics using Events. But it’s difficult to associate this information directly to the amount of visitors. I think using Events for tracking Silverlight support is the second best option we have.

Tracking through User Defined Value

We can also track information using the User Defined Value. This value is directly associated to the visitor. So even if your visitor takes a look at 10 pages, it will only track this value once. But the difficult thing with Google Analytics is the amount of User Defined Values we can have. It’s exactly one. So if you’re already using this User Defined Value for an other purpose, you will have to take advantage of the other options, like Event Tracking or Virtual Page View Tracking.

My preferred way of Tracking Silverlight support is using User Defined Values. If you’re already using the User Defined Value, your best option for tracking is Events.

Implementing tracking using the User Defined Value

First of all, you’ll need the Silverlight.js file from MSDN Code. This file hasn’t changed for the release of Silverlight 3, so if you’ve already got this file, you don’t have to update it.

Reference Silverlight.js:

<script type="text/javascript" src="**/Silverlight.js"></script> 

You probably already have the Javascript code for Referencing Google Analytics, it’s like this.

<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>

Let’s write a Javascript function to return the current Silverlight version. Although there’s backwards compatibility I only want to know the highest version supported.

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('3.0')) { version = 'Silverlight/3.0'; }
			else 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;
}

Let’s combine this function with the tracking of the value returned in the User Defined Value.

var pageTracker = _gat._getTracker("UA-xxxx-x");
$(function() {
	pageTracker._trackPageview();
	var version = getSilverlightVersion();
	if (version) { pageTracker._setVar(version); }
});

Because I’m using JQuery on my homepage as well, I put this tracking in the ready of page loading.

 

What does it look like in Google Analytics?

Let’s take a look at the statistics for my website containing the User Defined Value since I added support for Silverlight 3 tracking. It’s interesting to see that already a lot of people are using Silverlight 3.

Silverlight usage stats

Ps. This article is cross posted on: Mark Monster’s blog and Silverlight Help.