Tealium for Node.js

Tealium for Node.js

by Community Manager on ‎02-13-2018 12:03 PM - edited on ‎04-04-2018 04:36 PM by (283 Views)

This guide shows how to add Tealium to your Node.js application to track activity using the Universal Data Hub.

Table of Contents Placeholder

Overview

The tealium-collect module for Node.js sends event data to the Universal Data Hub (UDH) via the Tealium HTTP API. The module can be used in server-side applications or in a browser.

Requirements

Installation 

The tealium-collect module is installed using npm. The install will automatically include the dependency module tealium.

Using npm:

npm install tealium-collect

Initialization

Once the module is installed you are ready to start coding. Use the following code to require the Tealium modules and initialize with the following parameters:

  • account - the name of your account
  • profile - the name of the profile within your account (usually "main")
  • datasource - (optional) the data source key from UDH
var Tealium = require('tealium');
var tealiumCollect = require('tealium-collect');

var config = {
"account" : "account",
"profile" : "profile",
"datasource" : "datasource"
};

var tealium = Tealium(config);

tealium.addModule(tealiumCollect);

Tracking

Once the Tealium object has been defined you can begin tracking events with the method track(), which takes the following parameters:

  • event (required) - a name to identify the event, sent as the attribute tealium_event
  • data - object with event data as key/value pairs

Example tracking call:  

tealium.track("my_event", {"mydata": "hello world"}); 

Tracking User Activity

If your application tracks user activity and you want to associate events with a visitor, set the tealium_visitor_id attribute in the data object. The value should uniquely identify the current user, either anonymously or with a personal identifier such as an email address.

// A visitor ID could be read from Cookie Header or other function
var myVisitorId = myVisitorIdFunction();
tealium.track("page_view", {
    "tealium_visitor_id" : myVisitorId,
    "dom.pathname"       : "/mypath",
    "dom.domain"         : "my.example.com",
    "dom.title"          : "My home page"
});