Skip to main content

Node.js

Last updated 08/27/2026

This guide describes how to integrate the Node.js SDK into your project.

Latest version: v1.5.0

Update time: 2024-03-26

Downloads: Source code

1. Integrate the SDK​

  1. Use npm to get the Node.js SDK:
# Get the SDK
npm install thinkingdata-node --save

# Update the SDK
npm i thinkingdata-node@{version}
  1. Install Logbus

We recommend using SDK + LogBus to collect and report server-side data. To install Logbus, see the following document: LogBus user guide

2. Initialization​

The following sample code initializes the SDK:

const ThinkingData = require('thinkingdata-node');

let teSDK = ThinkingData.initWithLoggingMode('LOG_DIRECTORY', {
filePrefix: 'test',
rotateHourly: true
});

LOG_DIRECTORY is the path of the local folder that data is written to. Set the folder that LogBus monitors to this path, and LogBus will monitor and upload the data. In loggingMode, this SDK does not support writing files from multiple instances.

3. Common features​

To make sure that the distinct ID and account ID can be bound correctly, if your game uses both the distinct ID and the account ID, we strongly recommend that you upload both IDs. Otherwise, accounts may fail to match and users may be counted more than once. For details on ID binding rules, see User identification rules.

3.1 Send events​

You can call track to upload events. We recommend setting event properties and the conditions for sending events based on the document you prepared earlier. The following sample code sends an event:

let trackEvent = {
accountId: '2222',
distinctId: '1111',
event: 'test_event',
time: new Date(),
ip: '202.38.64.1',
properties: {
prop_double: 134.1,
},
callback(e) {
if (e) {
console.log(e);
}
}
};

teSDK.track(trackEvent)
  • The event name is of the string type. It must start with a letter, can contain digits, letters, and underscores "_", and can be up to 50 characters long.
  • Key is the name of the property and is of string type. It must start with a letter, can contain digits, letters, and underscores "_", can be up to 50 characters long, and is case-insensitive. AE converts it to lowercase
  • Value is the value of the property. Supported types are string, number, Boolean, time, object, object group, and array

User properties have the same requirements as event properties

3.2 Set user properties​

For general user properties, you can call userSet to set them. Properties uploaded through this API overwrite the existing property values. If the user property did not exist before, it is created with the same type as the value passed in. The following example sets the user name:

let userSetData = {
accountId: 'node_test',
properties: {
prop_date: new Date(),
prop_double: 134.12,
prop_string: 'hello',
prop_int: 666,
prop_array: ['str1', 'str2'],
},
callback(e) {
if (e) {
console.log(e);
}
}
};

teSDK.userSet(userSetData);

3.3 Send data​

When you use TDLoggingConsumer, the SDK writes the collected data to disk in real time, so you don't need to call the flush() method.

3.4 Shut down the SDK​

teSDK.close();

Shuts down and exits the SDK. Call this API before you shut down the server to avoid losing data in the cache

4. Best practices​

The following sample code includes all of the operations above. We recommend using them in the following order:

var teSDK = ThinkingData.initWithLoggingMode('LOG_DIRECTORY');
let trackEvent = {
accountId: '2222',
distinctId: '1111',
event: 'test_event',
time: new Date(),
ip: '202.38.64.1',
properties: {
prop_double: 134.1,
},
callback(e) {
if (e) {
console.log(e);
}
}
};

teSDK.track(trackEvent)
let userSetData = {
accountId: 'node_test',
properties: {
prop_date: new Date(),
prop_double: 134.12,
prop_string: 'hello',
prop_int: 666,
prop_array: ['str1', 'str2'],
},
callback(e) {
if (e) {
console.log(e);
}
}
};

teSDK.userSet(userSetData);
Was this page helpful?