UNPKG

@sergiodxa/analytics

Version:

Use my own analytics service to track events

115 lines (88 loc) 3.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _getPrototypeOf = require("babel-runtime/core-js/object/get-prototype-of"); var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck"); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require("babel-runtime/helpers/createClass"); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require("babel-runtime/helpers/possibleConstructorReturn"); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require("babel-runtime/helpers/inherits"); var _inherits3 = _interopRequireDefault(_inherits2); var _react = require("react"); var _react2 = _interopRequireDefault(_react); var _index = require("./index.js"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Check if is running in localhost * @private * @return {Boolean} */ /** @module analytics/react */ function isLocal() { return location.hostname === "localhost"; } /** * Wrap a Next.js Page to track each page view * @returns {WithAnalytics} */ exports.default = function (isProd) { var isApp = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; return function (Page) { /** * Render the wrapped page and each time is mounted if it's not running in * localhost track a `page view` event. * @class * @extends Component * @param {Object} props The component props */ var WithAnalytics = function (_Component) { (0, _inherits3.default)(WithAnalytics, _Component); function WithAnalytics() { (0, _classCallCheck3.default)(this, WithAnalytics); return (0, _possibleConstructorReturn3.default)(this, (WithAnalytics.__proto__ || (0, _getPrototypeOf2.default)(WithAnalytics)).apply(this, arguments)); } (0, _createClass3.default)(WithAnalytics, [{ key: "componentDidMount", /** * When the component is mounted check if it's running in localhost * If it's not send a `page view` event with the pagename in the description */ value: function componentDidMount() { if (isLocal()) return; var event = { type: "event", action: "page view", description: "User accessed " + window.location.pathname }; (0, _index.track)(event, isProd); } }, { key: "componentDidUpdate", value: function componentDidUpdate() { if (!isApp) return; if (isLocal()) return; var event = { type: "event", action: "page view", description: "User accessed " + window.location.pathname }; (0, _index.track)(event, isProd); } }, { key: "render", value: function render() { return _react2.default.createElement(Page, this.props); } }]); return WithAnalytics; }(_react.Component); if (Page.getInitialProps) { WithAnalytics.getInitialProps = Page.getInitialProps; } return WithAnalytics; }; };