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 http://msaf.codeplex.com/. 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):

//AnalyticsService.cs
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 ) {
            CompositionHost.Initialize(
                new AssemblyCatalog( Application.Current.GetType().Assembly ),
                new AssemblyCatalog( typeof( AnalyticsEvent ).Assembly ),
                new AssemblyCatalog( typeof( TrackAction ).Assembly ) );
            _innerService.StartService( context );
        }

        public void StopService() {
            _innerService.StopService();
        }

        #endregion        
    }
}
//AnalyticsTracker.cs
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):

<Application 
    x:Class="..."
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"       
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:Analytics="clr-namespace:Common.AnalyticsStats;assembly=Common.AnalyticsStats"
    >

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

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

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:

AnalyticsTracker.Track(this
                       , "Name of group of objects that are tracked"
                       , "Name of the event");
//OR
AnalyticsTracker.Track(this
                       , "Name of group of objects that are tracked"
                       , "Name of the event"
                       , "Name of the object that this event is associated with");
Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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