@userflux/backend-js
v1.0.7
Published
UserFlux's Backend JavaScript SDK - send your analytics data to the UserFlux platform
Downloads
2,382
Maintainers
Readme
@userflux/backend-js
UserFlux's Backend JavaScript SDK - send your analytics data to the UserFlux platform.
Getting Started
1. Install the package
npm i @userflux/backend-js
2. Initialise the SDK
import UserFlux from '@userflux/backend-js';
// or
const UserFlux = require('@userflux/backend-js');
const ufClient = new UserFlux('<YOUR_WRITE_KEY>', {
autoEnrich: false,
defaultTrackingProperties: { ... }
});
The client constructor takes two arguments:
writeKey
- Your UserFlux write key. You can find this in the UserFlux dashboard underManagement > Account Settings > Developers > Write Key
options
- An object containing the following optional properties:autoEnrich
- A boolean indicating whether or not to automatically enrich events with additional information such as location properties. Defaults tofalse
defaultTrackingProperties
- An object containing any default properties to be sent with every event. Defaults to an empty object
3. Tracking events
ufClient.track({
userId: '<USER_ID>',
anonymousId: '<ANONYMOUS_ID>',
sessionId: '<SESSION_ID>',
event: 'event_name',
timestamp: 1700968957392,
properties: { ... }
});
The track
method takes a single argument:
parameters
- An object containing the following properties:userId
- (optional) A string representing the user ID of the user who performed the eventanonymousId
- (optional) A optional string representing the anonymous ID of the user who performed the eventsessionId
- (optional) A string representing the session ID of the user who performed the eventevent
- (required) A string representing the name of the eventtimestamp
- (optional) A number representing the timestamp of the event in milliseconds since the Unix epoch. Defaults to the current timeproperties
- (optional) An object containing any properties to be sent with the event. Defaults to an empty object
Note: At least one of userId
or anonymousId
must be provided.
Note: This method is asynchronous and can be awaited if required.
4. Identifying users
ufClient.identify({
userId: '<USER_ID>',
anonymousId: '<ANONYMOUS_ID>',
properties: { ... }
});
The identify
method takes a single argument:
parameters
- An object containing the following properties:userId
- (optional) A string representing the user ID of the user you're identifying with attributesanonymousId
- (optional) A optional string representing the anonymous ID of the user you're identifying with attributesproperties
- (required) An object containing any attributes to be associated with the users profile
Note: At least one of userId
or anonymousId
must be provided.
Note: This method is asynchronous and can be awaited if required.
Other Methods Available
getUserIdFromCookie
UserFlux.getUserIdFromCookie(req.headers.cookie)
If you're using the backend SDK in conjunction with the frontend SDK, you can use this method to extract the user ID set by the frontend SDK from the cookie header of an HTTP request.
The getUserIdFromCookie
method takes a single argument:
cookie
- A string representing the cookie header from an HTTP request
getAnonymousIdFromCookie
UserFlux.getAnonymousIdFromCookie(req.headers.cookie)
If you're using the backend SDK in conjunction with the frontend SDK, you can use this method to extract the anonymous ID set by the frontend SDK from the cookie header of an HTTP request.
The getAnonymousIdFromCookie
method takes a single argument:
cookie
- A string representing the cookie header from an HTTP request
getSessionIdFromCookie
UserFlux.getSessionIdFromCookie(req.headers.cookie)
If you're using the backend SDK in conjunction with the frontend SDK, you can use this method to extract the session ID set by the frontend SDK from the cookie header of an HTTP request.
The getSessionIdFromCookie
method takes a single argument:
cookie
- A string representing the cookie header from an HTTP request
isoTimestampToEpoch
UserFlux.isoTimestampToEpoch('2021-01-01T00:00:00.000Z')
This method converts an ISO timestamp to the number of milliseconds since the Unix epoch.
If you need to provide a timestamp to the track
method, you can use this method to convert an ISO timestamp to the required format.
The isoTimestampToEpoch
method takes a single argument:
isoTimestamp
- A string representing an ISO timestamp