Back arrowBlog

Announcing the Node SDK

We're excited to announce the release of windsor-node, a Node.js analytics library for Windsor.io. Until now, we required you to use Segment or Mixpanel for Windsor to handle incoming analytics events. Now you have another option.

You can now install windsor-node from npm. To get started, simply

$ yarn add windsor-node

Then,

  1. Create a Windsor Account
  2. Setup the Node source
  3. Create Users on Windsor
    • With windsor-node, call windsor.user(...) everytime a new user signs up or data about a user changes to keep things updated
  4. Track Events
    • You'll want to see every important event or issue your user runs into. Call windsor.event(...) for every event you want to know a user has taken. You can then find these events and more from the Windsor Dashboard.

Read the docs here.

Usage

You can create a new Windsor user from any Node app:

windsor.user({
  userId: "01nc83ns",
  traits: {
    name: "Pranay Prakash",
    email: "hey@windsor.io",
    company: "windsor",
    posts: 42,
  },
});

Then, track the events your users perform

windsor.event({
  userId: "01nc83ns",
  event: "Post Created",
  properties: {
    title: "Getting Started",
    private: true,
  },
});

Then follow the user on Windsor

Lambda Support

This library started as a fork from the Segment analytics-node library. We made a couple of notable improvements based on feedback on the library:

  1. Promise based API. All the methods (user(), event() and flush()) return promises. Each promise is resolved once our servers confirm receiving the event. You usually don't have to await these promises since analytics events should not affect your latency, however, this is not true on a serverless/lambda environment -
  2. Better Serverless/Lambda support. In addition to promises, we fixed a number of issues with the flush method. This enables reliable analytics when using the library on a serverless or lambda environment like AWS Lambda, Vercel or Serverless.

To ensure that all events are sent before responding to the request, make sure to call and await flush() like so:

const Windsor = require("windsor-node");
const windsor = new Windsor("token");

exports.handler = async (event) => {
  let greeting = "";
  if (event.queryStringParameters && event.queryStringParameters.greeting) {
    console.log("Received greeting: " + event.queryStringParameters.greeting);
    greeting = event.queryStringParameters.greeting;
  }

  // A promise is returned, but instead of using await here
  // we can send multiple analytics events and then await a single
  // call to windsor.flush() before returning the response
  windsor.event({
    event: "Sent Greeting",
    properties: {
      greeting,
    },
  });

  const message = `${greeting} World.`;
  const responseBody = { message };
  const response = {
    statusCode: 200,
    body: JSON.stringify(responseBody),
  };

  await windsor.flush();
  return response;
};

Documentation

Documentation is available at https://docs.windsor.io/docs/analytics

Find all your user data in one place. Invites go out every week.