Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
IeuanWalker authored and dhindrik committed Sep 10, 2024
1 parent b4caa73 commit a7015f3
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions TinyInsights/ApplicationInsightsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using System.Globalization;
using System.Net.NetworkInformation;
using System.Text.Json;

namespace TinyInsights;
Expand All @@ -30,6 +29,7 @@ public class ApplicationInsightsProvider : IInsightsProvider, ILogger
public bool IsTrackDependencyEnabled { get; set; } = true;

#if IOS || MACCATALYST || ANDROID

public ApplicationInsightsProvider(string connectionString)
{
_connectionString = connectionString;
Expand All @@ -54,6 +54,7 @@ void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs
}
}
}

#elif WINDOWS
public ApplicationInsightsProvider(MauiWinUIApplication app, string connectionString)
{
Expand All @@ -73,6 +74,7 @@ void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionE
#endif

public static bool IsInitialized { get; private set; }

public void Initialize()
{
CreateTelemetryClient();
Expand All @@ -86,7 +88,7 @@ public void Initialize()
{
if(Application.Current is null)
{
throw new NullReferenceException("Unable to configure `IsAutoTrackPageViewsEnabled` as `Application.Current` is null. You can eihter set `IsAutoTrackPageViewsEnabled` to false to ignore this issue, or check out this link for a possible reason - https://github.com/dhindrik/TinyInsights.Maui/issues/21");
throw new NullReferenceException("Unable to configure `IsAutoTrackPageViewsEnabled` as `Application.Current` is null. You can either set `IsAutoTrackPageViewsEnabled` to false to ignore this issue, or check out this link for a possible reason - https://github.com/dhindrik/TinyInsights.Maui/issues/21");
}
WeakEventHandler<Page> weakHandler = new(OnAppearing);
Application.Current.PageAppearing += weakHandler.Handler;
Expand All @@ -104,6 +106,7 @@ private static void OnAppearing(object? sender, Page e)
}

readonly Dictionary<string, string> _globalProperties = [];

private TelemetryClient? CreateTelemetryClient()
{
if(_client is not null)
Expand All @@ -127,7 +130,7 @@ private static void OnAppearing(object? sender, Page e)
// Role name will show device name if we don't set it to empty and we want it to be so anonymous as possible.
_client.Context.Cloud.RoleName = string.Empty;
_client.Context.Cloud.RoleInstance = string.Empty;
_client.Context.User.Id = Preferences.Get(userIdKey, GenerateNewAnonymousUserId());
_client.Context.User.Id = GetUserId();

// Add any global properties, the user has already added
foreach(KeyValuePair<string, string> property in _globalProperties)
Expand Down Expand Up @@ -183,10 +186,17 @@ public void OverrideAnonymousUserId(string userId)
Preferences.Set(userIdKey, userId);
if(Client is not null)
{
Client.Context.User.Id = Preferences.Get(userIdKey, GenerateNewAnonymousUserId());
Client.Context.User.Id = userId;
}
}

private string GetUserId()
{
var userId = Preferences.Get(userIdKey, null);

return userId ?? GenerateNewAnonymousUserId();
}

public string GenerateNewAnonymousUserId()
{
var userId = Guid.NewGuid().ToString();
Expand Down Expand Up @@ -217,7 +227,7 @@ private async Task SendCrashes()
{
var ex = crash.GetException();

if (ex is null)
if(ex is null)
{
continue;
}
Expand Down

0 comments on commit a7015f3

Please sign in to comment.