Short guide for adding VS App Center telemetry into a Windows app

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

Don’t let their website fool you, App Center also works on native Windows apps even though they never market it.

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.

Getting started

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.

ITelemetry.cs

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.

AppCenterTelemetry.cs

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 Start() call.

All done

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.

Bonus

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.

I’m a software engineer at Microsoft, and I build Windows apps. I created Nightingale REST client. My stories are personal & not Microsoft’s.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store