Snowplow .NET Tracker 0.1.0 released

29 September 2014  •  Fred Blundun

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;

Create an emitter which will asynchronously send HTTP GET requests:

var e = new AsyncEmitter("d3rkrsqld9gmqf.cloudfront.net");

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();
s.SetUserId("user-567296");
s.SetTimezone("Europe/London");
s.SetLang("en");

Create a tracker:

var t = new Tracker(e, s, "my-tracker-namespace", "my-application-id");

Send some events:

// Track a page view
t.TrackPageView("http://www.example.com", "title page");

// Track a structured add-to-basket event
t.TrackStructEvent("shop", "add-to-basket", null, "red hat", 2);

// Track an ecommerce transaction
// Use the TransactionItem class for the items within a transaction
var hat = new TransactionItem("pbz0026", 20, 1);
var shirt = new TransactionItem("pbz0038", 15, 1, "shirt", "clothing");
var items = new List<TransactionItem> { hat, shirt };
t.TrackEcommerceTransaction("6a8078be", 35, "affiliation", 3, 0, "Phoenix", "Arizona", "US", "USD", items);

// 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" }
	}
	}
};
t.TrackUnstructEvent(eventJson);

// 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 }
	}
	}
};

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" }
	}
	}
};

var contextsList = new List<Dictionary<string, object>>
{
	pageContext,
	userContext
};

t.TrackScreenView("Test screen", "id-0004346", contextsList);

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

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!