0

i'm using react-ga from track my react app using google analytics. I'm fetching the tracking id through an API call and is returned after approximately 4 seconds. My function is this:

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

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

    componentWillReceiveProps(nextProps) {
      console.log(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;

The problem is that the initiliaze function must be called first and is being called with undefined because at first the trackid is undefined(remember that is fetched asynchronously). I used this edit:

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

      class HOC extends Component {
        componentWillReceiveProps(nextProps) {
          if(nextProps.trackId) {
            ReactGA.initiliaze(nextProps.trackId);
            const currentPage = this.props.location.pathname;
            const nextPage = nextProps.location.pathname;
            trackPage(nextPage);
         }
        }

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

      return HOC;
    };

i'm using react-ga from track my react app using google analytics. I'm fetching the tracking id through an API call and is returned after approximately 4 seconds. My function is this:

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

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

    componentWillReceiveProps(nextProps) {
      console.log(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;

The problem is that the initiliaze function must be called first and is being called with undefined because at first the trackid is undefined(remember that is fetched asynchronously). I used this edit:

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

      class HOC extends Component {
        componentWillReceiveProps(nextProps) {
          if(nextProps.trackId) {
            ReactGA.initiliaze(nextProps.trackId);
            const currentPage = this.props.location.pathname;
            const nextPage = nextProps.location.pathname;
            trackPage(nextPage);
         }
        }

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

      return HOC;
    };

but i'm getting an error that cannot read property parentNode of undefined Do you have an ideas what to do?

RamAlx
  • 6,976
  • 23
  • 58
  • 106

2 Answers2

0

Did you set the username like this GoogleAnalytics.set({ username }); before making a call to GoogleAnalytics.initialize? If not try to do that and see if it makes a difference. I had the same issue just now and this worked for me.

apiyo
  • 103
  • 7
0

Calling initialize attempts to inserting an element in the first script element.

document.getElementsByTagName('script')[0]

If seen this throw Cannot read property parentNode of undefined in test environments. It might also do this if you are running JS in node?

Tom
  • 6,325
  • 4
  • 31
  • 55