Skip to content

Commit

Permalink
Integrate react-ga with react-router
Browse files Browse the repository at this point in the history
  • Loading branch information
gocreating committed Oct 22, 2016
1 parent 6c5f438 commit 670b27a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import rootReducer from '../common/reducers';
import getRoutes from '../common/routes';
import setupLocale from './setupLocale';
import setupNProgress from './setupNProgress';
import setupGA from './setupGA';
import { setApiEngine } from '../common/actions/apiEngine';
import ApiEngine from '../common/utils/ApiEngine';

setupNProgress();
setupLocale();
let logPageView = setupGA();
const initialState = window.__INITIAL_STATE__;
let store = createStore(rootReducer, initialState, applyMiddleware(thunk));

Expand All @@ -34,7 +36,11 @@ match({
render(
<Provider store={store}>
<LocaleProvider>
<Router history={browserHistory} {...renderProps}>
<Router
history={browserHistory}
onUpdate={logPageView}
{...renderProps}
>
{routes}
</Router>
</LocaleProvider>
Expand Down
14 changes: 14 additions & 0 deletions src/client/setupGA.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import reactGA from 'react-ga';
import configs from '../../configs/project/client';

export default () => {
if (configs.GA) {
reactGA.initialize(configs.GA[process.env.NODE_ENV].trackingID);

return function logPageView() {
reactGA.set({ page: window.location.pathname });
reactGA.pageview(window.location.pathname);
};
}
return undefined;
};

0 comments on commit 670b27a

Please sign in to comment.