Collecting data with Trackers and Webhooks

  1. Home
  2. Docs
  3. Collecting data with Trackers and Webhooks
  4. Trackers – collecting data from your own applications
  5. JavaScript Trackers (Web and Node.js)
  6. Node.js Tracker
  7. Previous versions
  8. Node.js Tracker (0.3.0)
  9. Initialization


Assuming you have completed the Node.js Tracker Setup for your project, you are now ready to initialize the Tracker.

Require the Node.js Tracker module into your code like so:

var snowplow = require('snowplow-tracker'); var emitter = snowplow.emitter; var tracker = snowplow.tracker;
Code language: JavaScript (javascript)

First, initialize an emitter instance. This object will be responsible for how and when events are sent to Snowplow.

var e = emitter( '', // Collector endpoint 'http', // Optionally specify a method - http is the default 8080, // Optionally specify a port 'POST', // Method - defaults to GET 5, // Only send events once n are buffered. Defaults to 1 for GET requests and 10 for POST requests. function (error, body, response) { // Callback called for each request if (error) { console.log("Request to Scala Stream Collector failed!"); } }, { maxSockets: 6 } // Node.js agentOptions object to tune performance );
Code language: JavaScript (javascript)

Note that last emitter’s argument agentOptions is default configuration object for Node.js HTTP module. It required to constrain maxSockets for bulk loads, which otherwise can lead to event loss.

Initialise a tracker instance like this:

var t = tracker([e], 'myTracker', 'myApp', false);
Code language: JavaScript (javascript)

The tracker function takes four parameters:

  • An array of emitters to which the tracker will hand Snowplow events
  • An optional tracker namespace which will be attached to all events which the tracker fires, allowing you to identify their origin
  • The appId, or application ID
  • encodeBase64, which determines whether unstructured events and custom contexts will be base 64 encoded (by default they are).

If you’d like to learn more about Snowplow BDP you can book a demo with our team, or if you’d prefer, you can try Snowplow technology for yourself quickly and easily.