Adding Google Analytics to React

后端 未结 6 1547
别那么骄傲
别那么骄傲 2021-01-30 16:23

I am trying to add Google Analytics to a React Web Application.

I know how to do it in HTML/CSS/JS sites and I have integrated it in an AngularJS app too. But, I\'m not

6条回答
  •  攒了一身酷
    2021-01-30 16:48

    I suggest embedding the Segment script into your index.html, use the analytics library that is accessible on the window object, and add tracking calls onto React’s event handlers:

    export default class SignupButton extends Component {
      trackEvent() {
        window.analytics.track('User Signup');
      }
    
      render() {
        return (
          
        );
      }
    }
    

    I’m the maintainer of https://github.com/segmentio/analytics-react. I recommend checking it out if you want to solve this problem by using one singular API to manage your customer data, and be able to integrate into any other analytics tool (we support over 250+ destinations) without writing any additional code.

提交回复
热议问题