How to monitor your C# application via Google Analytics or Matomo/Piwik

This article explains how to add application analytics (Software Product Analytics) to your C# (c-sharp) desktop software.

Application Analytics will complement your Website Analytics and give you the capability to see what happens after the download. How many users continue to use your application and how.

Product Analytics for software applications for Windows and MacOS

SoftMeter, our application usage analytics library, is a kind of minimal SDK for Google Analytics tracking (and Matomo/Piwik, in Beta).

By embedding SoftMeter in your C# desktop software, you can send hits to Google Analytics or Matomo/Piwik. Then you can use a normal Google analytics or Matomo/Piwik account to remotely monitor and track the usage of your software, together with any exceptions or crashes. Your can see how your software is being used both in real-time and in historical reports, compare month by month, or year by year.

Here is an example of one of the applications that use the free edition of SoftMeter.
C# Desktop software application usage tracking via Google analytics, Matomo Piwik

Benefits of SoftMeter as your application usage statistics tool

  • Unified analytics between your website visitors and your software users. Either via Google Analytics or via Matomo you can see what happens beyond the download.
  • Small library (maybe the smallest GA and Matomo client)
  • No dependencies on other software packages.
  • Implementation is designed to be very simple and can be completed today, that you read this article.

What you need

  • 1x C# desktop application
  • 1x SoftMeter DLL (32 or 64bit)
  • 1x Google analytics (or Matomo/Piwik, email us for details) account.
  • A few hours of development work

Steps

1. Add in your application files the class "SoftMeterDLL" which loads the SoftMeter DLL and links its functions.



using System;
using System.Runtime.InteropServices;

// a class to encapsulate the SoftMeter DLL functions
public static class SoftMeterDLL
{

    public const string DLL_FILE_NAME = 
        "libSoftMeter64.dll"; // for 64bit  software
        // "libSoftMeter.dll"; // for 32bit software


    [DllImport(DLL_FILE_NAME)]
    internal static extern IntPtr getVersion(); // If a C++ DLL function returns char*, C# code will treat it as IntPtr and Marshal.PtrToStringAnsi() will convert it into C# string.

    [DllImport(DLL_FILE_NAME)]
    internal static extern IntPtr getLogFilename();

    [DllImport(DLL_FILE_NAME)]
    internal static extern void enableLogfile([MarshalAs(UnmanagedType.LPStr)]string appName, [MarshalAs(UnmanagedType.LPStr)]string macBundleId);

    [DllImport(DLL_FILE_NAME)]
    internal static extern void disableLogfile();

    [DllImport(DLL_FILE_NAME)]
    internal static extern bool start([MarshalAs(UnmanagedType.LPStr)]string appName, [MarshalAs(UnmanagedType.LPStr)]string appVersion,
                                        [MarshalAs(UnmanagedType.LPStr)]string appLicense, [MarshalAs(UnmanagedType.LPStr)]string appEdition,
                                        [MarshalAs(UnmanagedType.LPStr)]string propertyID, bool userGaveConsent);

    [DllImport(DLL_FILE_NAME)]
    internal static extern void stop();

    [DllImport(DLL_FILE_NAME)]
    internal static extern bool sendPageview([MarshalAs(UnmanagedType.LPStr)]string pagePath, [MarshalAs(UnmanagedType.LPStr)]string pageTitle);

    [DllImport(DLL_FILE_NAME)]
    internal static extern bool sendScreenview([MarshalAs(UnmanagedType.LPStr)]string screenName);

    [DllImport(DLL_FILE_NAME)]
    internal static extern bool sendEvent([MarshalAs(UnmanagedType.LPStr)]string eventAction, [MarshalAs(UnmanagedType.LPStr)]string eventLabel, int eventValue);

    [DllImport(DLL_FILE_NAME)]
    internal static extern bool sendException([MarshalAs(UnmanagedType.LPStr)]string exceptionDescription, bool isFatal);
 
}

2. In your MainWindow add the start() and stop() code for SoftMeter and also send the first hit to Google Analytics



    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();

            // SoftMeter initialization
            bool UserGaveConsent = true; // load this from your user settings file
            lblLibVer.Content = Marshal.PtrToStringAnsi(SoftMeterDLL.getVersion()); // show the dll version 
            string appName = "SoftMeter C-sharp DemoApp";
            string appVersion = "0.9";
            string appLicense = "pro";
            string appEdition = "ms store"; // the edition uploaded to the Microsoft store

            string trackingID = "UA-1234-1"; // replace this with your own Google PropertyID
            SoftMeterDLL.enableLogfile(appName, "");
            SoftMeterDLL.start(appName, appVersion, appLicense, appEdition, trackingID, UserGaveConsent);

            SoftMeterDLL.sendPageview("main","MainWindow");
            SoftMeterDLL.sendScreenview("MainWindow");

        }

        ~MainWindow()
        {
            SoftMeterDLL.stop();
        }


    }

3. Download the libSoftMeter.dll (or libSoftMeter64.dll) and place it next to the C# executable of your application

4. Compile and run your C# application

5. Log-in to Google Analytics and go to the real-time reports

Google Analytics real time reports for software usage

A pageview hit must appear with the name of your application window.

C# real time usage monitoring via Google Analytics
Real-time usage monitoring of a C# desktop software application

6. Done. You have telemetry in your software.

The basic implementation of telemetry in your software is done. You can grab a coffee or a beer and take your victory lap in the office, or in your home-office if you are working from home.

Well done!

7. Telemetry consent

Create a dialog where you ask the user's consent for the telemetry to be enabled. Save the user's choice to a settings variable and pass the value to SoftMeter's start() function. No other conditional IFs are needed.

8. Track the most popular application features

Add more calls to sendPageview() or sendScreenview, in each of the software features that you want to measure. The reports will show you the most popual features of your application together with the least used ones.

9. Prepare the distribution/installation package

Remember to distribute libSoftMeter.dll (or libSoftMeter64.dll) with your software installer.

Cross-platform

SoftMeter is a cross-platform application analytics library (Windows, MacOS, IOS). While these instructions refer to a Windows demo application, if you try them with a MacOS application developed in C#, send us your code samples. (On MacOS you will use libSoftMeter.dylib, instead of the DLL).

Feedback

If you enjoyed or met difficulties with this article or you simply want to share with us your impressions, do send us your comments.

Add new comment