Real-time Google Analytics with MVVM

by Geert 18. January 2012 21:13

Inside Catel, there are several secret gems. One of these gems is the possibility to audit everything that happens in the view models (property changes, events, commands, etc). This is very useful for logging, or in the case of this blog post, real-time tracking with Google Analytics.

Show me first

Below is a movie that demonstrates the example application with real-time tracking:

Cool, now show me the code

The code is pretty simple. First you need to create an implementation of the IAuditor interface. The prevent that you need to implement all methods, there is a convenience implementation in the form of the AuditorBase. For the Google Analytics implementation, we use Web-Analysis.net (also used in the Microsoft Silverlight Analytics Framework).

   1: /// <summary>
   2: /// Google Analytics auditor.
   3: /// </summary>
   4: public class GoogleAnalytics : AuditorBase
   5: {
   6:     private readonly ApplicationTracker _appTracker;
   7:  
   8:     public GoogleAnalytics(string apiKey, string applicationName)
   9:     {
  10:         Argument.IsNotNull("apiKey", apiKey);
  11:         Argument.IsNotNull("applicationName", applicationName);
  12:  
  13:         _appTracker = new ApplicationTracker(apiKey, applicationName);
  14:         _appTracker.StartSession();
  15:     }
  16:  
  17:     public override void OnCommandExecuted(IViewModel viewModel, string commandName, ICatelCommand command, object commandParameter)
  18:     {
  19:         _appTracker.TrackEvent(ApplicationTrackerCategories.Command, string.Format("{0}.{1}", viewModel.GetType().Name, commandName));
  20:     }
  21:  
  22:     public override void OnViewModelCreated(Type viewModelType)
  23:     {
  24:         _appTracker.TrackPageView(viewModelType.Name);
  25:  
  26:         _appTracker.TrackCustomEvent("ViewModel.Created", viewModelType.Name);
  27:     }
  28:  
  29:     public override void OnViewModelCanceled(IViewModel viewModel)
  30:     {
  31:         _appTracker.TrackCustomEvent("ViewModel.Canceled", viewModel.GetType().Name);
  32:     }
  33:  
  34:     public override void OnViewModelSaved(IViewModel viewModel)
  35:     {
  36:         _appTracker.TrackCustomEvent("ViewModel.Saved", viewModel.GetType().Name);
  37:     }
  38:  
  39:     public override void OnViewModelClosed(IViewModel viewModel)
  40:     {
  41:         _appTracker.TrackCustomEvent("ViewModel.Closed", viewModel.GetType().Name);
  42:     }
  43: }

Last but not least, you need to register the auditor in the AuditorManager.

   1: AuditingManager.RegisterAuditor(new GoogleAnalytics(myApiKey, "Catel Analytics Example"));

Easy huh, creating real-time Google Analytics for your app Glimlach. The advantages is that you can now see exactly what your users are doing and what features of your application are used most (and thus should have a higher priority for support calls).

Tags: ,

C# | MVVM

Pingbacks and trackbacks (2)+

Comments are closed

About the Author

Geert van Horrik is a independent freelance software developer since January 1st, 2007. Since then he was been working on several projects from C++ to C# (WPF, ASP.NET, etc). Currently he loves to write his software using WPF (or Silverlight if WPF isn't an option).

Lately, Geert is spending a lot of time on Catel, a free open-source MVVM Framework for WPF and Silverlight. Actually, it's more than "just" an MVVM Framework, it's a complete application library!