Skip to content

TinyInsights is a library for tracking and a website for showing insights for your .NET MAUI app. Data will be saved in Application Insights. The website will help you to show the data in a more mobile app-friendly user interface.

License

Notifications You must be signed in to change notification settings

dhindrik/TinyInsights.Maui

Repository files navigation

TinyInsights

TinyInsights is a library for tracking and a website for showing insights for your .NET MAUI app. Data will be saved in Application Insights. The website will help you to show the data in a more mobile app-friendly user interface.

You can try out the website here, https://mauiinsights.z6.web.core.windows.net/. No data is saved in the application.

Add TinyInsights to your app

Install the latest version of the package TinyInsights.Maui.AppInsights to your app project.

dotnet add package TinyInsights.Maui.AppInsights

Installation

In MauiProgram.cs, you should call UseTinyInsights like below.

var builder = MauiApp.CreateBuilder();
builder
  .UseMauiApp<App>()
  .ConfigureFonts(fonts =>
    {
      fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
      fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
     })
  .UseTinyInsights("{YOUR_CONNECTION_STRING}")

If you want, you can configure what type of events you want to track.

builder.UseMauiInsights("InstrumentationKey=8b51208f-7926-4b7b-9867-16989206b950;IngestionEndpoint=https://swedencentral-0.in.applicationinsights.azure.com/;ApplicationId=0c04d3a0-9ee2-41a5-996e-526552dc730f",
        (provider) =>
        {
            provider.IsTrackDependencyEnabled = true;
            provider.IsTrackEventsEnabled = true;
            provider.IsTrackErrorsEnabled = true;
            provider.IsTrackPageViewsEnabled = true;
            provider.IsTrackCrashesEnabled = true;           
        });

Track events

Crashes will be tracked automatically if it is enabled, other events you need to track manually.

To get access to the insights provider that you should use to track events, you should inject the IInsights interface.

private readonly IInsights insights;

public MainViewModel(IInsights insights)
{
  this.insights = insights;
}

Track page views

await insights.TrackPageViewAsync("MainView");

Track custom events

await insights.TrackEventAsync("AddButtonTapped");

Track exceptions

catch (Exception ex)
{
    await insights.TrackErrorAsync(ex);
}

Track additional data For all the track methods, you can pass additional data by passing a Dictionary.

var data = new Dictionary<string, string>()
          {           
              {"key", "value"},
              {"key2", "value2"}
          };

await insights.TrackPageViewAsync("MainView", data);

Track dependencies To automatically track HTTP calls you can use the InsightsMessageHandler together with the HttpClient.

private readonly InsightsMessageHandler insightsMessageHandler;
private readonly HttpClient client;

public MainViewModel(InsightsMessageHandler insightsMessageHandler)
{
  this.insightsMessageHandler = insightsMessageHandler;
  client = new HttpClient(insightsMessageHandler);
}

You can also create a DependencyTracker and use it to track dependencies.

using var dependency = insights.CreateDependencyTracker("BLOB",blobContainer.Uri.Host, url);
await blob.DownloadToAsync(stream);

or if you don't want to wait for the scope of the using.

var dependency = insights.CreateDependencyTracker("BLOB",blobContainer.Uri.Host, url);
await blob.DownloadToAsync(stream);
dependency.Dispose();

About

TinyInsights is a library for tracking and a website for showing insights for your .NET MAUI app. Data will be saved in Application Insights. The website will help you to show the data in a more mobile app-friendly user interface.

Topics

Resources

License

Stars

Watchers

Forks