Adding Google Analytics to React

后端 未结 6 1546
别那么骄傲
别那么骄傲 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:41

    Looking at google's site https://developers.google.com/analytics/devguides/collection/analyticsjs,

    you could also add Google Analytics using this function:

    const enableGA = () => {
      !function(A,n,g,u,l,a,r){A.GoogleAnalyticsObject=l,A[l]=A[l]||function(){
      (A[l].q=A[l].q||[]).push(arguments)},A[l].l=+new Date,a=n.createElement(g),
      r=n.getElementsByTagName(g)[0],a.src=u,r.parentNode.insertBefore(a,r)
      }(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
    
      ga('create', 'UA-XXXXXXXX-X');
      ga('send', 'pageview');
    }
    

    This way you don't need an external library, and it's pretty quick to setup.

提交回复
热议问题