Wednesday, February 29, 2012

Windows Phone - Google Analytics tracking

Yesterday I had a look at the options available to Windows Phone developers wanting to add Google Analytics tracking to their Windows Phone apps. I had expected this to be a bit tricky since Microsoft and Google aren't best friends neither of them wish to promote the other's products. I was right.

There's something called Microsoft Silverlight Analytics Framework with support for a whole bunch of different analytics services and this seems to be the recommended framework if you want to track user behavior in WP apps. Unfortunately this doesn't come as a NuGet package so you need to download and install a separate MSI. I'm aware of the fact that the NuGet package manager isn't an official Microsoft product, but it really does simplify the process of installing third-party libraries.

Since I'm doing this with my Android hat on I was expecting something at least remotely similar in setup time to EasyTracker. I was wrong. Not only does it require me to write an AnalyticsService and AnalyticsTracker class and add the service to the App.xml, I also need to make sure to add the right dlls to my project (NuGet would have done this for me). Among the required dlls there's one called System.ComponentModel.Composition.dll and you would expect to find that one in the component called ComponentModel, but no, if you add that one you'll get a runtime error related to a class called CompositionHost. What you need to do is to make sure to add the Microsoft.SilverlightMediaFramework.Compatibility.Phone dll, because that's the dll that contains the correct version of the CompositionHost...

I finally figured it out and now I have the whole thing set up complete with dependency injection and all:

public class MyClass
 [Inject]
 public IAnalyticsTracker AnalyticsTracker { get; set; }

        public void SomeFunction()
        {
   AnalyticsTracker.TrackEvent("MyCategory", "MyName");

Note: Page tracking can be handled automatically by the framework so there's no need to do this manually.

No comments:

Post a Comment