3

i need to integrate my react application using google analytics. I found the react-ga library which looks reliable and "easy" to use. But i didn't use google analytics in the past and i'm having some dificulties. First of all i use the withTracker component i the demo page of react-ga github project and then in my router i wrap my homepage component with this wrapper. I need to make a pretty simple task but i can't find how. I need to track the number of visitors that hit the homepage. The withTracker component is this:

import React, { Component } from 'react';
import ReactGA from 'react-ga';

const withTracker = (WrappedComponent, options = {}) => {
  const trackPage = (page) => {
    ReactGA.set({
      page,
      options
    });
    ReactGA.pageview(page);
  };

  class HOC extends Component {
    componentDidMount() {
      const page = this.props.location.pathname;
      trackPage(page);
    }

    componentWillReceiveProps(nextProps) {
      const currentPage = this.props.location.pathname;
      const nextPage = nextProps.location.pathname;

      if (currentPage !== nextPage) {
        trackPage(nextPage);
      }
    }

    render() {
      return <WrappedComponent {...this.props} />;
    }
  }
  return HOC;
};

export default withTracker;

and my homePage is this:

import React, { PropTypes } from 'react';
import Footer from './header/footer';

const Main= props => (
  <div>
    <Footer/>
  </div>
);

export default MainExperienceComponent;

Can you help me deal with this issue? Thanks a lot

RamAlx
  • 6,976
  • 23
  • 58
  • 106

0 Answers0