Skip to content
Merged
Show file tree
Hide file tree
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
19 changes: 16 additions & 3 deletions src/DesktopAnalytics/Analytics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private class InitializationParameters

private readonly IClient _client;
private volatile InitializationParameters _deferredInitializationParameters;
private bool _disposed;

/// <summary>
/// Initialized a singleton; after calling this, use Analytics.Track() for each event.
Expand Down Expand Up @@ -593,18 +594,29 @@ internal static UserInfo ApplyPiiPolicy(UserInfo userInfo, bool retainPii,
/// <param name="userInfo">The user information to update. A null userInfo is a no-op.
/// </param>
/// <remarks>
/// If called after the Analytics object was constructed, this honors the same retainPii
/// setting used in the constructor. If called before the Analytics object was constructed,
/// retainPii defaults to false.
/// Must be called after the Analytics object has been constructed (that's where the
/// retainPii policy this honors comes from). A no-op if the Analytics object has already
/// been disposed - by that point in the app's lifecycle, updating the user can't
/// meaningfully work, and failing silently is safer than risking a crash during shutdown.
/// </remarks>
/// <exception cref="ApplicationException">Thrown if called before the Analytics object
/// has been constructed.</exception>
[PublicAPI]
public static void IdentifyUpdate(UserInfo userInfo)
{
if (userInfo == null)
return;

if (s_singleton == null)
throw new ApplicationException("The application must first construct a single Analytics object");

if (s_singleton._disposed)
return;

s_userInfo = ApplyPiiPolicy(userInfo, s_retainPii, s_userInfo);
s_singleton.UpdateServerInformationOnThisUser();
if (AllowTracking)
FlushClient();
}

/// <summary>
Expand Down Expand Up @@ -795,6 +807,7 @@ private static void Client_Failed(Exception e)
public void Dispose()
{
_client?.ShutDown();
_disposed = true;
}

/// <summary>
Expand Down
8 changes: 8 additions & 0 deletions src/DesktopAnalyticsTests/AnalyticsTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Globalization;
using System.Xml.Linq;
using DesktopAnalytics;
Expand Down Expand Up @@ -192,6 +193,13 @@ public void IdentifyUpdate_NullUserInfo_IsNoOpAndDoesNotThrow()
Assert.AreSame(before, Analytics.CurrentUserInfo);
}

[Test]
public void IdentifyUpdate_SingletonNotYetConstructed_NonNullUserInfo_Throws()
{
// No Analytics object is ever constructed in this test run, so s_singleton is null.
Assert.Throws<ApplicationException>(() => Analytics.IdentifyUpdate(new UserInfo()));
}

#endregion
}
}
Loading