Skip to content

+semver:minor Made it possible to disable and enable tracking on the fly#42

Draft
tombogle wants to merge 1 commit intomasterfrom
enable-change-to-allow-tracking-property
Draft

+semver:minor Made it possible to disable and enable tracking on the fly#42
tombogle wants to merge 1 commit intomasterfrom
enable-change-to-allow-tracking-property

Conversation

@tombogle
Copy link
Contributor

@tombogle tombogle commented Feb 27, 2026

This change is Reviewable

@tombogle tombogle requested a review from andrew-polk February 27, 2026 13:52
@tombogle tombogle self-assigned this Feb 27, 2026
@tombogle tombogle requested a review from nabalone March 9, 2026 12:24
/// Indicates whether we are tracking or not
/// </summary>
public static bool AllowTracking { get; private set; }
public static bool AllowTracking { get; set; }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Setting AllowTracking to true after constructing with allowTracking:false causes NullReferenceException

Making AllowTracking publicly settable enables a scenario where the Analytics object is constructed with allowTracking: false, and then AllowTracking is later set to true. This causes a crash because the client was never initialized.

Root Cause

When the constructor is called with allowTracking: false, it returns early at src/DesktopAnalytics/Analytics.cs:171-172:

if (!AllowTracking)
    return;

This skips _client.Initialize() at line 211, the assignment of AnalyticsSettings.Default.IdForAnalytics at lines 213-217, and the initialization of s_locationInfo at line 219.

Later, if a consumer sets Analytics.AllowTracking = true and calls Analytics.Track(...), the call chain reaches TrackWithApplicationProperties at line 934:

s_singleton._client.Track(AnalyticsSettings.Default.IdForAnalytics, eventName, properties);

Inside SegmentClient.Track (src/DesktopAnalytics/SegmentClient.cs:62), _analytics is null because Initialize was never called, causing a NullReferenceException. The same applies to MixpanelClient where _client at src/DesktopAnalytics/MixpanelClient.cs:12 would also be null.

Impact: Any application that constructs Analytics with tracking initially disabled and later enables it will crash with a NullReferenceException.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

@tombogle tombogle marked this pull request as draft March 16, 2026 20:33
@tombogle tombogle marked this pull request as draft March 16, 2026 20:33
@tombogle tombogle marked this pull request as draft March 16, 2026 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant