I\'m trying set up Google Analytics on my react site, and have come across a few packages, but none of which has the kind of set up that I have in terms of examples. Was ho
I suggest using the Segment analytics library and following the React quickstart guide to track page calls using the react-router library. You can allow the component to handle when the page renders and use componentDidMount to invoke page calls. The example below shows one way you could do this:
const App = () => (
);
export default App;
export default class Home extends Component {
componentDidMount() {
window.analytics.page('Home');
}
render() {
return (
Home page.
);
}
}
I’m the maintainer of https://github.com/segmentio/analytics-react. With Segment, you’ll be able to switch different destinations on-and-off by the flip of a switch if you are interested in trying multiple analytics tools (we support over 250+ destinations) without having to write any additional code.