# Node.js Source

## How it works[](#how-it-works)

Our Node.js library helps you record source events from your node-side code. Requests from your Node.js server go to our servers, and we route your data to your destinations.

This library uses an internal queue so that your `identify` and `track` calls are fast and [non-blocking](https://nodejs.org/en/guides/blocking-vs-non-blocking#overview-of-blocking-vs-non-blocking). It also batches requests and flushes asynchronously to Customer.io’s servers.

Like our other libraries, you can log anonymous activity—`track` and `page` events—with an `anonymousId`. When you `identify` a person, you can pass the `anonymousId` and we’ll associate the anonymous activity with the identified person.

## Getting Started[](#getting-started)

 We support node 14 or later

If you’re on an earlier version of node, you should upgrade to take advantage of our Node.js library.

1.  Go to the tab and click **Sources**.
    
2.  Click **Add Source** and pick **Node.js**.
    
3.  Give the source a *Name* and click **Complete Setup**. The name is simply a friendly name to help you find and recognize your source in Customer.io.
    
4.  On your Node server, install the source:
    
    ```javascript
     # npm
     npm install @customerio/cdp-analytics-node
     # yarn
     yarn add @customerio/cdp-analytics-node
     # pnpm
     pnpm install @customerio/cdp-analytics-node
    ```
    
5.  Use the `Analytics` constructor and initialize Customer.io with your **API Key**. If you’re in our EU region, make sure you set the `host` parameter to `https://cdp-eu.customer.io`.
    
    ```javascript
     import { Analytics } from '@customerio/cdp-analytics-node'
     // or, if you use require:
     const { Analytics } = require('@customerio/cdp-analytics-node')
    
     // instantiation
     const cioanalytics = new Analytics({ 
       writeKey: '<YOUR_API_KEY>' 
       // if you're in our EU region
       // host: 'https://cdp-eu.customer.io',
     })
    ```
    
    This creates an instance of `Analytics` that you can use to send data to Customer.io. The default initialization settings are production-ready and queue 20 messages before sending requests.
    

Now you’re ready to send requests to Customer.io. Check out our [Pipelines API reference](/integrations/api/cdp), or read further to see example requests and understand the types of requests you can make using our Node.js library.

As you work on your integration, you might want to use [development settings](#development).

### If you’re in our EU data center[](#eu-data-center)

You’ll need to set the `endpoint` parameter to set our EU URL (`https://cdp-eu.customer.io`). Note that our EU regional endpoints account for the location of your data in Customer.io; they don’t account for the locations of your sources and destinations.

```python
import { Analytics } from '@customerio/cdp-analytics-node'

const cioanalytics = new Analytics({ 
  writeKey: '<YOUR_API_KEY>' 
  host: 'https://cdp-eu.customer.io',
})
```

## Identify[](#identify)

The `identify` method tells us who the current website visitor is, and lets you assign unique [traitsA key-value pair that you associate with a person or an object—like a person’s name, the date they were created in your workspace, or a company’s billing date etc. Use attributes to target people and personalize messages.](/journeys/attributes/) to a person.

You should call `identify` when a user creates an account, logs in, etc. You can also call it again whenever a person’s traits change. We’ve shown a typical call with a `traits` object, but we’ve listed all the fields available in an `identify` call below.

You can send an identify call with an `anonymousId` and/or `userId`.

*   **`anonymousId` only**: This assigns traits to a person before you know who they are.
*   **`userId` only**: Identifies a user and sets traits.
*   **both `userId` and `anonymousId`**: Associates the data sent in previous anonymous `page`, `track`, and `identify` calls with the person you identify by `userId`.

```javascript
cioanalytics.identify({
  userId: '019mr8mf4r',
  traits: {
    name: 'Cool Person',
    email: 'cool.person@example.com',
    plan: 'Enterprise',
    friends: 42
  }
});
```

*   integrations object
    
    Contains a list of booleans indicating the integrations that are enabled (true) or disabled (false). By default, all integrations are enabled (returning an empty object). Set `"All": false` to reverse this behavior.
    
    *   *Enabled/Disabled integrations\** boolean
        
*   timestamp string  (date-time)
    
    The ISO-8601 timestamp when the event originally took place. This is mostly useful when you backfill data past events. If you’re not backfilling data, you can leave this field empty and we’ll use the current time or server time.
    
*   traits object
    
    Additional properties that you know about a person. We’ve listed some common/reserved traits below, but you can add any traits that you might use in another system.
    
    *   createdAt string  (date-time)
        
        We recommend that you pass date-time values as ISO 8601 date-time strings. We convert this value to fit destinations where appropriate.
        
    *   email string
        
        A person’s email address. In some cases, you can pass an empty `userId` and we’ll use this value to identify a person.
        
    *   *Additional Traits\** any type
        
        Traits that you want to set on a person. These can take any JSON shape.
        

## Track[](#track)

The `track` method tells us about actions people take—the events people perform—on your site. Every `track` call represents an *event*.

You should track your audience’s activities with events both as performance indicators *and* so you can respond to your audience’s activities with [campaignsCampaigns are automated workflows you set up to send people messages and perform other actions when they meet your criteria.](/journeys/campaigns-in-customerio/) in Journeys. For example, if your audience performs a **Video Viewed** or **Item Purchased** event, you might respond with other videos or products the person might enjoy.

You can send events with an `anonymousId` or a `userId`. Calls that you make with an `anonymousId` are associated with a `userId` when you `identify` someone by their `userId`.

Track calls require an `event` name describing what a person did. And they generally include a series of `properties`, providing additional information about the event. Beyond that, we’ve provided a complete schema for writable event fields below, and you can find more information in our [API documentation](/integrations/api/cdp/#operation/track).

 track with userId

#### track with userId[](#track with userId)

```javascript
cioanalytics.track({
  userId: '019mr8mf4r',
  event: 'added_to_cart',
  properties: {
    product: "shoes",
    revenue: 39.95,
    qty: 1,
    size: 9
  }
});
```

 track with anonymousId

#### track with anonymousId[](#track with anonymousId)

```javascript
cioanalytics.track({
  anonymousId: '48d213bb-95c3-4f8d-af97-86b2b404dcfe',
  event: 'added_to_cart',
  properties: {
    product: "shoes",
    revenue: 39.95,
    qty: 1,
    size: 9
  }
});
```

*   event string
    
    Required The name of the event
    
*   integrations object
    
    Contains a list of booleans indicating the integrations that are enabled (true) or disabled (false). By default, all integrations are enabled (returning an empty object). Set `"All": false` to reverse this behavior.
    
    *   *Enabled/Disabled integrations\** boolean
        
*   properties object
    
    Additional properties for your event.
    
    *   *Event Properties\** any type
        
        Additional properties that you want to capture in the event. These can take any JSON shape.
        
*   timestamp string  (date-time)
    
    The ISO-8601 timestamp when the event originally took place. This is mostly useful when you backfill data past events. If you’re not backfilling data, you can leave this field empty and we’ll use the current time or server time.
    

*   event string
    
    Required The name of the event
    
*   integrations object
    
    Contains a list of booleans indicating the integrations that are enabled (true) or disabled (false). By default, all integrations are enabled (returning an empty object). Set `"All": false` to reverse this behavior.
    
    *   *Enabled/Disabled integrations\** boolean
        
*   properties object
    
    Additional properties for your event.
    
    *   *Event Properties\** any type
        
        Additional properties that you want to capture in the event. These can take any JSON shape.
        
*   timestamp string  (date-time)
    
    The ISO-8601 timestamp when the event originally took place. This is mostly useful when you backfill data past events. If you’re not backfilling data, you can leave this field empty and we’ll use the current time or server time.
    

### Enable automatic geolocation support[](#enable-automatic-geolocation-support)

You can automatically geolocate people when you identify them and pass their IP addresses in the `context.ip` field in your `identify` requests. This helps you gather information about your audience’s location and time zone so you can schedule messages at the right times or send messages relevant to their communities.

If you’ve already set up your integration to capture IP addresses, and you’ve enabled the workspace-level [Automatic Geolocation Data Collection](/journeys/geolocation-data/#enable-or-disable-automatic-geolocation-data-collection) setting, you can enable geolocation for your integration.

**After you set up your integration**, go to your integration’s **Settings** tab and turn on the **Enable Geolocation** setting.

[![settings for a server-side integration showing the Enable Geolocation setting](https://docs.customer.io/images/automatic-geolocation-server.png)](#e920e572d15951c4f5191f022724e78d-lightbox)

 Make sure you capture your users’ IP addresses

If you don’t set the `context.ip` in your requests, we won’t be able to capture geolocation data for your users. If our libraries infer the address as your server’s IP address, it’ll look like everyone is in the same location as your server.

## Page[](#page)

The [Page](/integrations/api/cdp/#operation/page) method records page views on your website, along with optional extra information about the page a person visited.

If you’re using Customer.io’s client-side set up in combination with the Node.js library, page calls are already tracked for you by default on any page that loads the client-side script.

But, if you have a single page app or you *don’t* use our JavaScript client library on your website, you’ll need to send your own page calls.

```javascript
cioanalytics.page({
  userId: '019mr8mf4r',
  category: 'Docs',
  name: 'Customer.io CDP',
  properties: {
    url: 'https://customer.io/cdp/',
    path: '/cdp/',
    title: 'Customer.io CDP',
    referrer: 'https://customer.io'
  }
});
```

*   integrations object
    
    Contains a list of booleans indicating the integrations that are enabled (true) or disabled (false). By default, all integrations are enabled (returning an empty object). Set `"All": false` to reverse this behavior.
    
    *   *Enabled/Disabled integrations\** boolean
        
*   name string
    
    Required The name of the page.
    
*   properties object
    
    Additional properties for your event.
    
    *   category string
        
        The category of the page. This might be useful if you have a single page routes or have a flattened URL structure.
        
    *   *Page Properties\** any type
        
        Additional properties tha tyou want to send with the page event. By default, we capture \`url\`, \`title\`, and stuff.
        
*   timestamp string  (date-time)
    
    The ISO-8601 timestamp when the event originally took place. This is mostly useful when you backfill data past events. If you’re not backfilling data, you can leave this field empty and we’ll use the current time or server time.
    

*   integrations object
    
    Contains a list of booleans indicating the integrations that are enabled (true) or disabled (false). By default, all integrations are enabled (returning an empty object). Set `"All": false` to reverse this behavior.
    
    *   *Enabled/Disabled integrations\** boolean
        
*   name string
    
    Required The name of the page.
    
*   properties object
    
    Additional properties for your event.
    
    *   category string
        
        The category of the page. This might be useful if you have a single page routes or have a flattened URL structure.
        
    *   *Page Properties\** any type
        
        Additional properties tha tyou want to send with the page event. By default, we capture \`url\`, \`title\`, and stuff.
        
*   timestamp string  (date-time)
    
    The ISO-8601 timestamp when the event originally took place. This is mostly useful when you backfill data past events. If you’re not backfilling data, you can leave this field empty and we’ll use the current time or server time.
    

## Group[](#group)

The Group method associates an identified person with a group—like a company, organization, project, online class or any other collective noun you come up with for the same concept. In Customer.io Journeys, we call groups [objectsAn object is a non-person entity that you can associate with one or more people—like a company, account, or online course.](/journeys/objects/).

Group calls are useful for integrations where you maintain relationships between people and larger organizations, like in Customer.io! In Customer.io Journeys, you can store groups as [objectsAn object is a non-person entity that you can associate with one or more people—like a company, account, or online course.](/journeys/objects/), and trigger campaigns based on a person’s relationship to an object—like an account, online class, and so on.

Find more details about `group`, including the **`group` payload**, in our [API spec](/integrations/api/cdp/#operation/group).

```javascript
cioanalytics.group({
  userId: '019mr8mf4r',
  groupId: '56',
  traits: {
    name: 'Initech',
    description: 'Accounting Software'
  }
});
```

 Include `objectTypeId` when you send data to Customer.io

Customer.io supports different kinds of groups (called [objectsAn object is a non-person entity that you can associate with one or more people—like a company, account, or online course.](/journeys/objects/)) where each object has an [object type](/journeys/object-types/) represented by an incrementing integer beginning at 1. If you send `group` calls to Customer.io, you should include the object type ID or we’ll assume that the object type is 1.

*   groupId string
    
    Required ID of the group
    
*   integrations object
    
    Contains a list of booleans indicating the integrations that are enabled (true) or disabled (false). By default, all integrations are enabled (returning an empty object). Set `"All": false` to reverse this behavior.
    
    *   *Enabled/Disabled integrations\** boolean
        
*   timestamp string  (date-time)
    
    The ISO-8601 timestamp when the event originally took place. This is mostly useful when you backfill data past events. If you’re not backfilling data, you can leave this field empty and we’ll use the current time or server time.
    
*   traits object
    
    Additional information about the group.
    
    *   *Group Traits\** any type
        
        Additional traits you want to associate with this group.
        

*   groupId string
    
    Required ID of the group
    
*   integrations object
    
    Contains a list of booleans indicating the integrations that are enabled (true) or disabled (false). By default, all integrations are enabled (returning an empty object). Set `"All": false` to reverse this behavior.
    
    *   *Enabled/Disabled integrations\** boolean
        
*   timestamp string  (date-time)
    
    The ISO-8601 timestamp when the event originally took place. This is mostly useful when you backfill data past events. If you’re not backfilling data, you can leave this field empty and we’ll use the current time or server time.
    
*   traits object
    
    Additional information about the group.
    
    *   *Group Traits\** any type
        
        Additional traits you want to associate with this group.
        

## Alias[](#alias)

The Alias method combines two previously unassociated user identities. Some integrations automatically reconcile profiles with different identifiers based on whether you send `anonymousId`, `userId`, or another trait that the integration expects to be unique. But for integrations that don’t, you may need to send `alias` requests to do this.

In general, you won’t need to use the `alias` call; we try to handle user identification gracefully so you don’t need to merge profiles. But you may need to send `alias` calls to manage user identities in *some* data-out integrations.

For example, in [Mixpanel](/integrations/data-out/connections/mixpanel/#alias) it’s used to associate an anonymous user with an identified user once they sign up.

Here’s how you might use the `alias` call. In this case, we start with an `anonymous_user` and switch to an email address when a person provides their `userId`.

```javascript
// the anonymous user does actions ...
cioanalytics.track({ userId: 'anonymous_user', event: 'Anonymous Event' })
// the anonymous user signs up and is aliased
cioanalytics.alias({ previousId: 'anonymous_user', userId: 'identified@example.com' })
// the identified user is identified
cioanalytics.identify({ userId: 'identified@example.com', traits: { plan: 'Free' } })
// the identified user does actions ...
cioanalytics.track({ userId: 'identified@example.com', event: 'Identified Action' })
```

*   previousId string
    
    Required The userId that you want to merge into the canonical profile.
    
*   userId string
    
    Required The userId that you want to keep. This is required if you haven’t already identified someone with one of our web or server-side libraries.
    

## Configuration[](#configuration)

The first argument for the `Analytics` constructor is a dictionary of configuration settings, including your API key and optional settings.

```javascript
var cioanalytics = new Analytics({
  writeKey: 'YOUR_API_KEY',
  maxEventsInBatch: 20,
  flushInterval: 10000,
});
```

Setting

Details

`maxEventsInBatch`

(*Number*) The number of messages to enqueue before flushing.

`flushInterval`

(*Number*) The number of milliseconds to wait before flushing the queue automatically.

### Error Handling[](#error-handling)

You can listen for `error` events on the `Analytics` instance. Errors contain the following properties:

*   `code`: The code of the error.
*   `reason`: The error, like an HTTP error, network error, etc.
*   `ctx`: The context object (for delivery failures).

```javascript
const { Analytics } = require('@customerio/cdp-analytics-node');

const client = new Analytics({ writeKey: 'api key' });

// Listen to the 'error' event
client.on('error', (err) => {
  console.error('cdp-analytics-node error occurred:');
  console.error('Code:', err.code);
  console.error('Reason:', err.reason);
  if (err.ctx) {
    console.error('Context:', err.ctx);
  }
});

// Now you can make analytics calls
client.track({
  userId: '123',
  event: 'Test Event'
});
```

## Development[](#development)

While integrating with Customer.io, you might want to make our library flush after every event or call. This can help you test your implementation and make sure that all of your calls work properly before you start making calls from your production environment.

```javascript
var cioanalytics = new Analytics({ writeKey: 'YOUR_API_KEY', maxEventsInBatch: 1 });
```

## Selecting Destinations[](#selecting-destinations)

You can pass an `integrations` object to outgoing calls to turn certain destinations on or off. By default all destinations are enabled. Passing `false` for an integration disables the call to that destination.

You might want to do this for things like `alias` calls, which aren’t supported by all destinations. `All: false` disables all destinations except the ones you explicitly specify.

```javascript
cioanalytics.track({
  event: 'Membership Upgraded',
  userId: '97234974',
  integrations: {
    'All': false,
    'Mixpanel': true,
    'Google Analytics': false
  }
})
```

Destination flags are **case sensitive**. You’ll find each integration’s `name` at the top of each integration’s page [in our documentation](/integrations/catalog).

 You can filter track calls on the source’s *Schema* tab

We recommend that you filter events in our UI if you can. It’s easier than writing code, and you can update your source or make changes to your filters without involving developers!

## Backfilling historical data[](#backfilling-historical-data)

You can backfill data by adding a `timestamp` to your calls. This can be helpful if you’ve just switched to Customer.io or you’re getting started with Customer.io and want to send historical data.

You can only do this for destinations that accept timestamped data (most analytics tools like Mixpanel and Amplitude do). The notable destination that *doesn’t* support timestamped data is Google Analytics.

 Leave out the timestamp if you’re tracking real-time events

If you’re only tracking things as they happen, you can leave the `timestamp` out of your calls and we’ll timestamp requests for you.

## Batching[](#batching)

Our libraries are built to support high performance environments. It’s safe to use this library on a web server that serves hundreds of requests per second.

But every method you invoke **does not** result in an HTTP request. Instead, we queue requests in memory and then flush them in [batches](/integrations/api/cdp/#operation/batch), which allows for more efficient operation.

By default, our Node.js source library flushes:

*   The very first call.
*   Every 20 messages (controlled by `options.maxEventsInBatch`).
*   If 10 seconds pass after the previous flush (controlled by `options.flushInterval`)

There is a maximum of `500KB` per batch request and `32KB` per call. If you don’t want to batch messages, you can turn batching off by setting the `maxEventsInBatch` option to `1`.

Batching means that your message might not get sent right away. Every method call takes an optional `callback`, which you can use to know when a particular message is flushed from the queue.

```javascript
cioanalytics.track({
  userId: '019mr8mf4r',
  event: 'Ultimate Played'
}, function(err, batch){
  if (err) // There was an error flushing your message...
  // Your message was successfully flushed!
});
```

## Serverless applications[](#serverless-applications)

When using the library from serverless applications such as AWS Lambda, Cloudflare Workers or Vercel Functions, you should invoke the `closeAndFlush` method to process all data before your lambda exits or is suspended.

Make sure you create the cioanalytics object and call the `closeAndFlush` method from the handler. When you call `closeAndFlush`, you’ll no longer be able to use the cioanalytics object to send messages.

```javascript
export default async function handler(req, res) {
  var cioanalytics = new Analytics({ writeKey: 'YOUR_API_KEY' });

  await cioanalytics.track({
    userId: '019mr8mf4r',
    event: 'Ultimate Played'
  });

  await cioanalytics.closeAndFlush();
  console.log('Flushed, and now this program can exit!');
}
```

## Flush long running processes[](#flush-long-running-processes)

Because we queue messages, you’ll want to capture interruptions (for example, a server restart) and call `closeAndFlush` so that you don’t inadvertently drop requests when you need to perform maintenance on your server.

```js
import { randomUUID } from 'crypto';
import Analytics from 'cdp-analytics-node'

const API_KEY = '...';

const cioanalytics = new Analytics({ writeKey: API_KEY, maxEventsInBatch: 10 });

cioanalytics.track({
  anonymousId: randomUUID(),
  event: 'Test event',
  properties: {
    name: 'Test event',
    timestamp: new Date()
  }
});

const exitGracefully = async (code) => {
  console.log('Flushing events');
  await cioanalytics.closeAndFlush(function(err, batch) {
    console.log('Flushed, and now this program can exit!');
    process.exit(code);
  });
};

[
  'beforeExit', 'uncaughtException', 'unhandledRejection',
  'SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP',
  'SIGABRT','SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV',
  'SIGUSR2', 'SIGTERM',
].forEach(evt => process.on(evt, exitGracefully));

function logEvery2Seconds(i) {
    setTimeout(() => {
        console.log('Infinite Loop Test n:', i);
        logEvery2Seconds(++i);
    }, 2000);
}

logEvery2Seconds(0);
```

## Multiple Clients[](#multiple-clients)

Different parts of your application may require different types of batching, or even sending to multiple Customer.io sources. In these cases, you can initialize multiple instances of `Analytics` with different settings!

```javascript
var Analytics = require('cdp-analytics-node');
var marketingAnalytics = new Analytics({ writeKey: 'MARKETING_API_KEY' });
var appAnalytics = new Analytics({ writeKey: 'APP_API_KEY' });
```