Request a Demo Start Trial
Windows Analytics
Download Windows Analytics SDK
version 6.2.0

C# Apps

Introduction

This document will walk you through integrating the DeskMetrics Analytics SDK into a .NET application. We’ll be working through the sample C# WPF application in examples/HelloDeskMetrics.NET, so open up the Visual Studio project and follow along.

The SDK has the following requirements and dependencies:

We’ll make use of the following files from the SDK zip:

Add the SDK to your project

Note: if your application is built specifically for 32 bit, dma_x64.dll can be omitted.

Include DeskMetrics

We’ll now turn to examples/HelloDeskMetrics.NET/App.xaml.cs and follow the integration through.

At the top of the source file, we’ve included the Com.DeskMetrics namespace:

using Com.DeskMetrics;

We’ve defined a static accessor for the DeskMetricsClient instance:

private static readonly DeskMetricsClient dma = DeskMetricsClient.Instance;

Lastly, we’ve defined our app key, as well as the registry location where our installer has stashed some recommended event properties:

// Your app key - this can be found on the applications page of your DeskMetrics dashboard.
private static readonly string APP_KEY = "<YOUR_APP_KEY_HERE>";

// Location of properties we stashed in the registry at install time
private static readonly string REGISTRY_KEY = @"HKEY_CURRENT_USER\SOFTWARE\HelloDeskMetrics\_dm";

Initialize

Just inside the entry point of the application (App() in this case), load the DeskMetrics properties that were previously saved by your installer, and start the SDK.

If you haven’t integrated DeskMetrics into your installer, you might want to use SetProperty and SaveProperties to establish a set of properties to be sent along with all of your events.

// Load the properties saved by the installer.
dma.LoadProperties(REGISTRY_KEY, false);

// Start sending events.
// This will trigger the automatic 'launch' event, and a 'heart_beat' event every 12 hours.
dma.Start(DMASessionType.Application, APP_KEY);

If you’ve set up a custom domain for use with DeskMetrics you can use the SetAnalyticsBaseUrl method before your call to Start.

// Possibly we're using our own domain so lets set it here:
dma.SetAnalyticsBaseUrl("https://custom.mybusiness.com");

// Start the SDK like normal:
dma.Start(DMASessionType.Application, APP_KEY);

Remember that this method should only be called before Start; if you call SetAnalyticsBaseUrl afterwards then your events will not be sent using your custom domain.

Sending events

The integration we’ve seen so far will automatically fire launch, exit, and heart_beat events. However, you’ll probably want some custom events as well. In this example, we fire a custom event each time the user clicks a button.

Let’s take a look at the Button_Click event handler in MainWindow.xaml.cs to see how this is achieved:

// Send a custom analytics event.
dma.Send("hello", new { message = "Hello from .NET & WPF." });

Thread safety

The DeskMetricsClient instance is threadsafe.

However, it should be noted that: