Nick's .NET Travels

Continually looking for the yellow brick road so I can catch me a wizard....

How’s my Windows Phone 7 application being used? Getting started with the Microsoft Silverlight Analytics Framework for Windows Phone development

Microsoft has just released an update to the Silverlight Analytics Framework which now includes support for Windows Phone development. This gives you a standard approach to include usage tracking within your Silverlight application that is somewhat independent of the tracking site/service you use. I say somewhat as each service requires a custom behavior to be created in order for it to be used with the framework. Luckily the major tracking services already have behaviors available for Silverlight 3 and 4.

To get started tracking usage within your Windows Phone application get the updated version of the Microsoft Silverlight Analytics Frameworkand run through the installer.


We’ll cover using the Console output and Google Analytics to track usage but you can use the behavior that corresponds to the tracking service you use (at the time of writing this only includes Webtrends, Google Analytics and the generic Service Oriented Analytics).

In this case we’re going to create a new Windows Phone application but there is no reason why you can’t add the event tracking to an existing application (just skip the first step of creating a new application).


Next, add the following references to your application:

Google.WebAnalytics (only required if you want to use Google Analytics to track events)


Due to some of the limitations of the Silverlight implementation on Windows Phone there is an additional step that is required in order for the MSAF to work properly. Add a new class called AnaltyicsApplicationService to your project can update the code to look like the following:

using System.ComponentModel.Composition.Hosting;
using System.Windows;

public class AnalyticsAppplicationService : IApplicationService
    public void StartService(ApplicationServiceContext context)
        CompositionHost.Initialize(new AssemblyCatalog(
            new AssemblyCatalog(typeof(Microsoft.WebAnalytics.AnalyticsEvent).Assembly),
            new AssemblyCatalog(typeof(Microsoft.WebAnalytics.Behaviors.TrackAction).Assembly));

    public void StopService() { }

Next, update your App.xaml to look like the following. If you are adding MSAF to an existing application the important part in this code snippet is the creation of the two additional ApplicationLifetimeObjects, AnalyticsApplicationService and WebAnalyticsService.


    <!--Application Resources-->

            Launching="Application_Launching" Closing="Application_Closing" 
            Activated="Application_Activated" Deactivated="Application_Deactivated"/>


We’re going to add some behaviors to the pages of the application so let’s jump across to Expression Blend. You can easily do this by right-clicking on the Windows Phone application project and clicking the Open in Expression Blend link


Make sure that you rebuild the application within Blend so that the various windows are all up to date. In the Assets window, under Behaviors > Analytics, you’ll see that there are a number of different behaviors that you can drag onto your application to track events.


Start by dragging both the ConsoleAnalytics and GoogleAnalytics behaviors on the LayoutRoot node in the Objects and Timeline window. In this example we’re also going to track a button click event, so you’ll have to add a button to the page. Then you can drag the TrackAction behavior onto the button. This should give you the following tree in the Objects and Timeline window.


There are some additional properties you will need to set on the GoogleAnalytics behavior. Whilst the Category isn’t required, it’s a good idea to set it so that you can easily filter the event data by category. I’d suggest at least making the Category application specific.


The Web Property ID is required and needs to correspond to the ID of your Google Analytics account. When you log into Google Analytics, you can find this ID on the first page, along side the list of profiles.


You also need to configure properties on the TrackAction behavior. In this case the behavior is responding to the Click event of the button. We’re also going to specify both a Category and Value that will be passed through to Google Analytics (don’t include any user information that can be identified unless you’re going to be using SSL!)


And that’s all there is to it. Run your application up and you will see a basic page where you can go ahead and click the button. The Google Analytics event tracking will record application start up and the button click event.


Now you might be wondering how you’re going to view the console output. Well there are two ways: Firstly, assuming the debugger is attached you can view the console output in the Output window in Visual Studio. Alternatively, if you’re running the application on the emulator you can enable the console output window. This is done by setting the \HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\XDE\EnableConsole registry key to 1 – you’ll most likely have to create this entry as it isn’t there by default.


With this registry key set, if you restart the emulator, you should see a command prompt window appear that lists the console output from the emulator.


Ok, now the last thing to do is to go back to Google Analytics and check that the events are coming through. Be warned that there can be a substantial delay between raising the events and them appearing in the Google Analytics window. To find the event tracking you have to drill into the Content > Event Tracking node on the left side of the page.


That’s all there is to getting started with the MSAF for Windows Phone.


Issues: At the moment the current installer doesn’t work as described above. There are a couple of issues, one relating to the binaries that are installed and the other relating to the Google Analytics implementation for WP7. The installer is also missing half the source files for the WP7 sample project

1 & 3) The first and third issues can be resolve by downloading the actual source for the MSAF. This includes all the source code for the sample project and will allow you to recompile the MSAF. Download the source from the Source Code tab of the MSAF codeplex site

2) The second issue relates to the way the code detects whether to use ssl or not for downloading the Google Analytics event tracking image. Essentially this code attempts to detect whether the uri for the current document uses ssl or not. Clearly for a WP7 app this isn’t going to work. The work around for this is to decompile the GoogleAnalytics assembly (not sure why this isn’t checked into codeplex), modify the code and recompile the source. The following link contains the Google Analytics assembly along with the working test project.

MSAF with Fixed Google Analytics Download

Comments (1) -

  • Michael S. Scherotter

    3/08/2010 9:59:14 AM |

    Thank you so much for the thorough review!  Please post your suggestions in the Issue Tracker on the MSAF site.

  • UFC 125

    15/12/2010 4:02:10 AM |

    hi! i like your blog., its very interesting and i learn a lot it. thank for the information here.....

  • Allen Carroway

    21/05/2011 10:32:44 PM |

    Even you will find several running for years, when you begin your barefoot working training the very first thing you should know is that you will need to cut way back on your usual routine, and begin as though you were merely getting started working.

Pingbacks and trackbacks (1)+

Comments are closed