How to set up Google Analytics for React-Router?

后端 未结 15 670
名媛妹妹
名媛妹妹 2020-12-12 12:25

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

15条回答
  •  无人及你
    2020-12-12 13:23

    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.

提交回复
热议问题