Node.js
@usermaven/sdk-js
is an isomorphic package. This package is capable of functioning in both the frontend (browser) and the backend (Node.js).
Getting started
To install the package to your project, please follow the instructions below.
npm install --save @usermaven/sdk-js
The API for Node.js is quite similar to that of the browser, so please be sure to read this page before proceeding. Nonetheless, there are some differences that this page will point out and explain.
On frontend, the easiest way to send data to Usermaven is calling usermaven.track('<event_name>')
. The call will collect all necessary data points information about page: url, user agent etc.
SDK initialization
To get started, initialise the package by replacing the API_KEY
with your workspace API KEY
. You can find it in your workspace settings.
import {
EventPayload,
UsermavenClient,
usermavenClient,
UsermavenOptions,
} from "@usermaven/sdk-js";
const USERMAVEN_OPTS: UsermavenOptions = {
key: 'YOUR_API_KEY',
tracking_host: "https://events.usermaven.com",
autocapture: true,
randomize_url: true
};
const usermaven = usermavenClient(USERMAVEN_OPTS);
Identifying users
If the identity of the user is known, the usermaven.id({email, id}, true)
method should be called. It's important to note the last parameter (doNotSendEvent)
and set it to true, as this will prevent a separate identify event from being sent, which is not usually necessary for server-side tracking.
usermaven.id({
// Required attributes
id: 'lzL24K3kYw', // Unique ID for the user in database.
email: "user@domain.com", // Email address for the user.
created_at: "2021-01-20T09:55:35", // DateTime string in your system that represents when the user first signed up.
// Recommended attributes
// First name and last name are shown on people pages.
first_name: 'John',
last_name: 'Smith',
// Optional attributes (you can name attributes what you wish)
custom: {
plan_name: "premium",
},
// If your product is used by multiple users in a company, we recommend to pass the Company attributes.
company: {
// Required Attributes
id: "uPq9oUGrIt", // Company ID in your database
name: "Usermaven", // Company Name in your database.
created_at: "2021-01-20T09:55:35", // DateTime string in your system that represents when the company first signed up.
// Optional attributes such as industry, website, employee count etc.
custom: {
plan: "enterprise",
industry: "Technology",
website: "https://usermaven.com",
employees: 20
}
}
});
Custom events
You can also send the events through command line using the javascript SDK using usermaven.track('<event_name>')
.
usermaven.track("signed_up", {
// additional properties
});
Additional: Sending events from express server
Currently, it works for express Express (opens in a new tab):
app.get('<url>', async (req, res) => {
await usermaven.track('pageview', {env: envs.express(req, res)});
})