1.0.3 • Published 7 months ago

@ht-sdks/events-sdk-node v1.0.3

Weekly downloads
-
License
-
Repository
github
Last release
7 months ago

Events Node.js SDK

This SDK lets you track user event data from your Node applications.

Supported Versions of Node

See https://nodejs.dev/en/about/releases/ for currently supported and recommended versions of Node. Unsupported versions may work for this SDK, but Node versions older than 12 will require ES2019 polyfills.

Installing the SDK

Run the following command to install the Node.js SDK via npm:

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

Using the SDK

Create a global client:

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

const client = new Analytics(WRITE_KEY, {
  dataPlaneUrl: DATA_PLANE_URL,
});

SDK Initialization Options

Below parameters are optional and can be passed during SDK initialization:

NameTypeDefault valueDescription
dataPlaneUrlStringThe data plane URL.
pathString/v1/batchPath to batch endpoint.
flushAtNumber20The number of events to be flushed when reached this limit.
flushIntervalNumber10000The maximum timespan (in milliseconds) after which the events from the in-memory queue is flushed
maxQueueSizeNumber460800(500kb)Maximum payload size of a batch request
maxInternalQueueSizeNumber20000The maximum length of the in-memory queue
logLevelString'info'Log level. Ex: 'debug', 'error'
axiosConfigObjectN/AAxios config
axiosInstanceObjectN/AAxios instance
axiosRetryConfigObjectN/AAxios retry configuration
retryCountNumber3Number of times requests will be retried by axios if failed
errorHandlerFunctionN/AA function that will be called if request to server failed
gzipBooleantrueWhether to compress request with gzip or not

Example calls

Unlike the client-side SDKs that deal with a single user at a given time, the server-side SDKs deal with multiple users simultaneously. Therefore, you must specify either the userId or anonymousId every time while making any API calls supported by the Node SDK.

Identify

client.identify({
  userId: '1hKOmRA4GRlm',
  traits: {
    name: 'Alex Keener',
    email: 'alex@example.com',
    plan: 'Free',
    friends: 21,
  },
});

Track

client.track({
  userId: '1hKOmRA4GRlm',
  event: 'Item Viewed',
  properties: {
    revenue: 19.95,
    shippingMethod: 'Premium',
  },
});

Page

client.page({
  userId: '1hKOmRA4GRlm',
  category: 'Food',
  name: 'Pizza',
  properties: {
    url: 'https://example.com',
    title: 'Pizza',
    referrer: 'https://google.com',
  },
});

Screen

client.screen({
  userId: '12345',
  category: 'Food',
  name: 'Pizza',
  properties: {
    screenSize: 10,
    title: 'Pizza',
    referrer: 'https://google.com',
  },
});

Group

client.group({
  userId: '12345',
  groupId: '1',
  traits: {
    name: 'Company',
    description: 'Google',
  },
});

Alias

client.alias({
  previousId: 'old_id',
  userId: 'new_id',
});