Windows Phone: Adding Google Analytics Event Tracking

Adding google analytics support to your windows phone apps is very easy. The following steps will guide you, not only to add analytics, but to create an assembly to make custom track calls.

Step 1:
Download “Microsoft Silverlight Analytics Framework” from here You ‘ll need these three DLLs:

  • Google.WebAnalytics.dll
  • Microsoft.WebAnalytics.dll
  • Microsoft.WebAnalytics.Behaviors.dll

Step 2:
Create a new Class Library using Studio and add the references above (my assembly name and namespace is “Common.AnalyticsStats”). You will need the following:

using System;
using System.Windows;
using Microsoft.WebAnalytics.Data;
using Google.WebAnalytics;
using System.ComponentModel.Composition.Hosting;
using Microsoft.WebAnalytics;
using Microsoft.WebAnalytics.Behaviors;

Step 3:
Add the following 2 classes to the project (one is the service and one is the tracker):

namespace Common.AnalyticsStats{
public class AnalyticsService : IApplicationService {
        private readonly IApplicationService _innerService;
        private readonly GoogleAnalytics _googleAnalytics;

        public AnalyticsService() {
            string CallerAssembly = Assembly.GetCallingAssembly().FullName;
            _googleAnalytics = new GoogleAnalytics();
            //You can add up to four Custom Variables, since one is always ProductId.
            //I have the following, but you have to implement some code of your own for the values.
            //_googleAnalytics.CustomVariables.Add( new PropertyValue { PropertyName = "Application Name", Value = Common.App.AppFull } );
            //_googleAnalytics.CustomVariables.Add( new PropertyValue { PropertyName = "Is Trial", Value = Guide.IsTrialMode ? "Trial" : "Full" } );
            //_googleAnalytics.CustomVariables.Add( new PropertyValue { PropertyName = "Device OS", Value = Common.Device.OsVersion } );
            //_googleAnalytics.CustomVariables.Add( new PropertyValue { PropertyName = "Device", Value = Common.Device.DeviceFullNameWithMem } );
            _innerService = new WebAnalyticsService {
                IsPageTrackingEnabled = true,
                Services = { _googleAnalytics, }

        public string WebPropertyId {
            get { return _googleAnalytics.WebPropertyId; }
            set { _googleAnalytics.WebPropertyId = value; }

        #region IApplicationService Members

        public void StartService( ApplicationServiceContext context ) {
                new AssemblyCatalog( Application.Current.GetType().Assembly ),
                new AssemblyCatalog( typeof( AnalyticsEvent ).Assembly ),
                new AssemblyCatalog( typeof( TrackAction ).Assembly ) );
            _innerService.StartService( context );

        public void StopService() {

namespace Common.AnalyticsStats{
    public static class AnalyticsTracker {
        private static object _o;
        [Import( "Log" )]
        public static Action<AnalyticsEvent> Log { get; set; }
        public static void Track( object o, string category, string name ) {
            if( o != null )
                _o = o;
            CompositionInitializer.SatisfyImports( _o );
            Track( category, name, null );

        public static void Track( object o, string category, string name, string actionValue ) {
            if( o != null )
                _o = o;
            Log( new AnalyticsEvent { Category = category, Name = name, ObjectName = actionValue } );

Step 4:
Build your dll and add a reference to your app solution.

Step 5:
Open your App.xaml and at the top add the last line (line in bold):


and at the bottom of your App.xml the following:

            Launching="Application_Launching" Closing="Application_Closing" 
            Activated="Application_Activated" Deactivated="Application_Deactivated"/>
        <Analytics:AnalyticsService WebPropertyId="UA-XXXXXX-XX" />

That’s it! You have Google Analytics! You can track your visitors and test it immediately with the Real-Time feature of google analytics. But there is one more thing, the custom calls. You can push custom, user defined events like this:

                       , "Name of group of objects that are tracked"
                       , "Name of the event");
                       , "Name of group of objects that are tracked"
                       , "Name of the event"
                       , "Name of the object that this event is associated with");

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s