redux-segmentio

Redux middleware for sending analytics to Segment

Usage no npm install needed!

<script type="module">
  import reduxSegmentio from 'https://cdn.skypack.dev/redux-segmentio';
</script>

README

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
        }
      }
    }
  }
}