Skip to content

Instantly share code, notes, and snippets.

@mwq27
Last active February 28, 2018 04:31
Show Gist options
  • Save mwq27/37fdfe66ba66a4ca789ed7437ddbb50e to your computer and use it in GitHub Desktop.
Save mwq27/37fdfe66ba66a4ca789ed7437ddbb50e to your computer and use it in GitHub Desktop.
import eventTracking from './events';
import { createStore, combineReducers, applyMiddleware } from 'redux';
const trackEvent = (properties, state) => {
ReactGA.event(properties); // Track event to google analytics
// Here we can get extra data from our state to attach to the event.
const bigQueryRecord = createRecord(buildRecordParam(properties, state));
postBigQueryEvent(bigQueryRecord); // Track event to BigQuery
};
const trackingMiddleware = store => next => action => {
if (action.type === '@@router/LOCATION_CHANGE') {
const nextPage = `${action.payload.pathname}${action.payload.search}`;
trackPage(nextPage, store.getState());
}
if (eventTracking[action.type]) {
trackEvent(eventTracking[action.type], store.getState());
}
return next(action);
};
// Apply the middleware like this
let myApp = combineReducers(reducers)
let store = createStore(
myApp,
applyMiddleware(
trackingMiddleware
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment