Search documentation...

K
ChangelogBook a demoSign up

Node.js SDK

The Node.js SDK makes it easy to track events from Node.js applications.

Installation

The Node.js SDK is available via NPM.

To install the Node.js SDK, run:

npm install @ht-sdks/events-sdk-node

Then, initialize the SDK wherever you want to track events:

const Analytics = require('@ht-sdks/events-sdk-node');

const client = new Analytics("HT_WRITE_KEY", {
  dataPlaneUrl: "https://us-east-1.hightouch-events.com",
});

The Analytics constructor accepts the following parameters:

ParameterTypeDescription
flushAtIntegerThe maximum number of events to store locally before forwarding to Hightouch. Defaults to 20.
flushIntervalIntegerThe maximum amount of time (in milliseconds) to store events locally before forwarding to Hightouch. Defaults to 10000.
maxInternalQueueSizeIntegerThe maximum number of events to store locally. This may be configured separately from flushAt because the event requests to Hightouch may fail, in which case they remain on the queue so that they can be retried.
logLevelStringThe log level for internal logs. Can be set to log, info, debug, warn, error, or none. Defaults to info.
dataPlaneUrlStringThe URL to send events to. Defaults to https://us-east-1.hightouch-events.com

API

Identify

The identify method sends an identify event.

Unlike the Browser SDK, it does not persist the user ID and traits locally, so user IDs must be explicitly added to other events. This is because server side events are usually servicing many different users at once.

Example usage:

client.identify({
  userId: "123",
  traits: {
    location: "San Francisco",
  }
})

The first argument to the method is an object representing the identify event to be sent. The second argument is an optional callback that's invoked after the event is sent.

client.identify(message, [callback])

The message object may include the following fields:

ParameterTypeDescription
userIdStringThe user's persistent ID
anonymousIdStringThe user's anonymous ID. This is automatically set if both userId and anonymousId are omitted.
traitsObjectAdditional traits about the user, such as email and name.
contextObjectOverrides to values in the event context. By default, context contains information autocollected by the SDK.
timestampDateThe date of the message. When backfilling data, this can be set to the past. By default, this is autoset to the current date.

Track

The track method sends a track event.

Example usage:

client.track({
  userId: "123",
  event: "Order completed",
  properties: {
    total: 29.99,
  }
})

The first argument to the method is an object representing the track event to be sent. The second argument is an optional callback that's invoked after the event is sent.

client.track(message, [callback])

The message object may include the following fields:

ParameterTypeDescription
userIdStringThe user's persistent ID
anonymousIdStringThe user's anonymous ID. This is automatically set if both userId and anonymousId are omitted.
eventStringThe name of the event.
propertiesObjectAdditional properties about the event, such as product_id.
contextObjectOverrides to values in the event context. By default, context contains information autocollected by the SDK.
timestampDateThe date of the message. When backfilling data, this can be set to a date in the past. By default, this is autoset to the current date.

Page

The page method sends a page event.

Example usage:

client.page({
  userId: "123",
  category: "Docs",
  name: "Getting started",
})

The first argument to the method is an object representing the page event to be sent. The second argument is an optional callback that's invoked after the event is sent.

client.page(message, [callback])

The message object may include the following fields:

ParameterTypeDescription
userIdStringThe user's persistent ID
anonymousIdStringThe user's anonymous ID. This is automatically set if both userId and anonymousId are omitted.
categoryStringThe page's category. For example "Docs"
nameStringThe page's name. For example "Getting started"
propertiesObjectAdditional properties about the event, such as url.
contextObjectOverrides to values in the event context. By default, context contains information autocollected by the SDK.
timestampDateThe date of the message. When backfilling data, this can be set to a date in the past. By default, this is autoset to the current date.

Screen

The screen method sends a screen event.

Example usage:

client.screen({
  userId: "123",
  category: "Docs",
  name: "Getting started",
})

The first argument to the method is an object representing the screen event to be sent. The second argument is an optional callback that's invoked after the event is sent.

client.screen(message, [callback])

The message object may include the following fields:

ParameterTypeDescription
userIdStringThe user's persistent ID
anonymousIdStringThe user's anonymous ID. This is automatically set if both userId and anonymousId are omitted.
categoryStringThe screen's category. For example "Docs"
nameStringThe screen's name. For example "Getting started"
propertiesObjectAdditional properties about the screen.
contextObjectOverrides to values in the event context. By default, context contains information autocollected by the SDK.
timestampDateThe date of the message. When backfilling data, this can be set to a date in the past. By default, this is autoset to the current date.

Group

The group method sends a group event.

Example usage:

client.group({
  userId: "123",
  groupId: "456",
  traits: {
    company_location: "San Francisco",
  },
})

The first argument to the method is an object representing the group event to be sent. The second argument is an optional callback that's invoked after the event is sent.

client.group(message, [callback])

The message object may include the following fields:

ParameterTypeDescription
userIdStringThe user's persistent ID
anonymousIdStringThe user's anonymous ID. This is automatically set if both userId and anonymousId are omitted.
groupIdStringThe id for the group.
traitsObjectAdditional traits about the group, such as company_name.
contextObjectOverrides to values in the event context. By default, context contains information autocollected by the SDK.
timestampDateThe date of the message. When backfilling data, this can be set to a date in the past. By default, this is autoset to the current date.

Ready to get started?

Jump right in or a book a demo. Your first destination is always free.

Book a demoSign upBook a demo

Need help?

Our team is relentlessly focused on your success. Don't hesitate to reach out!

Feature requests?

We'd love to hear your suggestions for integrations and other features.

Last updated: Sep 21, 2023

On this page

InstallationAPIIdentifyTrackPageScreenGroup

Was this page helpful?