Snowplow .NET Tracker 0.1.0 released


We are pleased to announce the release of the first version of the Snowplow .NET Tracker. The tracker supports synchronous and asynchronous GET and POST requests and has an offline mode which stores unsent events using Message Queueing.

This introductory post will cover the following topics:

  1. Installation
  2. How to use the tracker
  3. Features
  4. Logging
  5. Getting help

The Snowplow .NET Tracker is published to NuGet, the .NET package manager. To add it to your project, install it in the Visual Studio Package Manager Console:

Install-Package Snowplow.Tracker

You will also need to add an assembly reference to the .NET Tracker to your project.

Add a using directive to the Snowplow Tracker to your project:

using Snowplow.Tracker<span class="p">;

Create an emitter which will asynchronously send HTTP GET requests:

var e = new AsyncEmitter(""<span class="p">);

It is also possible to specify the protocol, method, and port that the emitter will use, as well as a bufferSize which determines the minimum number of events to queue before sending them all, and onSuccess and onFailure callbacks to be called depending on whether requests are sent successfully.

Create a subject to hold data about a specific user:

var s = new Subject<span class="p">(); s.SetUserId("user-567296"<span class="p">); s.SetTimezone("Europe/London"<span class="p">); s.SetLang("en"<span class="p">);

Create a tracker:

var t = new Tracker(e, s, "my-tracker-namespace", "my-application-id"<span class="p">);

Send some events:

// Track a page view t.TrackPageView("", "title page"<span class="p">); // Track a structured add-to-basket event t.TrackStructEvent("shop", "add-to-basket", null, "red hat", 2<span class="p">); // Track an ecommerce transaction // Use the TransactionItem class for the items within a transaction var hat = new TransactionItem("pbz0026", 20, 1<span class="p">); var shirt = new TransactionItem("pbz0038", 15, 1, "shirt", "clothing"<span class="p">); var items = new List<TransactionItem> { hat, shirt <span class="p">}; t.TrackEcommerceTransaction("6a8078be", 35, "affiliation", 3, 0, "Phoenix", "Arizona", "US", "USD", items<span class="p">); // Track a Snowplow custom unstructured event var eventJson = new Dictionary<string, object> { {"schema", "iglu:com.acme/test/jsonschema/1-0-0"}, {"data", new Dictionary<string, string> { { "page", "testpage" }, { "user", "tester" } } } <span class="p">}; t.TrackUnstructEvent(eventJson<span class="p">); // Track a screen view event with custom context attached var screenContext = new Dictionary<string, object> { {"schema", "iglu:com.snowplowanalytics.snowplow/screen_type/jsonschema/1-0-0"}, {"data", new Dictionary<string, object> { { "type", "test" }, { "public", false } } } <span class="p">}; var userContext = new Dictionary<string, object> { {"schema", "iglu:com.snowplowanalytics.snowplow/user/jsonschema/1-0-0"}, {"data", new Dictionary<string, object> { { "age", 40 }, { "name", "Ned" } } } <span class="p">}; var contextsList = new List<Dictionary<string, object>> { pageContext, userContext <span class="p">}; t.TrackScreenView("Test screen", "id-0004346", contextsList<span class="p">);

By default, instances of the Emitter class have offline tracking enabled. This means that if the device is offline when an event is sent, that event will be stored in a message queue. When a NetworkAvailabilityChanged event indicates that connectivity has been established, or when another event has been sent successfully, all events in the queue will be resent.

The Emitter and AsyncEmitter classes use NLog to log messages to the console. You can set the logging level using the static setLogLevel method:

Log.SetLogLevel(Log.Level.Debug<span class="p">);

By default the log level is set to Log.Level.Info. The possible levels are:

Level Description
Off Nothing logged
Warn Notification for requests with status code not equal to 200
Info Notification for all requests
Debug Contents of all requests

Some useful resources:

This is only the first version of the Snowplow .NET Tracker, so please raise an issue if you find any bugs. If you have an idea for a new feature or need help getting set up, get in touch!


Related articles