This doesn't have any depency on react-router. You can use it with any router you like. See the example for how to use it with react-router
npm install ga-react-router
- In your
webpack.config.js
addnew webpack.DefinePlugin({GA_TRACKING_CODE: JSON.stringify('XXXXXXXX')})
- Use analytics in your
Router.run
code.
import ga from 'ga-react-router'
import createBrowserHistory from 'history/lib/createBrowserHistory'
const history = createBrowserHistory()
// Listen for changes to the current location. The
// listener is called once immediately.
const unlisten = history.listen(location => {
ga('send', location);
});
React.render(<Router history={history}>{routes}</Router>, el)
// When you're finished, stop the listener.
unlisten()
You can pass more information to ga.create()
by adding a GA_CONFIG
object to webpack.DefinePlugin
like so:
new webpack.DefinePlugin({
GA_TRACKING_CODE: JSON.stringify('XXXXXXXX'),
GA_CONFIG: {
'name': 'myTracker'
}
})
You can read further on the subject here.