Class TrackerBase
- Namespace
- Ga4.Trackers
- Assembly
- VpnHood.Net.Toolkit.dll
Provides the base of the Google Analytics 4 trackers: their IDs, their switch and logging, and the HTTP send that they share.
public abstract class TrackerBase : ITracker
- Inheritance
-
TrackerBase
- Implements
- Derived
- Inherited Members
Properties
ClientId
Gets the ID of the client, the device or install.
public required string ClientId { get; init; }
Property Value
IsAdminDebugView
Gets or sets a value indicating whether the events show in Google Analytics' DebugView.
public bool IsAdminDebugView { get; set; }
Property Value
IsEnabled
Gets or sets a value indicating whether the tracker sends events; off, it drops them.
public bool IsEnabled { get; set; }
Property Value
Remarks
The default is true.
Logger
Gets or sets the logger of the requests and their failures, or null for none.
public ILogger? Logger { get; set; }
Property Value
LoggerEventId
Gets or sets the event ID the log lines carry. The default is Ga4Tracker.
public EventId LoggerEventId { get; set; }
Property Value
MeasurementId
Gets the Google Analytics 4 measurement ID, such as G-XXXXXXXXXX.
public required string MeasurementId { get; init; }
Property Value
RequestTimeout
Gets or sets how long a request may take: its token is canceled after it. The default is 10 seconds.
public TimeSpan RequestTimeout { get; set; }
Property Value
SessionId
Gets or sets the session's ID.
public required string SessionId { get; set; }
Property Value
ThrowExceptionOnError
Gets or sets a value indicating whether a failed request throws, after it is logged; otherwise, it is only logged.
public bool ThrowExceptionOnError { get; set; }
Property Value
UserAgent
Gets or sets the User-Agent header the requests send. The default is the OS version, without spaces.
public string UserAgent { get; set; }
Property Value
UserId
Gets the ID of the signed-in user, or null for none.
public string? UserId { get; init; }
Property Value
UserProperties
Gets or sets the user properties sent with the events. The default is none.
public Dictionary<string, object> UserProperties { get; set; }
Property Value
Methods
PrepareHttpHeaders(HttpHeaders)
Adds the tracker's headers to a request: its User-Agent.
protected void PrepareHttpHeaders(HttpHeaders httpHeaders)
Parameters
httpHeadersHttpHeadersThe request's headers.
SendHttpRequest(HttpRequestMessage, string, object?, CancellationToken)
Sends a request, with a body of JSON when there is one, and logs it with its answer; the request's token is canceled after RequestTimeout. Off, it sends nothing.
protected Task SendHttpRequest(HttpRequestMessage requestMessage, string name, object? jsonData, CancellationToken cancellationToken)
Parameters
requestMessageHttpRequestMessageThe request.
namestringThe request's name, for the log.
jsonDataobjectThe body, sent as JSON, or null for none.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Remarks
A failure is logged, and thrown only with ThrowExceptionOnError. An answer whose status is no
success is a failure too. The URL is logged with the value of an api_secret parameter hidden.
Exceptions
- Exception
The request failed, with ThrowExceptionOnError.
Track(TrackEvent, CancellationToken)
Sends an event.
public Task Track(TrackEvent trackEvent, CancellationToken cancellationToken)
Parameters
trackEventTrackEventThe event.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Exceptions
- Exception
The request failed, with ThrowExceptionOnError.
Track(IEnumerable<TrackEvent>, CancellationToken)
Sends events.
public abstract Task Track(IEnumerable<TrackEvent> trackEvents, CancellationToken cancellationToken)
Parameters
trackEventsIEnumerable<TrackEvent>The events.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
TrackError(string, Exception, CancellationToken)
Sends an exception as an exception event, the action in its page location (ex/action) and the message
in its page title.
public Task TrackError(string action, Exception ex, CancellationToken cancellationToken)
Parameters
actionstringWhat was being done.
exExceptionThe exception.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task
A task that represents the asynchronous operation.
Exceptions
- Exception
The request failed, with ThrowExceptionOnError.
UseSimpleLogger(bool)
Sets Logger to a simple console logger, for a tool or a test. A second call replaces the first one's logger and closes it.
public void UseSimpleLogger(bool singleLine = false)