score:0

this is the piece of code you should include:

(function(i,s,o,g,r,a,m){i['googleanalyticsobject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new date();a=s.createelement(o),
m=s.getelementsbytagname(o)[0];a.async=1;a.src=g;m.parentnode.insertbefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

or this if you want to keep your link to the analytics.js

window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new date;

score:0

i suggest using the segment analytics library and following our react quickstart guide. you can track page visits with react-router and user behavior all within react components using 1 single api. here is an example of tracking a user signup click using a react event handler:

export default class signupbutton extends component {
  trackevent() {
    window.analytics.track('user signup');
  }

  render() {
    return (
      <button onclick={this.trackevent}>
        signup with segment today!
      </button>
    );
  }
}

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. 🙂


Related Query

More Query from same tag