Skip to main content

Node

Percept NodeJS SDK

1. Install

npm install @perceptinsight/percept-node
or
yarn add @perceptinsight/percept-node

2. Initialize Percept SDK

import Percept from "@perceptinsight/percept-js";

Percept.init("Your Project Token");

init method takes an optional initOptions param as second argument. initOptions properties:

propertyrequireddefault value
autoFlushBatchSizefalse50
autoFlushThresholdMsfalse5000
maxApiRetryfalse2

3. Capture Event

Let's get started by sending event data. You can send an event from anywhere in your application.

// Track only event-name. Second argument is the distinct user id. Pass it as undefined if it is not available. We use this id for all user analysis
Percept.capture('Referral Banner Click', 'USER123');

// Track event-name with property
Percept.capture('Screen View', 'USER123', {'screenName': 'Homepage'});

These calls are queued and flushed on regular interval based on the duration and batch size specified while initialising the sdk.

4. Capture Bulk Events

You can bypass the queue and capture multiple events in bulk with this call

await Percept.bulkCapture([
{
eventName: 'Page Viewed',
properties: {
'screenName': 'Homepage'
}
},
{
eventName: 'Order Placed',
properties: {
'orderId': 'order123f31',
'totalAmount': '1200'
}
}
], 'USER123');

The response is returned in this call, so that you can handle retry on failures. As such we suggest you to use this with await

5. Set user properties

use setUserProperties method to set properties on the user profile

// sets user `isVerifiedAccount` property to true
Percept.setUserProperties('USER123', {isVerifiedAccount: true})

We expose some default user property keys in PERCEPT_DEFAULT_USER_PROPERTIES_KEYS. Please use them as this helps in standarization and usage in the Engage feature provided by Percept.

import Percept, {PERCEPT_DEFAULT_USER_PROPERTIES_KEYS} from '@perceptinsight/percept-js'

// sets user `phone` property to '123456789' and `isVerifiedAccount` property to true
Percept.setUserProperties('USER234', {[PERCEPT_DEFAULT_USER_PROPERTIES_KEYS.PHONE]: 123456789, isVerifiedAccount: true})

6. Manually flush the queue

Percept inner events processing queue can be manually flushed by calling this method

Percept.flushPendingEvents();

7. Shut down

Call shutdown function before closing the server to flush any pending events in queue

await Percept.shutDown()

This flushes the events queue and resets the token.