Analytics
/

Customer.io

Using the customer.io plugin


Integration with customer.io for analytics package.

Click to expand

Installation

Install analytics and @analytics/customerio packages

npm install analytics
npm install @analytics/customerio

How to use

The @analytics/customerio package works in the browser and server-side in node.js. To use, install the package, include in your project and initialize the plugin with analytics.

Below is an example of how to use the browser plugin.

import Analytics from 'analytics'
import customerIOPlugin from '@analytics/customerio'

const analytics = Analytics({
  app: 'awesome-app',
  plugins: [
    customerIOPlugin({
      siteId: '123-xyz'
    })
  ]
})

/* Track a page view */
analytics.page()

/* Track a custom event */
analytics.track('cartCheckout', {
  item: 'pink socks',
  price: 20
})

/* Identify a visitor */
analytics.identify('user-id-xyz', {
  firstName: 'bill',
  lastName: 'murray'
})

After initializing analytics with the customerIOPlugin plugin, data will be sent into Customer.io whenever analytics.page, analytics.track, or analytics.identify are called.

See additional implementation examples for more details on using in your project.

Browser usage

The Customer.io client side browser plugin works with these analytic api methods:

Browser API

const analytics = Analytics({
  app: 'awesome-app',
  plugins: [
    customerIOPlugin({
      siteId: '123-xyz'
    })
  ]
})

Initialization arguments

  • pluginConfig object Plugin settings
  • pluginConfig.siteId string Customer.io site Id for client side tracking
  • pluginConfig.disableAnonymousTraffic (optional) boolean Disable anonymous events from firing

Server-side usage

The Customer.io server-side node.js plugin works with these analytic api methods:

Server-side API

const analytics = Analytics({
  app: 'awesome-app',
  plugins: [
    customerIOServer({
      siteId: '123-xyz',
      apiKey: '9876543'
    })
  ]
})

Initialization arguments

  • pluginConfig object Plugin settings
  • pluginConfig.siteId string Customer.io site Id for server side tracking
  • pluginConfig.apiKey string Customer.io API key for server side tracking

Platforms Supported

The @analytics/customerio package works in the browser and server-side in node.js

Additional examples

Below are additional implementation examples.

Server-side ES6
import Analytics from 'analytics'
import customerIOServer from '@analytics/customerio'

const analytics = Analytics({
  app: 'awesome-app',
  plugins: [
    customerIOServer({
      siteId: '123-xyz',
      apiKey: '9876543'
    })
    // ...other plugins
  ]
})

/* Track a page view */
analytics.page()

/* Track a custom event */
analytics.track('cartCheckout', {
  item: 'pink socks',
  price: 20
})

/* Identify a visitor */
analytics.identify('user-id-xyz', {
  firstName: 'bill',
  lastName: 'murray'
})
Server-side Node.js with common JS

If using node, you will want to import the .default

const analyticsLib = require('analytics').default
const customerIOServer = require('@analytics/customerio').default

const analytics = analyticsLib({
  app: 'my-app-name',
  plugins: [
    customerIOServer({
      siteId: '123-xyz',
      apiKey: '9876543'
    })
  ]
})

/* Track a page view */
analytics.page()

/* Track a custom event */
analytics.track('cartCheckout', {
  item: 'pink socks',
  price: 20
})

/* Identify a visitor */
analytics.identify('user-id-xyz', {
  firstName: 'bill',
  lastName: 'murray'
})
Using in HTML

Below is an example of importing via the unpkg CDN. Please note this will pull in the latest version of the package.

<!DOCTYPE html>
<html>
  <head>
    <title>Using @analytics/customerio in HTML</title>
    <script src="https://unpkg.com/analytics/dist/analytics.min.js"></script>
    <script src="https://unpkg.com/@analytics/customerio/dist/@analytics/customerio.min.js"></script>
    <script type="text/javascript">
      /* Initialize analytics */
      var Analytics = _analytics.init({
        app: 'my-app-name',
        plugins: [
          analyticsCustomerio({
            siteId: '123-xyz'
          })
        ]
      })

      /* Track a page view */
      analytics.page()

      /* Track a custom event */
      analytics.track('cartCheckout', {
        item: 'pink socks',
        price: 20
      })

      /* Identify a visitor */
      analytics.identify('user-id-xyz', {
        firstName: 'bill',
        lastName: 'murray'
      })
    </script>
  </head>
  <body>
    ....
  </body>
</html>
Using in HTML via ES Modules

Using @analytics/customerio in ESM modules.

<!DOCTYPE html>
<html>
  <head>
    <title>Using @analytics/customerio in HTML via ESModules</title>
    <script>
      // Polyfill process.
      // **Note**: Because `import`s are hoisted, we need a separate, prior <script> block.
      window.process = window.process || { env: { NODE_ENV: 'production' } }
    </script>
    <script type="module">
      import analytics from 'https://unpkg.com/analytics/lib/analytics.browser.es.js?module'
      import analyticsCustomerio from 'https://unpkg.com/@analytics/customerio/lib/analytics-plugin-customerio.browser.es.js?module'
      /* Initialize analytics */
      const Analytics = analytics({
        app: 'analytics-html-demo',
        debug: true,
        plugins: [
          analyticsCustomerio({
            siteId: '123-xyz'
          })
          // ... add any other third party analytics plugins
        ]
      })

      /* Track a page view */
      analytics.page()

      /* Track a custom event */
      analytics.track('cartCheckout', {
        item: 'pink socks',
        price: 20
      })

      /* Identify a visitor */
      analytics.identify('user-id-xyz', {
        firstName: 'bill',
        lastName: 'murray'
      })
    </script>
  </head>
  <body>
    ....
  </body>
</html>

See the full list of analytics provider plugins in the main repo.