react-redux-google-analytics
Installation
npm install react-redux-google-analytics --save
Contributions are welcome !
1) Initialize google analytics at the root of your application
initGa(googleAnalyticsId, propertyId, [gaOptions = 'auto'], [onlyInProduction = false])
// root component component did mount { }
This function initializes google analytics and creates a windowga property.
2) Setup the middleware gaEventsMiddleware
gaEventsMiddleware
Redux middleware for google analytics.
If an action has a gaEvent property, then a google analytics event is triggered with the window.ga property.
Usage example:
let store =
3) Attach an event to an action
withGaEvent(category, action, [label = ''], [value = 0], [fieldsObject = {}]) => (wrappedPayload)
Usage example :
const addItem = type: TYPESAN_ACTION_TYPE
Equivalent to :
const addItem = type: TYPESAN_ACTION_TYPE gaEvent: category: 'menu' action: 'add-item' label: itemId
4) Trigger page view hit
gaPageView([page], [fieldsObject])
Usage with react router
;; ... const history = ;history; ReactDOM
5) Trigger events outside of redux actions if you like
gaEvent(category, action, label = '', value = 0, fieldsObject = {})