JavaScript library

You can use the Outbound JavaScript library to send user and event data to Outbound. The library is designed to run in the user's browser. For server-side JavaScript apps, use the Node.js module.

This document covers the following topics:

Add the JavaScript snippet to pages

Insert the following snippet at the bottom of your pages, before the closing body tag:

<script type="text/javascript">
window.outbound=window.outbound||[];window.outbound.methods=["identify","track","registerApnsToken","registerGcmToken","disableApnsToken","disableGcmToken"];window.outbound.factory=function(t){return function(){var o=Array.prototype.slice.call(arguments);o.unshift(t);window.outbound.push(o);return window.outbound}};for(var i=0;i<window.outbound.methods.length;i++){var key=window.outbound.methods[i];window.outbound[key]=window.outbound.factory(key)}if(!document.getElementById("outbound-js")){var script=document.createElement("script");script.type="text/javascript";script.id="outbound-js";script.async=true;script.src="https://cdn.outbound.io/PUBLIC_KEY.js";var first=document.getElementsByTagName("script")[0];first.parentNode.insertBefore(script,first)}
</script>

Find and replace the PUBLIC_KEY placeholder near the end of the snippet. Unlike the other libraries, you must specify a public key, not a private one, to authenticate requests to Outbound. For more information, see Authentication in the introduction.

Identify a user

Use the outbound.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
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'
  }
};
outbound.identify('4512345', userAttributes);

The method stores and updates the user id in a cookie.

For more information, see Users.

Track an event

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

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

Note: The method doesn't specify a user id as an argument. Instead, it gets the user id from a cookie.

Example
eventProperties = {
  membership_renewalDate: '2018-06-12'
};
outbound.track('renewMembership', eventProperties);

For more information about the arguments, see Events.

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.