Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

react-ga-core.js 31KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. (function webpackUniversalModuleDefinition(root, factory) {
  2. if(typeof exports === 'object' && typeof module === 'object')
  3. module.exports = factory();
  4. else if(typeof define === 'function' && define.amd)
  5. define([], factory);
  6. else {
  7. var a = factory();
  8. for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
  9. }
  10. })(typeof self !== 'undefined' ? self : this, function() {
  11. return /******/ (function(modules) { // webpackBootstrap
  12. /******/ // The module cache
  13. /******/ var installedModules = {};
  14. /******/
  15. /******/ // The require function
  16. /******/ function __webpack_require__(moduleId) {
  17. /******/
  18. /******/ // Check if module is in cache
  19. /******/ if(installedModules[moduleId]) {
  20. /******/ return installedModules[moduleId].exports;
  21. /******/ }
  22. /******/ // Create a new module (and put it into the cache)
  23. /******/ var module = installedModules[moduleId] = {
  24. /******/ i: moduleId,
  25. /******/ l: false,
  26. /******/ exports: {}
  27. /******/ };
  28. /******/
  29. /******/ // Execute the module function
  30. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  31. /******/
  32. /******/ // Flag the module as loaded
  33. /******/ module.l = true;
  34. /******/
  35. /******/ // Return the exports of the module
  36. /******/ return module.exports;
  37. /******/ }
  38. /******/
  39. /******/
  40. /******/ // expose the modules object (__webpack_modules__)
  41. /******/ __webpack_require__.m = modules;
  42. /******/
  43. /******/ // expose the module cache
  44. /******/ __webpack_require__.c = installedModules;
  45. /******/
  46. /******/ // define getter function for harmony exports
  47. /******/ __webpack_require__.d = function(exports, name, getter) {
  48. /******/ if(!__webpack_require__.o(exports, name)) {
  49. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  50. /******/ }
  51. /******/ };
  52. /******/
  53. /******/ // define __esModule on exports
  54. /******/ __webpack_require__.r = function(exports) {
  55. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  56. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  57. /******/ }
  58. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  59. /******/ };
  60. /******/
  61. /******/ // create a fake namespace object
  62. /******/ // mode & 1: value is a module id, require it
  63. /******/ // mode & 2: merge all properties of value into the ns
  64. /******/ // mode & 4: return value when already ns object
  65. /******/ // mode & 8|1: behave like require
  66. /******/ __webpack_require__.t = function(value, mode) {
  67. /******/ if(mode & 1) value = __webpack_require__(value);
  68. /******/ if(mode & 8) return value;
  69. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  70. /******/ var ns = Object.create(null);
  71. /******/ __webpack_require__.r(ns);
  72. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  73. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  74. /******/ return ns;
  75. /******/ };
  76. /******/
  77. /******/ // getDefaultExport function for compatibility with non-harmony modules
  78. /******/ __webpack_require__.n = function(module) {
  79. /******/ var getter = module && module.__esModule ?
  80. /******/ function getDefault() { return module['default']; } :
  81. /******/ function getModuleExports() { return module; };
  82. /******/ __webpack_require__.d(getter, 'a', getter);
  83. /******/ return getter;
  84. /******/ };
  85. /******/
  86. /******/ // Object.prototype.hasOwnProperty.call
  87. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  88. /******/
  89. /******/ // __webpack_public_path__
  90. /******/ __webpack_require__.p = "";
  91. /******/
  92. /******/
  93. /******/ // Load entry module and return exports
  94. /******/ return __webpack_require__(__webpack_require__.s = 2);
  95. /******/ })
  96. /************************************************************************/
  97. /******/ ([
  98. /* 0 */
  99. /***/ (function(module, exports, __webpack_require__) {
  100. "use strict";
  101. Object.defineProperty(exports, "__esModule", {
  102. value: true
  103. });
  104. exports["default"] = warn;
  105. function warn(s) {
  106. console.warn('[react-ga]', s);
  107. }
  108. /***/ }),
  109. /* 1 */
  110. /***/ (function(module, exports, __webpack_require__) {
  111. "use strict";
  112. Object.defineProperty(exports, "__esModule", {
  113. value: true
  114. });
  115. exports["default"] = trim;
  116. // GA strings need to have leading/trailing whitespace trimmed, and not all
  117. // browsers have String.prototoype.trim().
  118. function trim(s) {
  119. return s && s.toString().replace(/^\s+|\s+$/g, '');
  120. }
  121. /***/ }),
  122. /* 2 */
  123. /***/ (function(module, exports, __webpack_require__) {
  124. "use strict";
  125. Object.defineProperty(exports, "__esModule", {
  126. value: true
  127. });
  128. exports.addTrackers = addTrackers;
  129. exports["default"] = void 0;
  130. exports.event = event;
  131. exports.exception = exception;
  132. exports.ga = ga;
  133. exports.initialize = initialize;
  134. exports.modalview = modalview;
  135. exports.outboundLink = outboundLink;
  136. exports.pageview = pageview;
  137. exports.plugin = void 0;
  138. exports.send = send;
  139. exports.set = set;
  140. exports.testModeAPI = void 0;
  141. exports.timing = timing;
  142. var _format2 = _interopRequireDefault(__webpack_require__(3));
  143. var _removeLeadingSlash = _interopRequireDefault(__webpack_require__(7));
  144. var _trim = _interopRequireDefault(__webpack_require__(1));
  145. var _loadGA = _interopRequireDefault(__webpack_require__(8));
  146. var _warn = _interopRequireDefault(__webpack_require__(0));
  147. var _log = _interopRequireDefault(__webpack_require__(9));
  148. var _testModeAPI = _interopRequireDefault(__webpack_require__(10));
  149. var _excluded = ["category", "action", "label", "value", "nonInteraction", "transport"];
  150. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  151. function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
  152. function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
  153. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
  154. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
  155. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  156. function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
  157. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
  158. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  159. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  160. function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
  161. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
  162. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  163. var _isNotBrowser = typeof window === 'undefined' || typeof document === 'undefined';
  164. var _debug = false;
  165. var _titleCase = true;
  166. var _testMode = false;
  167. var _alwaysSendToDefaultTracker = true;
  168. var _redactEmail = true;
  169. var internalGa = function internalGa() {
  170. var _window;
  171. if (_testMode) return _testModeAPI["default"].ga.apply(_testModeAPI["default"], arguments);
  172. if (_isNotBrowser) return false;
  173. if (!window.ga) return (0, _warn["default"])('ReactGA.initialize must be called first or GoogleAnalytics should be loaded manually');
  174. return (_window = window).ga.apply(_window, arguments);
  175. };
  176. function _format(s) {
  177. return (0, _format2["default"])(s, _titleCase, _redactEmail);
  178. }
  179. function _gaCommand(trackerNames) {
  180. for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  181. args[_key - 1] = arguments[_key];
  182. }
  183. var command = args[0];
  184. if (typeof internalGa === 'function') {
  185. if (typeof command !== 'string') {
  186. (0, _warn["default"])('ga command must be a string');
  187. return;
  188. }
  189. if (_alwaysSendToDefaultTracker || !Array.isArray(trackerNames)) internalGa.apply(void 0, args);
  190. if (Array.isArray(trackerNames)) {
  191. trackerNames.forEach(function (name) {
  192. internalGa.apply(void 0, _toConsumableArray(["".concat(name, ".").concat(command)].concat(args.slice(1))));
  193. });
  194. }
  195. }
  196. }
  197. function _initialize(gaTrackingID, options) {
  198. if (!gaTrackingID) {
  199. (0, _warn["default"])('gaTrackingID is required in initialize()');
  200. return;
  201. }
  202. if (options) {
  203. if (options.debug && options.debug === true) {
  204. _debug = true;
  205. }
  206. if (options.titleCase === false) {
  207. _titleCase = false;
  208. }
  209. if (options.redactEmail === false) {
  210. _redactEmail = false;
  211. }
  212. if (options.useExistingGa) {
  213. return;
  214. }
  215. }
  216. if (options && options.gaOptions) {
  217. internalGa('create', gaTrackingID, options.gaOptions);
  218. } else {
  219. internalGa('create', gaTrackingID, 'auto');
  220. }
  221. }
  222. function addTrackers(configsOrTrackingId, options) {
  223. if (Array.isArray(configsOrTrackingId)) {
  224. configsOrTrackingId.forEach(function (config) {
  225. if (_typeof(config) !== 'object') {
  226. (0, _warn["default"])('All configs must be an object');
  227. return;
  228. }
  229. _initialize(config.trackingId, config);
  230. });
  231. } else {
  232. _initialize(configsOrTrackingId, options);
  233. }
  234. return true;
  235. }
  236. function initialize(configsOrTrackingId, options) {
  237. if (options && options.testMode === true) {
  238. _testMode = true;
  239. } else {
  240. if (_isNotBrowser) {
  241. return;
  242. }
  243. if (!options || options.standardImplementation !== true) (0, _loadGA["default"])(options);
  244. }
  245. _alwaysSendToDefaultTracker = options && typeof options.alwaysSendToDefaultTracker === 'boolean' ? options.alwaysSendToDefaultTracker : true;
  246. addTrackers(configsOrTrackingId, options);
  247. }
  248. /**
  249. * ga:
  250. * Returns the original GA object.
  251. */
  252. function ga() {
  253. for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  254. args[_key2] = arguments[_key2];
  255. }
  256. if (args.length > 0) {
  257. internalGa.apply(void 0, args);
  258. if (_debug) {
  259. (0, _log["default"])("called ga('arguments');");
  260. (0, _log["default"])("with arguments: ".concat(JSON.stringify(args)));
  261. }
  262. }
  263. return window.ga;
  264. }
  265. /**
  266. * set:
  267. * GA tracker set method
  268. * @param {Object} fieldsObject - a field/value pair or a group of field/value pairs on the tracker
  269. * @param {Array} trackerNames - (optional) a list of extra trackers to run the command on
  270. */
  271. function set(fieldsObject, trackerNames) {
  272. if (!fieldsObject) {
  273. (0, _warn["default"])('`fieldsObject` is required in .set()');
  274. return;
  275. }
  276. if (_typeof(fieldsObject) !== 'object') {
  277. (0, _warn["default"])('Expected `fieldsObject` arg to be an Object');
  278. return;
  279. }
  280. if (Object.keys(fieldsObject).length === 0) {
  281. (0, _warn["default"])('empty `fieldsObject` given to .set()');
  282. }
  283. _gaCommand(trackerNames, 'set', fieldsObject);
  284. if (_debug) {
  285. (0, _log["default"])("called ga('set', fieldsObject);");
  286. (0, _log["default"])("with fieldsObject: ".concat(JSON.stringify(fieldsObject)));
  287. }
  288. }
  289. /**
  290. * send:
  291. * Clone of the low level `ga.send` method
  292. * WARNING: No validations will be applied to this
  293. * @param {Object} fieldObject - field object for tracking different analytics
  294. * @param {Array} trackerNames - trackers to send the command to
  295. * @param {Array} trackerNames - (optional) a list of extra trackers to run the command on
  296. */
  297. function send(fieldObject, trackerNames) {
  298. _gaCommand(trackerNames, 'send', fieldObject);
  299. if (_debug) {
  300. (0, _log["default"])("called ga('send', fieldObject);");
  301. (0, _log["default"])("with fieldObject: ".concat(JSON.stringify(fieldObject)));
  302. (0, _log["default"])("with trackers: ".concat(JSON.stringify(trackerNames)));
  303. }
  304. }
  305. /**
  306. * pageview:
  307. * Basic GA pageview tracking
  308. * @param {String} path - the current page page e.g. '/about'
  309. * @param {Array} trackerNames - (optional) a list of extra trackers to run the command on
  310. * @param {String} title - (optional) the page title e. g. 'My Website'
  311. */
  312. function pageview(rawPath, trackerNames, title) {
  313. if (!rawPath) {
  314. (0, _warn["default"])('path is required in .pageview()');
  315. return;
  316. }
  317. var path = (0, _trim["default"])(rawPath);
  318. if (path === '') {
  319. (0, _warn["default"])('path cannot be an empty string in .pageview()');
  320. return;
  321. }
  322. var extraFields = {};
  323. if (title) {
  324. extraFields.title = title;
  325. }
  326. if (typeof ga === 'function') {
  327. _gaCommand(trackerNames, 'send', _objectSpread({
  328. hitType: 'pageview',
  329. page: path
  330. }, extraFields));
  331. if (_debug) {
  332. (0, _log["default"])("called ga('send', 'pageview', path);");
  333. var extraLog = '';
  334. if (title) {
  335. extraLog = " and title: ".concat(title);
  336. }
  337. (0, _log["default"])("with path: ".concat(path).concat(extraLog));
  338. }
  339. }
  340. }
  341. /**
  342. * modalview:
  343. * a proxy to basic GA pageview tracking to consistently track
  344. * modal views that are an equivalent UX to a traditional pageview
  345. * @param {String} modalName e.g. 'add-or-edit-club'
  346. * @param {Array} trackerNames - (optional) a list of extra trackers to run the command on
  347. */
  348. function modalview(rawModalName, trackerNames) {
  349. if (!rawModalName) {
  350. (0, _warn["default"])('modalName is required in .modalview(modalName)');
  351. return;
  352. }
  353. var modalName = (0, _removeLeadingSlash["default"])((0, _trim["default"])(rawModalName));
  354. if (modalName === '') {
  355. (0, _warn["default"])('modalName cannot be an empty string or a single / in .modalview()');
  356. return;
  357. }
  358. if (typeof ga === 'function') {
  359. var path = "/modal/".concat(modalName);
  360. _gaCommand(trackerNames, 'send', 'pageview', path);
  361. if (_debug) {
  362. (0, _log["default"])("called ga('send', 'pageview', path);");
  363. (0, _log["default"])("with path: ".concat(path));
  364. }
  365. }
  366. }
  367. /**
  368. * timing:
  369. * GA timing
  370. * @param args.category {String} required
  371. * @param args.variable {String} required
  372. * @param args.value {Int} required
  373. * @param args.label {String} required
  374. * @param {Array} trackerNames - (optional) a list of extra trackers to run the command on
  375. */
  376. function timing() {
  377. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  378. category = _ref.category,
  379. variable = _ref.variable,
  380. value = _ref.value,
  381. label = _ref.label;
  382. var trackerNames = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
  383. if (typeof ga === 'function') {
  384. if (!category || !variable || typeof value !== 'number') {
  385. (0, _warn["default"])('args.category, args.variable ' + 'AND args.value are required in timing() ' + 'AND args.value has to be a number');
  386. return;
  387. } // Required Fields
  388. var fieldObject = {
  389. hitType: 'timing',
  390. timingCategory: _format(category),
  391. timingVar: _format(variable),
  392. timingValue: value
  393. };
  394. if (label) {
  395. fieldObject.timingLabel = _format(label);
  396. }
  397. send(fieldObject, trackerNames);
  398. }
  399. }
  400. /**
  401. * event:
  402. * GA event tracking
  403. * @param args.category {String} required
  404. * @param args.action {String} required
  405. * @param args.label {String} optional
  406. * @param args.value {Int} optional
  407. * @param args.nonInteraction {boolean} optional
  408. * @param args.transport {string} optional
  409. * @param {{action: string, category: string}} trackerNames - (optional) a list of extra trackers to run the command on
  410. */
  411. function event() {
  412. var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  413. category = _ref2.category,
  414. action = _ref2.action,
  415. label = _ref2.label,
  416. value = _ref2.value,
  417. nonInteraction = _ref2.nonInteraction,
  418. transport = _ref2.transport,
  419. args = _objectWithoutProperties(_ref2, _excluded);
  420. var trackerNames = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
  421. if (typeof ga === 'function') {
  422. // Simple Validation
  423. if (!category || !action) {
  424. (0, _warn["default"])('args.category AND args.action are required in event()');
  425. return;
  426. } // Required Fields
  427. var fieldObject = {
  428. hitType: 'event',
  429. eventCategory: _format(category),
  430. eventAction: _format(action)
  431. }; // Optional Fields
  432. if (label) {
  433. fieldObject.eventLabel = _format(label);
  434. }
  435. if (typeof value !== 'undefined') {
  436. if (typeof value !== 'number') {
  437. (0, _warn["default"])('Expected `args.value` arg to be a Number.');
  438. } else {
  439. fieldObject.eventValue = value;
  440. }
  441. }
  442. if (typeof nonInteraction !== 'undefined') {
  443. if (typeof nonInteraction !== 'boolean') {
  444. (0, _warn["default"])('`args.nonInteraction` must be a boolean.');
  445. } else {
  446. fieldObject.nonInteraction = nonInteraction;
  447. }
  448. }
  449. if (typeof transport !== 'undefined') {
  450. if (typeof transport !== 'string') {
  451. (0, _warn["default"])('`args.transport` must be a string.');
  452. } else {
  453. if (['beacon', 'xhr', 'image'].indexOf(transport) === -1) {
  454. (0, _warn["default"])('`args.transport` must be either one of these values: `beacon`, `xhr` or `image`');
  455. }
  456. fieldObject.transport = transport;
  457. }
  458. }
  459. Object.keys(args).filter(function (key) {
  460. return key.substr(0, 'dimension'.length) === 'dimension';
  461. }).forEach(function (key) {
  462. fieldObject[key] = args[key];
  463. });
  464. Object.keys(args).filter(function (key) {
  465. return key.substr(0, 'metric'.length) === 'metric';
  466. }).forEach(function (key) {
  467. fieldObject[key] = args[key];
  468. }); // Send to GA
  469. send(fieldObject, trackerNames);
  470. }
  471. }
  472. /**
  473. * exception:
  474. * GA exception tracking
  475. * @param args.description {String} optional
  476. * @param args.fatal {boolean} optional
  477. * @param {Array} trackerNames - (optional) a list of extra trackers to run the command on
  478. */
  479. function exception(_ref3, trackerNames) {
  480. var description = _ref3.description,
  481. fatal = _ref3.fatal;
  482. if (typeof ga === 'function') {
  483. // Required Fields
  484. var fieldObject = {
  485. hitType: 'exception'
  486. }; // Optional Fields
  487. if (description) {
  488. fieldObject.exDescription = _format(description);
  489. }
  490. if (typeof fatal !== 'undefined') {
  491. if (typeof fatal !== 'boolean') {
  492. (0, _warn["default"])('`args.fatal` must be a boolean.');
  493. } else {
  494. fieldObject.exFatal = fatal;
  495. }
  496. } // Send to GA
  497. send(fieldObject, trackerNames);
  498. }
  499. }
  500. var plugin = {
  501. /**
  502. * require:
  503. * GA requires a plugin
  504. * @param name {String} e.g. 'ecommerce' or 'myplugin'
  505. * @param options {Object} optional e.g {path: '/log', debug: true}
  506. * @param trackerName {String} optional e.g 'trackerName'
  507. */
  508. require: function require(rawName, options, trackerName) {
  509. if (typeof ga === 'function') {
  510. // Required Fields
  511. if (!rawName) {
  512. (0, _warn["default"])('`name` is required in .require()');
  513. return;
  514. }
  515. var name = (0, _trim["default"])(rawName);
  516. if (name === '') {
  517. (0, _warn["default"])('`name` cannot be an empty string in .require()');
  518. return;
  519. }
  520. var requireString = trackerName ? "".concat(trackerName, ".require") : 'require'; // Optional Fields
  521. if (options) {
  522. if (_typeof(options) !== 'object') {
  523. (0, _warn["default"])('Expected `options` arg to be an Object');
  524. return;
  525. }
  526. if (Object.keys(options).length === 0) {
  527. (0, _warn["default"])('Empty `options` given to .require()');
  528. }
  529. ga(requireString, name, options);
  530. if (_debug) {
  531. (0, _log["default"])("called ga('require', '".concat(name, "', ").concat(JSON.stringify(options)));
  532. }
  533. } else {
  534. ga(requireString, name);
  535. if (_debug) {
  536. (0, _log["default"])("called ga('require', '".concat(name, "');"));
  537. }
  538. }
  539. }
  540. },
  541. /**
  542. * execute:
  543. * GA execute action for plugin
  544. * Takes variable number of arguments
  545. * @param pluginName {String} e.g. 'ecommerce' or 'myplugin'
  546. * @param action {String} e.g. 'addItem' or 'myCustomAction'
  547. * @param actionType {String} optional e.g. 'detail'
  548. * @param payload {Object} optional e.g { id: '1x5e', name : 'My product to track' }
  549. */
  550. execute: function execute(pluginName, action) {
  551. var payload;
  552. var actionType;
  553. for (var _len3 = arguments.length, args = new Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) {
  554. args[_key3 - 2] = arguments[_key3];
  555. }
  556. if (args.length === 1) {
  557. payload = args[0];
  558. } else {
  559. actionType = args[0];
  560. payload = args[1];
  561. }
  562. if (typeof ga === 'function') {
  563. if (typeof pluginName !== 'string') {
  564. (0, _warn["default"])('Expected `pluginName` arg to be a String.');
  565. } else if (typeof action !== 'string') {
  566. (0, _warn["default"])('Expected `action` arg to be a String.');
  567. } else {
  568. var command = "".concat(pluginName, ":").concat(action);
  569. payload = payload || null;
  570. if (actionType && payload) {
  571. ga(command, actionType, payload);
  572. if (_debug) {
  573. (0, _log["default"])("called ga('".concat(command, "');"));
  574. (0, _log["default"])("actionType: \"".concat(actionType, "\" with payload: ").concat(JSON.stringify(payload)));
  575. }
  576. } else if (payload) {
  577. ga(command, payload);
  578. if (_debug) {
  579. (0, _log["default"])("called ga('".concat(command, "');"));
  580. (0, _log["default"])("with payload: ".concat(JSON.stringify(payload)));
  581. }
  582. } else {
  583. ga(command);
  584. if (_debug) {
  585. (0, _log["default"])("called ga('".concat(command, "');"));
  586. }
  587. }
  588. }
  589. }
  590. }
  591. };
  592. /**
  593. * outboundLink:
  594. * GA outboundLink tracking
  595. * @param args.label {String} e.g. url, or 'Create an Account'
  596. * @param {function} hitCallback - Called after processing a hit.
  597. */
  598. exports.plugin = plugin;
  599. function outboundLink(args, hitCallback, trackerNames) {
  600. if (typeof hitCallback !== 'function') {
  601. (0, _warn["default"])('hitCallback function is required');
  602. return;
  603. }
  604. if (typeof ga === 'function') {
  605. // Simple Validation
  606. if (!args || !args.label) {
  607. (0, _warn["default"])('args.label is required in outboundLink()');
  608. return;
  609. } // Required Fields
  610. var fieldObject = {
  611. hitType: 'event',
  612. eventCategory: 'Outbound',
  613. eventAction: 'Click',
  614. eventLabel: _format(args.label)
  615. };
  616. var safetyCallbackCalled = false;
  617. var safetyCallback = function safetyCallback() {
  618. // This prevents a delayed response from GA
  619. // causing hitCallback from being fired twice
  620. safetyCallbackCalled = true;
  621. hitCallback();
  622. }; // Using a timeout to ensure the execution of critical application code
  623. // in the case when the GA server might be down
  624. // or an ad blocker prevents sending the data
  625. // register safety net timeout:
  626. var t = setTimeout(safetyCallback, 250);
  627. var clearableCallbackForGA = function clearableCallbackForGA() {
  628. clearTimeout(t);
  629. if (!safetyCallbackCalled) {
  630. hitCallback();
  631. }
  632. };
  633. fieldObject.hitCallback = clearableCallbackForGA; // Send to GA
  634. send(fieldObject, trackerNames);
  635. } else {
  636. // if ga is not defined, return the callback so the application
  637. // continues to work as expected
  638. setTimeout(hitCallback, 0);
  639. }
  640. }
  641. var testModeAPI = _testModeAPI["default"];
  642. exports.testModeAPI = testModeAPI;
  643. var _default = {
  644. initialize: initialize,
  645. ga: ga,
  646. set: set,
  647. send: send,
  648. pageview: pageview,
  649. modalview: modalview,
  650. timing: timing,
  651. event: event,
  652. exception: exception,
  653. plugin: plugin,
  654. outboundLink: outboundLink,
  655. testModeAPI: _testModeAPI["default"]
  656. };
  657. exports["default"] = _default;
  658. /***/ }),
  659. /* 3 */
  660. /***/ (function(module, exports, __webpack_require__) {
  661. "use strict";
  662. Object.defineProperty(exports, "__esModule", {
  663. value: true
  664. });
  665. exports["default"] = format;
  666. var _redactEmail = _interopRequireDefault(__webpack_require__(4));
  667. var _toTitleCase = _interopRequireDefault(__webpack_require__(6));
  668. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  669. function format() {
  670. var s = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  671. var titleCase = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  672. var redactingEmail = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  673. var _str = s || '';
  674. if (titleCase) {
  675. _str = (0, _toTitleCase["default"])(s);
  676. }
  677. if (redactingEmail) {
  678. _str = (0, _redactEmail["default"])(_str);
  679. }
  680. return _str;
  681. }
  682. /***/ }),
  683. /* 4 */
  684. /***/ (function(module, exports, __webpack_require__) {
  685. "use strict";
  686. Object.defineProperty(exports, "__esModule", {
  687. value: true
  688. });
  689. exports["default"] = redactEmail;
  690. var _warn = _interopRequireDefault(__webpack_require__(0));
  691. var _mightBeEmail = _interopRequireDefault(__webpack_require__(5));
  692. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  693. var redacted = 'REDACTED (Potential Email Address)';
  694. function redactEmail(string) {
  695. if ((0, _mightBeEmail["default"])(string)) {
  696. (0, _warn["default"])('This arg looks like an email address, redacting.');
  697. return redacted;
  698. }
  699. return string;
  700. }
  701. /***/ }),
  702. /* 5 */
  703. /***/ (function(module, exports, __webpack_require__) {
  704. "use strict";
  705. Object.defineProperty(exports, "__esModule", {
  706. value: true
  707. });
  708. exports["default"] = mightBeEmail;
  709. // See if s could be an email address. We don't want to send personal data like email.
  710. // https://support.google.com/analytics/answer/2795983?hl=en
  711. function mightBeEmail(s) {
  712. // There's no point trying to validate rfc822 fully, just look for ...@...
  713. return typeof s === 'string' && s.indexOf('@') !== -1;
  714. }
  715. /***/ }),
  716. /* 6 */
  717. /***/ (function(module, exports, __webpack_require__) {
  718. "use strict";
  719. Object.defineProperty(exports, "__esModule", {
  720. value: true
  721. });
  722. exports["default"] = toTitleCase;
  723. var _trim = _interopRequireDefault(__webpack_require__(1));
  724. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  725. /**
  726. * To Title Case 2.1 - http://individed.com/code/to-title-case/
  727. * Copyright 2008-2013 David Gouch. Licensed under the MIT License.
  728. * https://github.com/gouch/to-title-case
  729. */
  730. var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i; // test
  731. function toTitleCase(string) {
  732. return (0, _trim["default"])(string).replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function (match, index, title) {
  733. if (index > 0 && index + match.length !== title.length && match.search(smallWords) > -1 && title.charAt(index - 2) !== ':' && (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') && title.charAt(index - 1).search(/[^\s-]/) < 0) {
  734. return match.toLowerCase();
  735. }
  736. if (match.substr(1).search(/[A-Z]|\../) > -1) {
  737. return match;
  738. }
  739. return match.charAt(0).toUpperCase() + match.substr(1);
  740. });
  741. }
  742. /***/ }),
  743. /* 7 */
  744. /***/ (function(module, exports, __webpack_require__) {
  745. "use strict";
  746. Object.defineProperty(exports, "__esModule", {
  747. value: true
  748. });
  749. exports["default"] = removeLeadingSlash;
  750. function removeLeadingSlash(string) {
  751. if (string.substring(0, 1) === '/') {
  752. return string.substring(1);
  753. }
  754. return string;
  755. }
  756. /***/ }),
  757. /* 8 */
  758. /***/ (function(module, exports, __webpack_require__) {
  759. "use strict";
  760. Object.defineProperty(exports, "__esModule", {
  761. value: true
  762. });
  763. exports["default"] = _default;
  764. var isLoaded = false;
  765. function _default(options) {
  766. if (isLoaded) return;
  767. isLoaded = true;
  768. var gaAddress = 'https://www.google-analytics.com/analytics.js';
  769. if (options && options.gaAddress) {
  770. gaAddress = options.gaAddress;
  771. } else if (options && options.debug) {
  772. gaAddress = 'https://www.google-analytics.com/analytics_debug.js';
  773. }
  774. var onerror = options && options.onerror; // https://developers.google.com/analytics/devguides/collection/analyticsjs/
  775. /* eslint-disable */
  776. (function (i, s, o, g, r, a, m) {
  777. i['GoogleAnalyticsObject'] = r;
  778. i[r] = i[r] || function () {
  779. (i[r].q = i[r].q || []).push(arguments);
  780. }, i[r].l = 1 * new Date();
  781. a = s.createElement(o), m = s.getElementsByTagName(o)[0];
  782. a.async = 1;
  783. a.src = g;
  784. a.onerror = onerror;
  785. m.parentNode.insertBefore(a, m);
  786. })(window, document, 'script', gaAddress, 'ga');
  787. /* eslint-enable */
  788. }
  789. /***/ }),
  790. /* 9 */
  791. /***/ (function(module, exports, __webpack_require__) {
  792. "use strict";
  793. Object.defineProperty(exports, "__esModule", {
  794. value: true
  795. });
  796. exports["default"] = log;
  797. function log(s) {
  798. console.info('[react-ga]', s);
  799. }
  800. /***/ }),
  801. /* 10 */
  802. /***/ (function(module, exports, __webpack_require__) {
  803. "use strict";
  804. Object.defineProperty(exports, "__esModule", {
  805. value: true
  806. });
  807. exports.gaCalls = exports["default"] = void 0;
  808. var gaCalls = [];
  809. exports.gaCalls = gaCalls;
  810. var _default = {
  811. calls: gaCalls,
  812. ga: function ga() {
  813. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  814. args[_key] = arguments[_key];
  815. }
  816. gaCalls.push([].concat(args));
  817. },
  818. resetCalls: function resetCalls() {
  819. gaCalls.length = 0;
  820. }
  821. };
  822. exports["default"] = _default;
  823. /***/ })
  824. /******/ ]);
  825. });