redux-reporter
Redux middleware for reporting actions to third party APIs. Extremely useful for analytics and error handling. Can be used with various APIs such as New Relic, Sentry, Adobe DTM, Keen
Installation
npm install --save redux-reporter
Usage
Generic Reporting
Create your reporting middleware
import reporter from 'redux-reporter';
export default reporter(({ type, payload }, getState) => {
try {
} catch (err) {}
});
Attach meta data to your actions
export function myAction() {
let type = 'MY_ACTION';
return {
type,
meta: {
report: {
type,
payload: 'example payload'
}
}
};
}
Configure your store with middleware
import { compose, createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers/rootReducer';
import myReporter from './middleware/myReporter';
const isBrowser = (typeof window !== 'undefined');
const enhancer = compose(
applyMiddleware(...[thunk, myReporter]),
isBrowser && window.devToolsExtension) ? window.devToolsExtension() : (f) => f
);
export default (initialState = {}) => {
return createStore(rootReducer, initialState, enhancer);
}
Analytics
This example uses Adobe DTM, but this pattern will work for other APIs (keen, segment, etc.)
export function myAction() {
let type = 'MY_ACTION';
return {
type,
meta: {
analytics: {
type,
payload: 'example payload'
}
}
};
}
import reporter from 'redux-reporter';
const select = ({ meta = {} }) => meta.analytics;
export default reporter(({ type, payload }) => {
try {
window._satellite.setVar('payload', payload);
window._satellite.track(type);
} catch (err) {}
}, select);
New Relic
error reporting
import { errorReporter as newrelicErrorReporter, crashReporter as newrelicCrashReporter } from 'redux-reporter';
const report = (error) => {
try {
window.newrelic.noticeError(error);
} catch (err) {}
};
export const crashReporter = newrelicCrashReporter(report);
export const errorReporter = newrelicErrorReporter(report);
export function myAction() {
let type = 'MY_ERROR_ACTION';
return {
type,
error: true
payload: new Error('My Handled Error')
};
}
analytics reporting
import reporter from 'redux-reporter';
export const analyticsReporter = reporter(({ type, payload }) => {
try {
window.newrelic.addPageAction(type, payload);
} catch (err) {}
}, ({ meta = {} }) => meta.analytics);
export function myAction() {
let type = 'MY_ACTION';
return {
type,
meta: {
analytics: {
type,
payload: 'example payload'
}
}
};
}
Reporting to Multiple APIs
You can report to multiple APIs by configuring multiple middlewares and attaching multiple attributes to your actions
export function myAction() {
let type = 'MY_ACTION';
return {
type,
meta: {
analytics: {
type,
payload: 'example payload'
},
experiments: {
type,
payload: 'example payload'
}
}
};
}
Optimizely
redux-reporter can be used for goal tracking with optimizely
import reporter from 'redux-reporter';
export default reporter(({ type, payload }) => {
window.optimizely = window.optimizely || [];
window.optimizely.push(['trackEvent', type, payload]);
}, ({ meta = {} }) => meta.experiments);
export function myAction() {
let type = 'MY_ACTION';
return {
type,
meta: {
experiments: {
type,
payload: 'example payload'
}
}
};
}
Todo
- Add examples: using global state, Sentry
- Add tests