I am trying to implement Google Analytics using the react-ga
library which needs to fire whenever the route changes. I have the following setup in App.js:
import createBrowserHistory from 'history/createBrowserHistory';
...
const history = createBrowserHistory();
history.listen((location) => {
console.log('ANALYTICS CODE GOES HERE', location);
});
class App extends Component {
render() {
return (
<Provider store={...}>
<Router history={history}>
....
</Router>
</Provider>
);
}
}
export default App;
When I click on a NavLink
throughout my app, the route changes and loads the appropriate component, but the history.listen()
function never fires. However, if I use the browser's back/forward functionality it does fire.
<NavLink to="/account" activeClassName="active">ACCOUNT</NavLink>
How do I listen to route changes triggered by a NavLink
?
Edit: further discussion taking place here: https://github.com/react-ga/react-ga/issues/122#issuecomment-316427527