Node.js module

You can use the Outbound Node.js module with your application to send user and event data to Outbound. You can also use it to manage device tokens if you have a mobile app and want to use push notifications.

You should only use the Node.js module for server-side JavaScript apps. Use the JavaScript library for client-side apps.

This document covers the following topics:

You can also examine the module's source code on Github.

Install the Outbound module

$ npm install outbound

Include the module in your project

var outbound = require('outbound');
var ob = new outbound('PRIVATE_KEY');

For more information on the private key, see Authentication in the introduction.

Identify a user

Use the identify() method to send user data to Outbound. The method takes the following positional arguments:

  • a string containing the user id
  • an object containing the user attributes. Only the property names in the example below are supported. All are optional

The method returns a promise using the D.js implementation of promises/A+. You can use the success and error callbacks to handle error or success states. See Using callback functions. For additional promise methods, see the D.js documentation.

Example
var userAttributes = {
  firstName: 'Jane',
  lastName: 'Doe',
  email: 'jdoe@example.com',
  phoneNumber: '+1 555-123-4567',
  apnsTokens: ['ios device token'],
  gcmTokens: ['android device token'],
  previousId: 'anonymous_user_123',
  attributes: {
    membership_level: 'Gold'
  },
  groupId: 'group_identifier',
  groupAttributes: {
    some_group_attribute_name: 'Loren Ipsum'
  }
};
ob.identify('4512345', userAttributes).then(
  successCallback,
  errorCallback
);

For more information about the user attributes, see Users. For more information about the callbacks, see Using callback functions.

Track an event

Use the track() method to send event data to Outbound. The method takes the following positional arguments:

  • a string containing the user id
  • a string containing the event name
  • (optional) an object containing custom event properties. See Properties attribute in the Events documentation

The method returns a promise using the D.js implementation of promises/A+. You can use the success and error callbacks to handle error or success states. See Using callback functions. For additional promise methods, see the D.js documentation.

Example
eventProperties = {
  membership_renewalDate: '2018-06-12'
}
ob.track('4512345', 'renewMembership', eventProperties).then(
  successCallback,
  errorCallback
);

For more information about the arguments, see Events. For more information about the callbacks, see Using callback functions.

The track() method sends the data to Outbound as soon as it runs, so make sure it has access to the user and event data.

Provide a device token

If you have a mobile app and want to use push notifications in your Outbound campaigns, you must provide the user's APNs (iOS) or GCM (Android) token to Outbound. You can provide the token in one of two ways:

  • Send the user's token when you identify the user, using the apnsTokens or gcmTokens properties in user attributes object you pass to the identify() method. See Identify a user above.

  • Use the registerApnsToken() (iOS) or registerGcmToken() (Android) method, which take the following positional arguments:

    • a string containing the user id
    • a string containing the device token
Example
ob.registerApnsToken('4512345', 'HjkCGFJA6z...').then(
  successCallback,
  errorCallback
);

Disable a device token

If you no longer want to send notifications to a user's device, use the disableApnsToken() (iOS) or disableGcmToken() (Android) method, which take the following positional arguments:

  • a string containing the user id
  • a string containing the device token

To enable the token again, register it again. See Provide a device token.

If you use the register and disable methods, Outbound recommends not sending any tokens in the identify method. This lets you more easily control the state of your tokens.

Example
ob.disableGcmToken('4512345', '8LAm9ZCAmC...').then(
  successCallback,
  errorCallback
);

Using callback functions

You can run optional callback functions after the operation is complete.

The error callback receives a single parameter consisting of an error object with the following two properties:

  • receivedCall - a boolean indicating whether or not Outbound received the HTTP request
  • message - a string describing what went wrong

The success callback receives no parameters.