Table of Contents

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

string

IsAdminDebugView

Gets or sets a value indicating whether the events show in Google Analytics' DebugView.

public bool IsAdminDebugView { get; set; }

Property Value

bool

IsEnabled

Gets or sets a value indicating whether the tracker sends events; off, it drops them.

public bool IsEnabled { get; set; }

Property Value

bool

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

ILogger

LoggerEventId

Gets or sets the event ID the log lines carry. The default is Ga4Tracker.

public EventId LoggerEventId { get; set; }

Property Value

EventId

MeasurementId

Gets the Google Analytics 4 measurement ID, such as G-XXXXXXXXXX.

public required string MeasurementId { get; init; }

Property Value

string

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

TimeSpan

SessionId

Gets or sets the session's ID.

public required string SessionId { get; set; }

Property Value

string

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

bool

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

string

UserId

Gets the ID of the signed-in user, or null for none.

public string? UserId { get; init; }

Property Value

string

UserProperties

Gets or sets the user properties sent with the events. The default is none.

public Dictionary<string, object> UserProperties { get; set; }

Property Value

Dictionary<string, object>

Methods

PrepareHttpHeaders(HttpHeaders)

Adds the tracker's headers to a request: its User-Agent.

protected void PrepareHttpHeaders(HttpHeaders httpHeaders)

Parameters

httpHeaders HttpHeaders

The 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

requestMessage HttpRequestMessage

The request.

name string

The request's name, for the log.

jsonData object

The body, sent as JSON, or null for none.

cancellationToken CancellationToken

The 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

trackEvent TrackEvent

The event.

cancellationToken CancellationToken

The 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

trackEvents IEnumerable<TrackEvent>

The events.

cancellationToken CancellationToken

The 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

action string

What was being done.

ex Exception

The exception.

cancellationToken CancellationToken

The 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)

Parameters

singleLine bool

true to write each entry on one line.