react-router-ga
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,31 +6,44 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports["default"] = void 0; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var React = _interopRequireWildcard(require("react")); | ||
var _react = require('react'); | ||
var _reactRouterDom = require("react-router-dom"); | ||
var React = _interopRequireWildcard(_react); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } } | ||
var _reactRouterDom = require('react-router-dom'); | ||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
var ReactRouterGA = function (_React$Component) { | ||
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } | ||
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } | ||
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } | ||
var ReactRouterGA = | ||
/*#__PURE__*/ | ||
function (_React$Component) { | ||
_inherits(ReactRouterGA, _React$Component); | ||
function ReactRouterGA(props) { | ||
var _this; | ||
_classCallCheck(this, ReactRouterGA); | ||
var _this = _possibleConstructorReturn(this, (ReactRouterGA.__proto__ || Object.getPrototypeOf(ReactRouterGA)).call(this, props)); | ||
_this = _possibleConstructorReturn(this, _getPrototypeOf(ReactRouterGA).call(this, props)); | ||
_this.sendPageView = _this.sendPageView.bind(_assertThisInitialized(_this)); | ||
_this.initialize = _this.initialize.bind(_assertThisInitialized(_this)); | ||
_this.sendPageView = _this.sendPageView.bind(_this); | ||
_this.initialize = _this.initialize.bind(_this); | ||
_this.initialize(props.id); | ||
_this.initialize(props.id); | ||
return _this; | ||
@@ -40,3 +53,3 @@ } | ||
_createClass(ReactRouterGA, [{ | ||
key: 'componentDidMount', | ||
key: "componentDidMount", | ||
value: function componentDidMount() { | ||
@@ -47,3 +60,3 @@ this.sendPageView(this.props.location); | ||
}, { | ||
key: 'initialize', | ||
key: "initialize", | ||
value: function initialize() { | ||
@@ -53,12 +66,17 @@ if (!this.props.id) { | ||
return; | ||
} | ||
} // Load Google Analytics | ||
// Load Google Analytics | ||
(function (i, s, o, g, r, a, m) { | ||
i['GoogleAnalyticsObject'] = r;i[r] = i[r] || function () { | ||
i['GoogleAnalyticsObject'] = r; | ||
i[r] = i[r] || function () { | ||
(i[r].q = i[r].q || []).push(arguments); | ||
}, i[r].l = 1 * new Date();a = s.createElement(o), m = s.getElementsByTagName(o)[0];a.async = 1;a.src = g;m.parentNode.insertBefore(a, m); | ||
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga'); | ||
}, i[r].l = 1 * new Date(); | ||
a = s.createElement(o), m = s.getElementsByTagName(o)[0]; | ||
a.async = 1; | ||
a.src = g; | ||
m.parentNode.insertBefore(a, m); | ||
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga'); // Initialize Google Analytics | ||
// Initialize Google Analytics | ||
window.ga('create', this.props.id, 'auto'); | ||
@@ -68,3 +86,3 @@ window.ga('send', 'pageview'); | ||
}, { | ||
key: 'sendPageView', | ||
key: "sendPageView", | ||
value: function sendPageView(location) { | ||
@@ -74,5 +92,5 @@ // Do nothing if GA was not initialized due to a missing tracking ID. | ||
return; | ||
} | ||
} // Do nothing if trackPathnameOnly is enabled and the pathname didn't change. | ||
// Do nothing if trackPathnameOnly is enabled and the pathname didn't change. | ||
if (this.props.trackPathnameOnly && location.pathname === this.lastPathname) { | ||
@@ -82,19 +100,16 @@ return; | ||
this.lastPathname = location.pathname; | ||
this.lastPathname = location.pathname; // Sets the page value on the tracker. If a basename is provided, then it is prepended to the pathname. | ||
// Sets the page value on the tracker. If a basename is provided, then it is prepended to the pathname. | ||
var page = this.props.basename ? '' + this.props.basename + location.pathname : location.pathname; | ||
var page = this.props.basename ? "".concat(this.props.basename).concat(location.pathname) : location.pathname; | ||
window.ga('set', 'page', page); // Sending the pageview no longer requires passing the page | ||
// value since it's now stored on the tracker object. | ||
window.ga('set', 'page', page); | ||
// Sending the pageview no longer requires passing the page | ||
// value since it's now stored on the tracker object. | ||
window.ga('send', 'pageview'); | ||
if (this.props.debug) { | ||
console.info('[react-router-ga] Page view: ' + page); | ||
console.info("[react-router-ga] Page view: ".concat(page)); | ||
} | ||
} | ||
}, { | ||
key: 'render', | ||
key: "render", | ||
value: function render() { | ||
@@ -112,2 +127,4 @@ return this.props.children; | ||
exports.default = (0, _reactRouterDom.withRouter)(ReactRouterGA); | ||
var _default = (0, _reactRouterDom.withRouter)(ReactRouterGA); | ||
exports["default"] = _default; |
{ | ||
"name": "react-router-ga", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"main": "lib/index.js", | ||
@@ -27,12 +27,13 @@ "license": "MIT", | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-flow": "^6.23.0", | ||
"babel-preset-react": "^6.24.1", | ||
"flow-bin": "^0.61.0" | ||
"@babel/cli": "^7.5.5", | ||
"@babel/core": "^7.5.5", | ||
"@babel/preset-env": "^7.5.5", | ||
"@babel/preset-flow": "^7.0.0", | ||
"@babel/preset-react": "^7.0.0", | ||
"flow-bin": "^0.103.0" | ||
}, | ||
"peerDependencies": { | ||
"react": "^16.2.0", | ||
"react-router-dom": "^4.2.2" | ||
"react": "^16.8.6", | ||
"react-router-dom": "^5.0.0" | ||
} | ||
} |
@@ -10,3 +10,3 @@ # react-router-ga | ||
| `id` | string | Google Analytics tracking ID | Required | | ||
| `basename` | string | If provided, react-router-ga will prepend the basename to the pathname of each page view. (This should match the `basename` provided to the React Router `BrowserRouter` component. See [here](https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/api/BrowserRouter.md#basename-string) for documentation.) | ||
| `basename` | string | If provided, react-router-ga will prepend the basename to the pathname of each page view. (This should match the `basename` provided to the React Router `BrowserRouter` component. See [here](https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/api/BrowserRouter.md#basename-string) for documentation.) | - | | ||
| `debug` | boolean | If enabled, react-router-ga will log all page views to the console | `false` | | ||
@@ -22,13 +22,12 @@ | `trackPathnameOnly` | boolean | If enabled, react-router-ga will only send page views when the pathname changed | `false` | | ||
import Analytics from 'react-router-ga'; | ||
import App from './containers/App'; | ||
import Login from './containers/Login'; | ||
import { Home, Login, NoMatch } from './components'; | ||
ReactDOM.render( | ||
<BrowserRouter> | ||
<Analytics id="UA-111111111-1" debug> | ||
<App> | ||
<Switch> | ||
<Route exact path="/login" component={Login} /> | ||
</Switch> | ||
</App> | ||
<Analytics id="UA-123456789-0" debug> | ||
<Switch> | ||
<Route exact path="/" component={Home}/> | ||
<Route exact path="/login" component={Login} /> | ||
<Route component={NoMatch} /> | ||
</Switch> | ||
</Analytics> | ||
@@ -40,4 +39,8 @@ </BrowserRouter>, | ||
## Demo App | ||
You can also have a look at this simple app bootstrapped with [create-react-app](https://github.com/facebook/create-react-app/) that's making use of [react-router](https://github.com/ReactTraining/react-router) and **react-router-ga**: https://github.com/fknussel/react-router-ga-example. | ||
## Dependencies | ||
This project has `react@^16.2.0` and `react-router-dom@^4.2.2` as peer dependencies. | ||
This project has `react@^16.0.0` and `react-router-dom@^4.0.0` as peer dependencies. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
117878
61.9%158
7.48%44
7.32%6
20%