ra

redux-analytics

Analytics middleware for Redux

Showing:

Popularity

Downloads/wk

246

GitHub Stars

491

Maintenance

Last Commit

5yrs ago

Contributors

1

Package

Dependencies

1

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

Build Status Coverage Status npm

redux-analytics

Analytics middleware for Redux.

$ npm install --save redux-analytics

Want to customise your metadata further? Check out redux-tap.

Usage

First, add some analytics metadata to your actions using the Flux Standard Action pattern:

const action = {
  type: 'MY_ACTION',
  meta: {
    analytics: {
      type: 'my-analytics-event',
      payload: {
        some: 'data',
        more: 'stuff'
      }
    }
  }
};

Note that the analytics metadata must also be a Flux Standard Action. If this isn't the case, an error will be printed to the console.

Then, write the middleware to handle the presence of this metadata:

import analytics from 'redux-analytics';
import track from 'my-awesome-analytics-library';

const middleware = analytics(({ type, payload }) => track(type, payload));

If you need to expose shared analytics data to multiple events, your entire state tree is provided as the second argument.

import analytics from 'redux-analytics';
import track from 'my-awesome-analytics-library';

const middleware = analytics(({ type, payload }, state) => {
  track(type, { ...state.analytics, ...payload });
});

If you'd like to use a different meta property than analytics, a custom selector function can be provided as the second argument.

The selector function is only invoked if the action has a meta property, and is provided the entire action as an argument. If the selector returns a falsy value, it will be ignored.

// Given the following middleware configuration:
const select = ({ meta }) => meta.foobar;
const middleware = analytics(({ type, payload }) => track(type, payload), select);

// You can then format a trackable action like this:
const action = {
  type: 'MY_ACTION',
  meta: {
    foobar: {
      type: 'my-analytics-event'
    }
  }
};

Thanks

@pavelvolek and @arturmuller for providing the initial inspiration with redux-keen.

License

MIT License

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial