Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

withPageView

Simeon Cheeseman edited this page Jul 31, 2017 · 2 revisions
Author @sergeybekrin
Usage Triggers a pageview when a component is mounted.
Dependencies hoist-non-react-statics: ^1.2.0

Implementation

const ProductPage = () => (
  <div>{/* ... */}</div>
);

module.exports = withPageView(ProductPage/*, '/some/optional/url/param' */);

Code

function withPageView(WrappedComponent, path) {
  var _this = this;
  var childCompName = WrappedComponent.displayName
    || WrappedComponent.name
    || 'Component';
  var WithPageViewHoC = createReactClass({
   componentDidMount: function () {
      const location = path || window.location.pathname + window.location.search;
      _this.set({ page: location });
      _this.pageview(location);
    },
   render: function () {
      return React.createElement(WrappedComponent, this.props);
    }
  });

  WithPageViewHoC.displayName = 'withPageView(' + childCompName + ')';

  return hoistStatics(WithPageViewHoC, WrappedComponent);
}
Clone this wiki locally