+semver:minor Made it possible to disable and enable tracking on the fly#42
+semver:minor Made it possible to disable and enable tracking on the fly#42
Conversation
| /// Indicates whether we are tracking or not | ||
| /// </summary> | ||
| public static bool AllowTracking { get; private set; } | ||
| public static bool AllowTracking { get; set; } |
There was a problem hiding this comment.
🔴 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.
Was this helpful? React with 👍 or 👎 to provide feedback.
This change is