Tuesday, November 2, 2010

Tracking Sales Statistics with the Silverlight Analytics Framework for Windows Phone

The official statistics are now available at the App Hub portal. However, this approach is capable of more than sales tracking.

Are you a Windows Phone developer? Are you also curious how well your app performs in the Windows Phone Marketplace? I am!
Some might say it doesn't matter yet, since there aren't many devices out there, but nevertheless I'd like to know my statistics. Also O2 Germany recently reported they have sold out the first batch of HTC HD7 devices with sales in the 5-figure range. Unfortunately we won't see official statistics from the Marketplace any time soon. An App Hub forum member contacted the Microsoft App Hub support and they told him that sales statistics won't be available before January 2011. Bummer!
Fortunately we are app developers and we can implement our own statistic tracking. On the other hand this means we have to setup and pay some service and implement tracking code. Here is where the Silverlight Analytics Framework and Google Analytics come to the rescue.
This blog post provides a step-by-step guide on how to track your app (sales) statistics with the Silverlight Analystics Framework and Google Analystics. Did I mention it's free and not only free for a limited period?
Let's go...

Setup Google Analytics
If you don't have a Google Analytics account, just sign up here. It's free.
  1. Create an empty HTML page with the minimum tags and upload it to some public webspace. If you don't have webspace available, just create a free Dropbox account and put the page in the public folder.

  2. Go to Google Analytics and use the "Add Website Profile" functionality. Choose "Add a Profile for a new domain". Enter the URL of the dummy website.

  3. The next page setup page will show the necessary Java Script tracking code. Copy the code, paste it into the dummy website and upload the website.

  4. Your website profile is now waiting for activation. Go back to the overview and click the Edit link of your website profile. See the little yellow exclamation mark with the text "Tracking Not Installed" at the upper right corner. Click on "Check Status". The status should change soon and the profile will become active. If not, give Google a couple of minutes and retry it then.

Setup the Silverlight Analytics Framework
Now it's time to submit the tracking events from your app to Google Analytics. This task is pretty easy with the help of the Silverlight Analytics Framework.
The Silverlight Analytics Framework is an extensible web analytics framework mainly developed by the Microsoft evangelist Michael S. Scherotter. It's really nice, designed in a flexible way with the use of modern technologies and open source. Google Analytics is just one of the many supported analytics services.
  1. Download the latest version from the Codeplex site and install it or download the latest source code from the repository and build it yourself. I've chosen the latter since Michael recently implemented a nice feature: The Silverlight Analytics Framework for Windows Phone tracks each page view by default. In the changeset 63020 Michael added an IsPageTracking property which allows to disable the page tracking. This feature is part of the latest release 1.4.8.

  2. Add the following assembly references to your project.
    From the Silverlight for Windows Phone Framework: System.Device, System.Windows.Interactivity
    From the Silverlight Analytics Framework: Microsoft.WebAnalytics, Microsoft.WebAnalytics.Behaviors, Google.WebAnalytics, System.ComponentModel.Composition, System.ComponentModel.Composition.Initialization

  3. Create a new class in your project:
    public class AnalyticsService : IApplicationService
       public void StartService(ApplicationServiceContext context)
          // Wire up MEF
             new AssemblyCatalog(
                new AssemblyCatalog(typeof(Microsoft.WebAnalytics.AnalyticsEvent).Assembly),
                new AssemblyCatalog(typeof(Microsoft.WebAnalytics.Behaviors.TrackAction).Assembly));
       public void StopService() { }

  4. Go to the App.xaml file of your project and add the namespace definitions to the first tag:
  5. Then add the service references inside the Application.ApplicationLifetimeObjects declaration in your App.xaml file:
        <local:AnalyticsService />
        <mwa:WebAnalyticsService IsPageTrackingEnabled="False">
                <ga:GoogleAnalytics WebPropertyId="UA-XXXXXXX-X" />
    Most important you need to replace the WebPropertyId in the GoogleAnalytics element with the Id of your newly created Google Analytics website profile.
    I'm not interested in the page tracking, therefore I've set the IsPageTrackingEnabled property to False. You should also keep in mind that page tracking has an impact on the performance of the app. I recommend to disable it.
  6. Since your app is now using a network connection, you need to add the ID_CAP_NETWORKING capability to your WMAppManifest.xml file
That was all to get the Silverlight Analytics Framework connected to your Google Analytics profile. It now tracks the Deactivated, Activated and Started events.
You can find the results here:

Please keep in mind that it usually takes 12 - 24 hours until the tracked data shows up in Google Analytics.

Add sales tracking
There's only one puzzle piece left now, the actual sales tracking. I use two Windows Phone APIs for this. First the LicenseInformation.IsTrial method which tells us if the app runs in trial mode or if it was paid. The second API is the DeviceExtendedProperties class with the DeviceUniqueId key which provides a unique 20 byte long hash for the phone. Such an id is needed to count unique installations.
  1. Add a new class to your project for the explicit tracking from code:
    public class AnalyticsHelper
       // Injected by MEF   
       public Action<AnalyticsEvent> Log { get; set; }
       public AnalyticsHelper()
          // Inject
       public void Track(string category, string name)
          // Track analytics event
          Log(new AnalyticsEvent { Category = category, Name = name, });

  2. Call the Track method when your app launches and provide the necessary information.
    Add this code to the Application_Launching event handler in the App.xaml.cs file.
    private void Application_Launching(object sender, LaunchingEventArgs e)
       var analyticsHelper = new AnalyticsHelper();
       // Get device id
       var value = (byte[])DeviceExtendedProperties.GetValue("DeviceUniqueId");
       var id = Convert.ToBase64String(value);
       // Track launch
       analyticsHelper.Track("Launch All", id);
       // Track if paid
       if (!new LicenseInformation().IsTrial())
          analyticsHelper.Track("Launch Paid", id);
    Gergely Orosz (@Gergely Orosz) gave me the idea to track the statistics only if the phone is connected to Wi-Fi. This is pretty easy to implement, just add an if condition around the tracking: NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211

  3. The app is now using the unique id of the phone, so you have to add the ID_CAP_IDENTITY_DEVICE capability to your WMAppManifest.xml file

As you can see all app launches are tracked in the category "Launch All" and all paid launches in the "Launch Paid" category. This makes it easy to see all installations and all sales directly. Needless to mention that Launch Trial = Launch All - Launch Paid.
You can check if all works with the Fiddler or the TCPView tool while using the Windows Phone emulator. By the way, the tracking is pretty fast. I measured 20 to 40 ms for each Track call when the phone was connected to Wi-Fi.

The tracked events are best viewed when grouped by categories. The screenshot below shows a test with multiple launches of a phone and the emulator. The total number of events is listed at the bottom right of the table. Note that Unique Events aren't significant here since they only represent unique events for one session.

The id (event name) we supply through the AnalyticsEvent is mapped to the Google Analytics Event Action. You can find all event mappings and more info in the relevant Silverlight Analytics Framework documentation.

The update I submitted to the Marketplace works nicely and I hope to see some meaningful statistics in the next weeks. I think the described method is a good way to track your sales and other statistics until the App Hub team provides the official usage / sales statistics. Actually the provided tracking functionality is even useful when the App Hub team provides official statistics. And it's all free without a limited timeframe.

Monday, November 1, 2010

Silverlight is not dead and not undead either

No, it's alive and kicking.
You might have heard about the rumors that came up after the PDC conference and Bob Muglia's interview with Mary Jo Foley. Many people flooded Twitter and other sites with speculations that Silverlight is dead for the web. Microsoft's President of the Server and Tools Division Bob Muglia just clarified the situation.

"Make no mistake; we’ll continue to invest in Silverlight and enable developers to build great apps and experiences with it in the future."

You should also read the blog post from the Silverlight program manager Tim Heuer. Without a doubt he puts all his effort into the success of Silverlight. And not to forget the Microsoft Silverlight evangelist John Papa who also proves his commitment to Silverlight here.

Now let's get back to work...

The picture is an old poster from the movie Santo en el Museo de Cera with the famous Mexican lucha libre wrestler El Santo.