Last active
February 28, 2018 04:31
-
-
Save mwq27/37fdfe66ba66a4ca789ed7437ddbb50e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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