In my time building apps, it brought me joy each time I look at my usage hours charts and I saw growth month over month. Mind you, my personal apps are just small hobby projects, so the growth is small. But I’m a firm believer that in order to grow and improve, you have to learn how to measure progress. And so here we talk about adding telemetry in a UWP app to measure your app’s growth.
Note: I haven’t tested it myself, but I believe these steps will also work for older Windows frameworks like WPF.
App Center is a product by Microsoft that provides a variety of developer services such as building pipelines, testing, and analytics. Think of this sort of like a competitor to Google Firebase. I like App Center because of their SDK’s ease of use when working with .NET Windows apps, and their charts are good enough for me (with options for advanced queries via exporting to Azure Application Insights). Today we’ll use their analytics services.
Follow this guide to sign up and get started with App Center: https://docs.microsoft.com/en-us/appcenter/sdk/getting-started/uwp. It’s an easy process, so no need for me to reiterate. Once you’ve added the SDK into your app project, we’ll get started.
My apps tend to follow the MVVM pattern, so my ViewModels live in a .NET Standard 2.0 project. This means that my ViewModels cannot easily access AppCenter’s static
TrackEvent method. So to solve this, I have an interface which is injected into my ViewModels. Here’s how the interface looks.
Inside your Universal Windows project, you can add an implementation of ITelemetry. Because we’re back inside the UWP project, we do have access to App Center’s static methods. We’ll use those here. Also, you’ll notice that my sample code contains some extra bits regarding retrieving a user setting to determine if the user disabled telemetry. I encourage you to do the same in your apps. Here’s AppCenterTelemetry.cs:
You’ll notice that
AppCenter.Start() is in the constructor of this class. By adding that here, you can remove it from your
App.xaml.cs constructor (as originally instructed by the App Center Get Started docs). And if you want to get data on the user’s locale, make sure to add
AppCenter.SetCountryCode(new GeographicRegion().CodeTwoLetter); before the
And… that’s it. You can start injecting these into your ViewModels or anywhere else to start tracking events.
In my experience, this dependency injection-based approach was more maintainable in the long run. This is because I’ve had to migrate between telemetry service in the past, but I was calling the vendor’s specific SDK all through my app. This made it a pain change all those lines into AppCenter. So, moving forward, I learned to apply this interface-based approach to save me potential pain in the future. All this has helped me grow my apps over the years.
Hope you get to see growth in your app’s metrics from now on as well.
As mentioned earlier, I encourage developers to provide options in the UI for users to disable telemetry. I will leave this as an exercise for the reader. Or if you need quick code, check out my Ambie White Noise repo on GitHub. There’s a settings toggle that lets users disable telemetry. The link to Ambie is below.
Real world example
Want to see all these used in a real app? My Ambie app is an open source white noise generator and it uses this setup. Feel free to browse the code: https://github.com/jenius-apps/ambie.