Application Analytics with App Center
Many times, we talk about DevOps – we get lost in the buzz of automated release, containers, microservices and all that. In my circle, there seems to be little focus on analytics of applications – and I’m not talking about Web Apps. Maybe someday, I will write about in-depth .NET Web Apps Analytics.
Let’s get in, shall we? There are tons of mobile (Phone, Tablet, PC, VR/AR) apps and games these days that needs to be tracked – usage, crashes, event logging.
Why is this important?
You see, that highlighted part there – it’s really important in the cycle just like the other parts.
In this post, I’ll walk through using App Center to monitor applications.
At the time of writing this post, App Center supports the following languages/frameworks (with official documentation)
- Android – https://docs.microsoft.com/en-us/appcenter/sdk/getting-started/android
- iOS – https://docs.microsoft.com/en-us/appcenter/sdk/getting-started/ios
- macOS – https://docs.microsoft.com/en-us/appcenter/sdk/getting-started/macos
- React Native – https://docs.microsoft.com/en-us/appcenter/sdk/getting-started/react-native
- tvOS – https://docs.microsoft.com/en-us/appcenter/sdk/getting-started/tvos
- UWP – https://docs.microsoft.com/en-us/appcenter/sdk/getting-started/uwp
- WPF/WinForms – https://docs.microsoft.com/en-us/appcenter/sdk/getting-started/wpf-winforms
- Xamarin – https://docs.microsoft.com/en-us/appcenter/sdk/getting-started/xamarin
Let’s Get Started
I’ll be using a WPF application (.NET Core 3) for this section.
Register on App Center: https://appcenter.ms
After that, you get instructions on how to instrument you application. Stay right on that page (or visit it later)
Step 1 – Create your Application
I’m using Visual Studio 2019 Enterprise Preview and some Windows Terminal.
First, I create my solution and project from the console (just my preference) in folder.
mkdir WindForceCorp.WPF cd WindForceCorp.WPF dotnet new sln dotnet new wpf -o WindForceCorp.WPF dotnet sln add .\WindForceCorp.WPF\WindForceCorp.WPF.csproj
I then open my solution file either from console or my folder
Let’s do a dotnet run (or just run from Visual Studio) to be sure it works after I put in this.
<Grid> <TextBlock Text="Hello .NET Core WPF" VerticalAlignment="Center" HorizontalAlignment="Center"/> </Grid>
First run was good.
Install App Center NUGET Packages
You need the following NUGET Packages
Microsoft.AppCenter.Analytics and Microsoft.AppCenter.Crashes
At the time of writing this post, use version 2.3.0 Preview (when it’s in GA, you can use the latest stable build)
Accept the license agreements and we’re set on the way.
Add your instrumentation keys
Now back to the appcenter page for our application.
Copy and paste as instructed (for your platform).
For a WPF app, there is no OnStartup method in your App.xaml.cs file so you’ll need create an override method there.
So here’s what it will look like
Now that our app is ready, let’s do a first run with AppCenter – F5.
This is out of the box and once you add it to your application startup, you’re cool.
Let’s login now to appcenter.ms to see the analytics.
Here goes our first view running on my dev machine.
You can drill more into the charts as seen to get more info on what OS your users use, most used device, number of runs, etc.
Now I’m going to write some code and push it on GitHub. To add, view and delete records, no database – just some list of objects. Please note, this is not a complex code nor does follow any standard coding principles.
Not all apps work well, there are bound to be bugs or some try-catch you just do avoid your app crashing. So, when you “catch” or an unexpected error occurs, let’s log that into diagnostics to figure out what went wrong.
Just for some fun, I wrote some bad code.
And automatically, those catches are made.
You know there are some actions you really want to track like how many times this button was clicked, what pages are mostly visited, which page do users spend most time on, etc. Logging helps you track that metric.
In my code, I will just track by the action that is being performed. See below.
And here is the analytics for it.
There are lots of other things you can do, I hope to write more soon. I hope that was worth your time.
Get source codes here.