Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Pageviews with Redux

Sarif Mia edited this page Nov 19, 2019 · 4 revisions
Author @santosh-kore
Usage Redux Middleware To Capture PageViews For Google Analytics
Dependencies redux, react-ga

For original PR see: #307

Implementation

import { createStore, combineReducers, applyMiddleware } from 'redux'
import ReactGA from 'react-ga';

ReactGA.initialize('<GA_TRACKING_ID>');
const gaTrackingMiddleware = store => next => action => {
  if (action.type === '@@router/LOCATION_CHANGE') {
    const nextPage = `${action.payload.location.pathname}${action.payload.location.search}`;
    trackPage(nextPage);
  }
   return next(action);
};

const trackPage = page => {
  ReactGA.pageview(page);
};

let myApp = combineReducers(reducers)
let store = createStore(
  myApp,
  applyMiddleware(gaTrackingMiddleware)
);