Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 112 additions & 1 deletion lib/react-optimize.js
Original file line number Diff line number Diff line change
@@ -1 +1,112 @@
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react"),require("prop-types")):"function"==typeof define&&define.amd?define("react-optimize",["react","prop-types"],e):"object"==typeof exports?exports["react-optimize"]=e(require("react"),require("prop-types")):t["react-optimize"]=e(t.react,t["prop-types"])}(window,function(t,e){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(r,o,function(e){return t[e]}.bind(null,o));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=2)}([function(e,n){e.exports=t},function(t,e,n){"use strict";e.__esModule=!0;var r=i(n(0)),o=i(n(3));function i(t){return t&&t.__esModule?t:{default:t}}e.default=r.default.createContext||o.default,t.exports=e.default},function(t,e,n){"use strict";n.r(e),n.d(e,"Experiment",function(){return b}),n.d(e,"Variant",function(){return m});var r=n(0),o=n.n(r),i=n(1);function u(t){return(u="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function a(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function c(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function f(t,e,n){return e&&c(t.prototype,e),n&&c(t,n),t}function l(t,e){return!e||"object"!==u(e)&&"function"!=typeof e?y(t):e}function p(t){return(p=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function s(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&d(t,e)}function d(t,e){return(d=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function y(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function h(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}var v=n.n(i)()(),b=function(t){function e(){var t,n;a(this,e);for(var r=arguments.length,o=new Array(r),i=0;i<r;i++)o[i]=arguments[i];return h(y(y(n=l(this,(t=p(e)).call.apply(t,[this].concat(o))))),"state",{variant:null}),h(y(y(n)),"updateVariant",function(t){n.setState({variant:void 0===t||null===t?"0":t})}),h(y(y(n)),"delayedInitialization",function(){var t=window.google_optimize&&window.google_optimize.get(n.props.id);n.updateVariant(t)}),n}return s(e,o.a.Component),f(e,[{key:"componentDidMount",value:function(){var t=this;if(!this.props.id)throw new Error("Please specify the experiment id");var e=window.dataLayer&&window.dataLayer.hide&&window.dataLayer.hide.end;e?window.dataLayer.hide.end=function(){t.delayedInitialization(),e()}:this.delayedInitialization(),window.gtag&&window.gtag("event","optimize.callback",{name:this.props.id,callback:this.updateVariant})}},{key:"componentWillUnmount",value:function(){window.gtag&&window.gtag("event","optimize.callback",{name:this.props.id,callback:this.updateVariant,remove:!0})}},{key:"render",value:function(){return o.a.createElement(v.Provider,{value:this.state.variant},null===this.state.variant?this.props.loader:this.props.children)}}]),e}();h(b,"defaultProps",{loader:null});var m=function(t){function e(){return a(this,e),l(this,p(e).apply(this,arguments))}return s(e,o.a.Component),f(e,[{key:"render",value:function(){var t=this;return o.a.createElement(v.Consumer,null,function(e){return e===t.props.id?t.props.children:null})}}]),e}();e.default={Experiment:b,Variant:m}},function(t,e,n){"use strict";e.__esModule=!0;var r=n(0),o=(u(r),u(n(4))),i=u(n(5));u(n(7));function u(t){return t&&t.__esModule?t:{default:t}}function a(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function c(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function f(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}var l=1073741823;e.default=function(t,e){var n,u,p="__create-react-context-"+(0,i.default)()+"__",s=function(t){function n(){var e,r;a(this,n);for(var o=arguments.length,i=Array(o),u=0;u<o;u++)i[u]=arguments[u];return e=r=c(this,t.call.apply(t,[this].concat(i))),r.emitter=function(t){var e=[];return{on:function(t){e.push(t)},off:function(t){e=e.filter(function(e){return e!==t})},get:function(){return t},set:function(n,r){t=n,e.forEach(function(e){return e(t,r)})}}}(r.props.value),c(r,e)}return f(n,t),n.prototype.getChildContext=function(){var t;return(t={})[p]=this.emitter,t},n.prototype.componentWillReceiveProps=function(t){if(this.props.value!==t.value){var n=this.props.value,r=t.value,o=void 0;!function(t,e){return t===e?0!==t||1/t==1/e:t!=t&&e!=e}(n,r)?(o="function"==typeof e?e(n,r):l,0!=(o|=0)&&this.emitter.set(t.value,o)):o=0}},n.prototype.render=function(){return this.props.children},n}(r.Component);s.childContextTypes=((n={})[p]=o.default.object.isRequired,n);var d=function(e){function n(){var t,r;a(this,n);for(var o=arguments.length,i=Array(o),u=0;u<o;u++)i[u]=arguments[u];return t=r=c(this,e.call.apply(e,[this].concat(i))),r.state={value:r.getValue()},r.onUpdate=function(t,e){0!=((0|r.observedBits)&e)&&r.setState({value:r.getValue()})},c(r,t)}return f(n,e),n.prototype.componentWillReceiveProps=function(t){var e=t.observedBits;this.observedBits=void 0===e||null===e?l:e},n.prototype.componentDidMount=function(){this.context[p]&&this.context[p].on(this.onUpdate);var t=this.props.observedBits;this.observedBits=void 0===t||null===t?l:t},n.prototype.componentWillUnmount=function(){this.context[p]&&this.context[p].off(this.onUpdate)},n.prototype.getValue=function(){return this.context[p]?this.context[p].get():t},n.prototype.render=function(){return function(t){return Array.isArray(t)?t[0]:t}(this.props.children)(this.state.value)},n}(r.Component);return d.contextTypes=((u={})[p]=o.default.object,u),{Provider:s,Consumer:d}},t.exports=e.default},function(t,n){t.exports=e},function(t,e,n){"use strict";(function(e){var n="__global_unique_id__";t.exports=function(){return e[n]=(e[n]||0)+1}}).call(this,n(6))},function(t,e){var n;n=function(){return this}();try{n=n||Function("return this")()||(0,eval)("this")}catch(t){"object"==typeof window&&(n=window)}t.exports=n},function(t,e,n){"use strict";var r=n(8);t.exports=r},function(t,e,n){"use strict";function r(t){return function(){return t}}var o=function(){};o.thatReturns=r,o.thatReturnsFalse=r(!1),o.thatReturnsTrue=r(!0),o.thatReturnsNull=r(null),o.thatReturnsThis=function(){return this},o.thatReturnsArgument=function(t){return t},t.exports=o}])});
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define("react-optimize", [], factory);
else if(typeof exports === 'object')
exports["react-optimize"] = factory();
else
root["react-optimize"] = factory();
})(window, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = "./src/index.js");
/******/ })
/************************************************************************/
/******/ ({

/***/ "./src/index.js":
/*!**********************!*\
!*** ./src/index.js ***!
\**********************/
/*! exports provided: Experiment, Variant, default */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"Experiment\", function() { return Experiment; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"Variant\", function() { return Variant; });\nfunction _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); }\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nfunction _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); } }\n\nfunction _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }\n\nfunction _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === \"object\" || typeof call === \"function\")) { return call; } return _assertThisInitialized(self); }\n\nfunction _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }\n\nfunction _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError(\"this hasn't been initialised - super() hasn't been called\"); } return self; }\n\nfunction _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); }\n\nfunction _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }\n\nfunction _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; }\n\nvar OptimizeContext = React.createContext();\nvar Experiment =\n/*#__PURE__*/\nfunction (_React$Component) {\n _inherits(Experiment, _React$Component);\n\n function Experiment() {\n var _getPrototypeOf2;\n\n var _this;\n\n _classCallCheck(this, Experiment);\n\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Experiment)).call.apply(_getPrototypeOf2, [this].concat(args)));\n\n _defineProperty(_assertThisInitialized(_this), \"state\", {\n variant: null\n });\n\n _defineProperty(_assertThisInitialized(_this), \"updateVariant\", function (value) {\n // if experiment not active, render original\n _this.setState({\n variant: value === undefined || value === null ? \"0\" : value\n });\n });\n\n _defineProperty(_assertThisInitialized(_this), \"delayedInitialization\", function () {\n var value = window.google_optimize && window.google_optimize.get(_this.props.id);\n\n _this.updateVariant(value);\n });\n\n return _this;\n }\n\n _createClass(Experiment, [{\n key: \"componentDidMount\",\n value: function componentDidMount() {\n var _this2 = this;\n\n if (!this.props.id) {\n throw new Error(\"Please specify the experiment id\");\n } // Delayed init\n\n\n var hideEnd = window.dataLayer && window.dataLayer.hide && window.dataLayer.hide.end;\n\n if (hideEnd) {\n window.dataLayer.hide.end = function () {\n _this2.delayedInitialization();\n\n hideEnd();\n };\n } else {\n this.delayedInitialization();\n }\n\n window.gtag && window.gtag(\"event\", \"optimize.callback\", {\n name: this.props.id,\n callback: this.updateVariant\n });\n }\n }, {\n key: \"componentWillUnmount\",\n value: function componentWillUnmount() {\n window.gtag && window.gtag(\"event\", \"optimize.callback\", {\n name: this.props.id,\n callback: this.updateVariant,\n remove: true\n });\n }\n }, {\n key: \"render\",\n value: function render() {\n return React.createElement(OptimizeContext.Provider, {\n value: this.state.variant\n }, this.state.variant === null ? this.props.loader : this.props.children);\n }\n }]);\n\n return Experiment;\n}(React.Component);\n\n_defineProperty(Experiment, \"defaultProps\", {\n loader: null\n});\n\nvar Variant =\n/*#__PURE__*/\nfunction (_React$Component2) {\n _inherits(Variant, _React$Component2);\n\n function Variant() {\n _classCallCheck(this, Variant);\n\n return _possibleConstructorReturn(this, _getPrototypeOf(Variant).apply(this, arguments));\n }\n\n _createClass(Variant, [{\n key: \"render\",\n value: function render() {\n var _this3 = this;\n\n return React.createElement(OptimizeContext.Consumer, null, function (value) {\n return value === _this3.props.id ? _this3.props.children : null;\n });\n }\n }]);\n\n return Variant;\n}(React.Component);\n/* harmony default export */ __webpack_exports__[\"default\"] = ({\n Experiment: Experiment,\n Variant: Variant\n});\n\n//# sourceURL=webpack://react-optimize/./src/index.js?");

/***/ })

/******/ });
});
Loading