Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-native-customerio

stefanoeb261MIT1.0.2TypeScript support: included

A react-native for the Customer.io event API. http://customer.io

customerio, react-native, react

readme

A react-native client for the Customer.io REST API.

This project was initally forked from https://github.com/customerio/customerio-node

Installation

npm i --save react-native-customerio

Usage

Creating a new instance

In order to start using the library, you first need to create an instance of the CIO class:

let CIO = require('react-native-customerio');
const cio = new CIO(siteId, apiKey, [defaults]);

Both the siteId and apiKey are required in order to create a Basic Authorization header, allowing us to associate the data with your account.

Optionally you may pass defaults as an object that will be passed to the underlying request instance. A list of the possible options are listed here.

This is useful to override the default 10s timeout. Example:

const cio = new CIO(123, 'abc', {
  timeout: 5000
});

cio.identify(id, data)

Creating a person is as simple as identifying them with this call. You can also use this method to update a persons data.

cio.identify(1, {
  email: 'customer@example.com',
  created_at: 1361205308,
  first_name: 'Bob',
  plan: 'basic'
});

Options

  • id: String (required)
  • data: Object (optional)
    • email is a required key if you intend to send email messages
    • created\at_ is a required key if you want to segment based on signed up/created date

cio.destroy(id)

This will delete a person from Customer.io.

cio.destroy(1);

Options

  • id: String (required)

cio.track(id, data)

The track method will trigger events within Customer.io. When sending data along with your event, it is required to send a name key/value pair in you data object.

Simple event tracking

cio.track(1, { name: 'updated' });

Sending data with an event

cio.track(1, {
  name: 'purchase',
  data: {
    price: '23.45',
    product: 'socks'
  }
});

Options

  • id: String (requiredl)
  • data: Object (required)
    • name is a required key on the Object
    • data is an optional key for additional data sent over with the event

cio.trackAnonymous(data)

Anonymous event tracking does not require a customer ID and these events will not be associated with a tracked profile in Customer.io

cio.trackAnonymous({
  name: 'updated',
  data: {
    updated: true,
    plan: 'free'
  }
});

Options

  • data: Object (required)
    • name is a required key on the Object
    • data is an optional key for additional data sent over with the event

cio.trackPageView(id, url)

Sending a page event includes sending over the customers id and the name of the page.

cio.trackPageView(1, '/home');

Options

  • id: String (required)
  • url: String (required)

cio.triggerBroadcast(campaign_id, data, recipients)

Trigger an email broadcast using the email campaign's id. You can also optionally pass along custom data that will be merged with the liquid template, and additional conditions to filter recipients.

cio.triggerBroadcast(1, { name: 'foo'}, { segment: { id: 7 }});

You can also use emails or ids to select recipients, and pass optional API parameters such as email_ignore_missing.

cio.triggerBroadcast(1, { name: 'foo'},  { emails: ['example@emails.com'], email_ignore_missing: true }
);

You can learn more about the recipient fields available here.

Options

  • id: String (required)
  • data: Object (optional)
  • recipients: Object (optional)

cio.addDevice(id, device_id, platform, data)

Add a device to send push notifications.

cio.addDevice(1, "device_id", "ios", { primary: true });

Options

  • customer_id: String (required)
  • device_id: String (required)
  • platform: String (required)
  • data: Object (optional)

cio.deleteDevice(id, device_id)

Delete a device to remove it from the associated customer and stop sending push notifications to it.

cio.deleteDevice(1, "device_token")

Options

  • customer_id: String (required)
  • device_token: String (required)

cio.suppress(id)

Suppress a customer.

cio.suppress(1)

Options

  • customer_id: String (required)

Using Promises

All calls to the library will return a native promise, allowing you to chain calls as such:

const customerId = 1;

cio.identify(customerId, { first_name: 'Finn' }).then(() => {
  return cio.track(customerId, {
    name: 'updated',
    data: {
      updated: true,
      plan: 'free'
    }
  });
});

Transactional API

To use the Customer.io Transactional API, import our API client and initialize it with an app key.

Create a new SendEmailRequest object containing:

  • transactional_message_id: the ID of the transactional message you want to send, or the body, from, and subject of a new message.
  • to: the email address of your recipients
  • an identifiers object containing the id of your recipient. If the id does not exist, Customer.io will create it.
  • a message_data object containing properties that you want reference in your message using Liquid.
  • You can also send attachments with your message. Use attach to encode attachments.

Use sendEmail referencing your request to send a transactional message. Learn more about transactional messages and SendEmailRequest properties.

const { APIClient, SendEmailRequest } = require("react-native-customerio/api");

const client = new APIClient("your API key");

const request = new SendEmailRequest({
  to: "person@example.com",
  transactional_message_id: "3",
  message_data: {
    name: "Person",
    items: {
      name: "shoes",
      price: "59.99",
    },
    products: [],
  },
  identifiers: {
    id: "2",
  },
});

// (optional) attach a file to your message.
request.attach("receipt.pdf", fs.readFileSync("receipt.pdf"));

client.sendEmail(request)
  .then(res => console.log(res))
  .catch(err => console.log(err.statusCode, err.message))

Further examples

We've included functional examples in the examples/ directory of the repo to further assist in demonstrating how to use this library to integrate with Customer.io

Tests

npm install && npm test

License

Released under the MIT license. See file LICENSE for more details.

changelog

Changelog

All notable changes to this project will be documented in this file.

Unreleased

[1.0.0]

Added

  • Support for the Transactional API

Removed

  • addToSegment and removeFromSegment methods

Changed

  • IDs in the URLs are now escaped.
  • Improved validations for data that's passed in.

[0.7.0]

Changed

  • Catch scenarios where a response body is unexpectedly null (#25)

Added

  • Allow request defaults to be overridden (#26)
  • New API call for supressing customers (#27)

[0.6.0]

Changed

  • Add missing API params to triggerBroadcast (#19)
  • Further improve the triggerBroadcast API call and catch additional params (#20)
  • Switch from Travis CI to Circle CI (#21)

[0.5.0]

Added

  • New API calls for manual segments (addToSegment, removeFromSegment) (#16)

[0.4.0]

Added

  • New API call for adding and removing devices from push notifications (#14)

0.3.0

Changed

  • Huge thanks to @jescalan for his work in modernizing the Javascript to es6 along with updating dependencies. (#13)
  • README now has the correct Travis-CI badge
  • README has standardized and expanded examples (#10)
  • Fixed link in README to official Customer.io API docs
  • Cleaned up .gitignore by removing unnecessary ignore statements

Added

  • This CHANGELOG file along with historical changes to provide better transparancy to changes made to the library
  • New API call for API triggered broadcasts
    • Added a test for the new call
    • Added an example for the new call to README and examples/ dir
  • An example config file for the practical examples
  • Travis-CI builds now use currently maintained LTS versions of Node.JS (6, 8, 9)

Removed

  • .gitkeep files no longer necessary to preserve directories

0.2.0 - 2015-07-22

Removed

  • url.resolve() removed from API calls

0.1.0 - 2015-07-22

Added

  • Initial API client library
    • Create Identify call
    • Create Track call
    • Create Track Page View call
    • Create Customer Delete call
    • Test suite for API calls
  • HTTP Request middleware
    • Create request handler with RSVP.js
    • Create options method for unifying all request calls
    • Create POST request
    • Create PUT request
    • Create DELETE request
    • Test suite for middleware