For AI agents: a Markdown version of this page is at https://docs.snowplow.io/docs/sources/node-js-tracker/previous-versions/node-js-tracker-0-3-0/initialization.md. The index of all documentation pages is at https://docs.snowplow.io/llms.txt, and the index for this section is at https://docs.snowplow.io/docs/api-reference/llms.txt.
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
namespacewhich 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).