Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: User ID #25

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -12,7 +11,7 @@
public class ApplicationInsightsProvider : IInsightsProvider, ILogger
{
private readonly string _connectionString;
private static ApplicationInsightsProvider provider;

Check warning on line 14 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'provider' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 14 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'provider' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 14 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'provider' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 14 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'provider' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 14 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'provider' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 14 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'provider' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 14 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'provider' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 14 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'provider' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 14 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'provider' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
private const string userIdKey = nameof(userIdKey);

private const string crashLogFilename = "crashes.mauiinsights";
Expand All @@ -30,6 +29,7 @@
public bool IsTrackDependencyEnabled { get; set; } = true;

#if IOS || MACCATALYST || ANDROID

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

#elif WINDOWS
public ApplicationInsightsProvider(MauiWinUIApplication app, string connectionString)
{
Expand All @@ -73,6 +74,7 @@
#endif

public static bool IsInitialized { get; private set; }

public void Initialize()
{
CreateTelemetryClient();
Expand All @@ -86,7 +88,7 @@
{
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 @@
}

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

private TelemetryClient? CreateTelemetryClient()
{
if(_client is not null)
Expand All @@ -127,7 +130,7 @@
// 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 @@
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 @@
{
var ex = crash.GetException();

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