Join us to discuss the challenges, solutions and best practices for in-house JavaScript code sharing. Tuesday, 12/17 at 10am PT/1pm ET.Sign up here »

redux-segmentio

0.1.0 • Public • Published

redux-segmentio

Redux middleware for sending analytics to Segment.io.

Usage

import segmentAnalytics from 'redux-segmentio';
 
// load analytics.js, globally unfortunately
analytics.load("YOUR_WRITE_KEY");
analytics.page();
 
let analyticsGlobals = {}; // define any globals you want to pass on with every event, such as user's browser info
let analyticsMiddleware = segmentAnalytics(window.analytics, analyticsGlobals);
 

The default export is a function requiring segment.io client instance. This function returns a middleware function, that can be applied using applyMiddleware from Redux.

If it receives an action whose meta property contains an analytics property with non-empty collection property, it will record the event in the Keen IO analytics.

Actions

An action that should be recorded to analytics MUST

  • have an analytics property inside its meta property
  • have a collection property inside its analytics property

and MAY

  • have an event property inside its analytics property

collection

The required collection property inside the analytics specifies the Keen IO event collection.

event

The optional event property inside the analytics contains the data of the Keen IO event.

An example of an action:

{
  type: ADD_TO_SHOPPING_CART,
  meta: {
    analytics: {
      collection: "add_item_to_shopping_cart"
    }
  }
}

An example with optional property event:

{
  type: ADD_TO_SHOPPING_CART,
  meta: {
    analytics: {
      collection: "add_item_to_shopping_cart",
      event: {
        item: {
          title: item.title,
          itemId: item.itemId
        }
      }
    }
  }
}

Install

npm i redux-segmentio

DownloadsWeekly Downloads

1

Version

0.1.0

License

MIT

Last publish

Collaborators

  • avatar