Adding Application Insights to a Windows 8.1 Universal App
I’ve commonly been asked if Application Insights supports Windows 8.1 Universal Apps. You can be highly successful utilizing Application Insights with Universal Apps. Integration of Application Insights is a few additional steps but very straightforward. In this blog post, I’ll walk through the steps to integrate Application Insights into your Universal App and discuss some of the specific scenarios for Universal Apps. Official documentation for getting started with Universal apps can be found at https://aka.ms/appinsightsuniversalapps.
Integrating the Application Insights SDK
- In the Azure portal, create a new Application Insights resource.
- Go to the Properties blade and copy the Instrumentation Key. This Instrumentation Key is used to identify your telemetry data as belonging to your Application Insights resource and is mandatory for SDK integration.
- Add the Application Insights SDK NuGet package to both Store and Phone projects. NOTE: You need to do this twice, once for each project. Addition of Application Insights SDK will change your app.xaml.cs file in the shared code project with a reference to ApplicationInsights. You will need to add the SDK NuGet package to both projects or you may see a build failure.
- For Windows 8.1 project: http://www.nuget.org/packages/Microsoft.ApplicationInsights.WindowsStore/
- For Windows Phone 8.1 project: http://www.nuget.org/packages/Microsoft.ApplicationInsights.WindowsPhone/
- Open the ApplicationInsights.config file. Add the InstrumentionKey from the portal by adding following code snippet to the root <ApplicationInsights> node in the config file.
<InstrumentationKey>INSERT YOUR KEY HERE</InstrumentationKey>
To demonstrate the addition of Application Insights we will walk through the steps for a basic sample app. We will use as our sample app the basic Hub App template provide by Visual Studio 2013.
We will start off by creating our Application Insights resource in the portal. In this case we select a Windows Store Application. The UI today is identical between Windows Store and Windows Phone.
Here we add the Microsoft.ApplicationInsights.WindowsStore NuGet package to the Windows 8.1 project. Likewise we will add the Microsoft.ApplicationInsights.WindowsPhone package to the Windows Phone 8.1 project.
We will then make sure to add in the InstrumentationKey to our ApplicationInsights.config file. This key is obtained in the portal by selecting your application, and clicking on properties. Remember that we need to add the key to both of the ApplicationInsights.config files in both projects.
At this point we are done! The application insights SDKs have been integrated into our project. If we run our application (for example through a simple F5 in Visual Studio) we will see that telemetry is automatically collected and flows directly to the Application Insights portal. The SDK will collect basic telemetry automatically. This telemetry includes things like basic user and sessions tracking, collection of page views, and collection of contextual meta-data, such as device model and operating system.
Universal App Specific Topics
Because the Application Insights SDKs API surface area is identical for both WindowsPhone and WindowsStore, you can utilize the API from shared code without special treatment. Any API that is callable will properly link and function for both WinStore and WinPhone applications produced from your universal project. This includes the ability to track and custom events or metrics. For more information on collecting custom telemetry please see the official documentation.
This example recommends the usage of the same Instrumentation Key for both your Phone and Store applications. It is assumed that for a universal app the functionality (and by extension telemetry) between the apps will be nearly the same. In those cases it is advantageous to have all of your telemetry data in the same place, allowing for analysis and comparisons about differences in behavior between the two apps. If however you find that you view the telemetry from each application separately, or find yourself collecting a different set of telemetry from each platform, you may want to use a dedicated key for each app. This will isolate your data from each other but in some situation that may be more helpful for analysis. It should be noted that this scenario is fully supported as you can specify a different Instrumentation Key in each ApplicationInsights.config file to allow for isolation of data collection.
The sample project was just a creation of a default template hub app, with the addition of Application Insights. If you would like access to the final project code you can access it at our GitHub Home Repo.
We are always looking for your feedback to improve the experience. If you like or dislike something, please let us know via any of our channels: Comments on this blog post, Uservoice (new feature requests), forum (discussion and support), and Connect (to report bugs).