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. Node.js Tracker
  6. Node.js Tracker (0.3.0)
  7. Initialization

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;

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

var e = emitter( 'myscalastreamcollector.net', // 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 );

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

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