From 670b27a0dfeba1b66b442bd5e0bfe5d577d10c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=81=E6=B2=BB=E5=B9=B3?= Date: Sun, 23 Oct 2016 00:10:32 +0800 Subject: [PATCH] Integrate react-ga with react-router --- src/client/index.js | 8 +++++++- src/client/setupGA.js | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/client/setupGA.js diff --git a/src/client/index.js b/src/client/index.js index fbd80f2..75513bb 100644 --- a/src/client/index.js +++ b/src/client/index.js @@ -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)); @@ -34,7 +36,11 @@ match({ render( - + {routes} diff --git a/src/client/setupGA.js b/src/client/setupGA.js new file mode 100644 index 0000000..ed9cb3a --- /dev/null +++ b/src/client/setupGA.js @@ -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; +};