@kodo-x/analytics-node
v0.1.3
Published
Kodo's Node.js SDK - send your backend analytics data to the Kodo platform
Maintainers
Readme
@kodo-x/analytics-node
Kodo's Node.js SDK - send your backend analytics data to the Kodo platform.
Getting Started
1. Install the package
npm i @kodo-x/analytics-node2. Initialise the SDK
import Kodo from '@kodo-x/analytics-node';
// or
const Kodo = require('@kodo-x/analytics-node');
const kodoClient = new Kodo('<YOUR_WRITE_KEY>', {
autoEnrich: false,
defaultTrackingProperties: { ... }
});The client constructor takes two arguments:
writeKey- Your Kodo write key. You can find this in the Kodo dashboard underManagement > Account Settings > Developers > Write Keyoptions- 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 tofalsedefaultTrackingProperties- An object containing any default properties to be sent with every event. Defaults to an empty object
3. Tracking events
kodoClient.track({
userId: '<USER_ID>',
anonymousId: '<ANONYMOUS_ID>',
sessionId: '<SESSION_ID>',
event: 'event_name',
timestamp: 1700968957392,
properties: { ... },
idempotencyKey: '<OPTIONAL_IDEMPOTENCY_KEY>'
});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 objectidempotencyKey- (optional) A string used as the idempotency key for the event. If omitted, the SDK generates a UUID automatically. Required by the Kodo Integration API for event ingest.
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
kodoClient.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
Kodo.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
Kodo.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
Kodo.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
Kodo.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
License
Licensed under the MIT License.
