| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- /**
- * From ReactGA Community Wiki Page https://github.com/react-ga/react-ga/wiki/React-Router-v4-withTracker
- */
-
- import React, { Component } from 'react';
- import ReactGA from '../../src';
-
- export default function withTracker(WrappedComponent, options = {}) {
- const trackPage = (page) => {
- ReactGA.set({
- page,
- ...options
- });
- ReactGA.pageview(page);
- };
-
- const HOC = class extends Component {
- componentDidMount() {
- const {
- location: { pathname: page }
- } = this.props;
- trackPage(page);
- }
-
- // eslint-disable-next-line camelcase
- UNSAFE_componentWillReceiveProps(nextProps) {
- const {
- location: { pathname: currentPage }
- } = this.props;
- const nextPage = nextProps.location.pathname;
-
- if (currentPage !== nextPage) {
- trackPage(nextPage);
- }
- }
-
- render() {
- return <WrappedComponent {...this.props} />;
- }
- };
-
- return HOC;
- }
|