npm install react-redux-google-analytics --save
Contributions are welcome !
initGa(googleAnalyticsId, propertyId, [gaOptions = 'auto'], [onlyInProduction = false])
// root component component did mount
componentDidMount() {
initGa('UA-XXXXX', 'XX')
}
This function initializes google analytics and creates a windowga property.
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:
import { createStore, applyMiddleware } from 'redux'
import todos from './reducers'
import { gaEventsMiddleware } from 'react-redux-google-analytics'
let store = createStore(
todos,
applyMiddleware(gaEventsMiddleware)
)
withGaEvent(category, action, [label = ''], [value = 0], [fieldsObject = {}]) => (wrappedPayload)
Usage example :
import { withGaEvent } from 'react-redux-google-analytics'
export const addItem = (itemId) => withGaEvent('menu', 'add-item', itemId)(
{
type: TYPES.AN_ACTION_TYPE,
}
)
Equivalent to :
export const addItem = (itemId) => ({
type: TYPES.AN_ACTION_TYPE,
gaEvent: {
category: 'menu',
action: 'add-item',
label: itemId
}
})
gaPageView([page], [fieldsObject])
Usage with react router
import { gaPageView } from 'react-redux-google-analytics';
import { createBrowserHistory } from 'history';
[...]
const history = createBrowserHistory();
history.listen((location) => gaPageView(location.pathname + location.search));
ReactDOM.render((
<Router history={history}>
[...]
gaEvent(category, action, label = '', value = 0, fieldsObject = {})