Using Application Insights for your Android App – with Visual Studio 2015

By | November 2, 2015

Application Insights helps us to track applications health in real time and let us monitor our application for usage, events and crashes.  Application Insights support multiple platforms including iOS, Android, Java , Rupy etc.  In this post, lets have a look how to start capturing telemetry information for your Android App.

To build the Android App, we will be using Xamarin.Forms, Visual Studio 2015 and Application Insights SDK for Xamarin.

Recommended Read  : Adding Application Insights to a Windows 10 Universal App

Let’s start by

Creating an Application Insights resource for Android

  • Open the the Azure portal
  • Navigate to New –> Developer Services –> Application Insights
  • Create a new Application Insights (provide name as Androidapp)
  • Pick the Android Application option from set of “Application type

image

Finally click on the “Create” button.  That’s all.  Within while your Application Insights for Android would be created.

Once it is created, you should be able to see it on the Azure Portal Dashboard

image

Create the Android App Using Xamarin.Forms and Visual Studio 2015

  • Start a new instance of Visual Studio 2015
  • From the new project dialog , select Mobile Apps –> Blank App ( Xamarin.Forms Shared)
  • Provide the App name, and Click on the Ok.

image

P.S : You can choose other templates from the list for building your android app or only xamarin.android app templates as well.

Once the solution is created, you should be able to see the following projects in the solutions explorer.  For now, our focused will be only on the MyAndroidapp.Droid project.

image

Including the Applications Insights SDK for Xamarin

Microsoft/ApplicationInsights-Xamarin SDK allows Xamarin apps to send telemetry to the Applications Insights. You need to add this package as local NuGet package source once your download it from Git.

Must Read : How to Install a local sources NuGet package or a Prerelease package in Visual Studio 2015 ?

Then install the package into your Android Project ( Please read the above mentioned articles for more details on how to install a local nuget package)

image

Once the installation done we are almost done with the installation of SDK.

image

 

Initializing  Application Insights for the App

Navigate to MainActivity.cs file and override the OnStart method as  follows

protected override void OnStart()
{
base.OnStart();
ApplicationInsights.Setup("");
ApplicationInsights.Start();
}

If you look in to the Setup Method, it is expecting an argument “instrumentationkey”

image

Let’s get that !!

Getting the Instrumentation Key for Android Application Insights

Open the Azure Portal and navigate tot eh “Androidapp” application insights created at very fast steps.

Get the  Instrumentation Key from there and update the OnStart() method with the same.

image

So, here is how the code block would look at the end.

protected override void OnStart()
{
base.OnStart();
ApplicationInsights.Setup("d446a4dc-0c6c-49af-9e6b-3129d307b71c");
ApplicationInsights.Start();
}

That’s all about the integration !

Run the App !

Take a look into Azure Portal

Open the Azure Portal, and navigate to the respective Application Insights for your Android Application, you should be able to start seeing the telemetry data.

Appinsightsforandroid

Adding Custom Events

Like other app platforms, you can easily add custom telemetry for your android app as well. For an example if you want to add a custom event when the app starts, you can just write following set of code in side the app.cs file

  protected override void OnStart()
        {
            base.OnStart();
            ApplicationInsights.Setup("d446a4dc-0c6c-49af-9e6b-3129d307b71c");
            ApplicationInsights.Start();
            TelemetryManager.TrackEvent("Application Start");
        }

Now if you run the application once again, you should be able to view the custom logged event.
CustomEvent
That’s it for now ! Hope now you would be able to explore it more and take it forward.
Thanks for reading !!