UNPKG

4.39 kBJavaScriptView Raw
1!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("hoist-non-react-statics"),require("prop-types"),require("deepmerge"),require("react")):"function"==typeof define&&define.amd?define(["exports","hoist-non-react-statics","prop-types","deepmerge","react"],e):e(t.reactTracking={},t.hoistNonReactStatic,t.PropTypes,t.deepmerge,t.react)}(this,function(t,e,r,n,a){e=e&&e.hasOwnProperty("default")?e.default:e,r=r&&r.hasOwnProperty("default")?r.default:r,n=n&&n.hasOwnProperty("default")?n.default:n;var i="default"in a?a.default:a;function o(t){Object.keys(t).length>0&&(window.dataLayer=window.dataLayer||[]).push(t)}var c=r.shape({data:r.object,dispatch:r.func,process:r.func}),s=i.createContext({});function p(t,r){void 0===t&&(t={}),void 0===r&&(r={});var i=r.dispatch;void 0===i&&(i=o);var c=r.dispatchOnMount;void 0===c&&(c=!1);var p=r.process;return function(r){var o=r.displayName||r.name||"Component",u=function(e){function a(t,r){var a=this;e.call(this,t,r),this.trackEvent=function(t){a.getTrackingDispatcher()(n(a.trackingData||{},t||{}))},r.tracking&&r.tracking.process&&p&&console.error("[react-tracking] options.process should be defined once on a top-level component"),this.computeTrackingData(t,r),this.tracking={trackEvent:this.trackEvent,getTrackingData:function(){return a.trackingData}}}return e&&(a.__proto__=e),(a.prototype=Object.create(e&&e.prototype)).constructor=a,a.prototype.componentDidMount=function(){var t=this.context.tracking,e=t&&t.process;if("function"==typeof e&&"function"==typeof c)this.trackEvent(n(e(this.ownTrackingData)||{},c(this.trackingData)||{}));else if("function"==typeof e){var r=e(this.ownTrackingData);(r||!0===c)&&this.trackEvent(r)}else"function"==typeof c?this.trackEvent(c(this.trackingData)):!0===c&&this.trackEvent()},a.prototype.componentWillReceiveProps=function(t,e){this.computeTrackingData(t,e)},a.prototype.getContextForProvider=function(){var t=this.context.tracking;return{tracking:{data:this.trackingData,dispatch:this.getTrackingDispatcher(),process:t&&t.process||p}}},a.prototype.getTrackingDispatcher=function(){var t=this.context.tracking;return t&&t.dispatch||i},a.prototype.computeTrackingData=function(e,r){this.ownTrackingData="function"==typeof t?t(e):t,this.contextTrackingData=r.tracking&&r.tracking.data||{},this.trackingData=n(this.contextTrackingData||{},this.ownTrackingData||{}),this.contextForProvider=this.getContextForProvider()},a.prototype.render=function(){return h(s.Provider,{value:this.contextForProvider},h(r,Object.assign({},this.props,{tracking:this.tracking})))},a}(a.Component);return u.displayName="WithTracking("+o+")",u.contextType=s,e(u,r),u}}function u(t){return void 0===t&&(t={}),e=function(e){return function(){for(var r=this,n=[],a=arguments.length;a--;)n[a]=arguments[a];var i=function(){for(var e=[],a=arguments.length;a--;)e[a]=arguments[a];if(r.props&&r.props.tracking&&"function"==typeof r.props.tracking.trackEvent){var i="function"==typeof t?t(r.props,r.state,n,e):t;i&&r.props.tracking.trackEvent(i)}},o=Reflect.apply(e,this,n);return Promise&&Promise.resolve(o)===o?o.then(i.bind(this)).catch(function(t){throw i(null,t),t}):(i(),o)}},function(t,r,n){var a=n.configurable,i=n.enumerable,o=n.value,c=n.get,s=n.initializer;if(o)return{configurable:a,enumerable:i,value:e(o)};if(c||s)return{configurable:a,enumerable:i,get:function(){if(this===t)return null;var n=s?Reflect.apply(s,this,[]):Reflect.apply(c,this,[]),o=e(n).bind(this);return Reflect.defineProperty(this,r,{configurable:a,enumerable:i,value:o}),o}};throw new Error("called makeClassMemberDecorator on unsupported descriptor")};var e}function f(t,e){return function(){for(var r=[],n=arguments.length;n--;)r[n]=arguments[n];return 1===r.length?p(t,e).apply(void 0,r):u(t).apply(void 0,r)}}var g=r.shape({trackEvent:r.func,getTrackingData:r.func});module.exports=f,t.track=f,t.withTracking=p,t.ReactTrackingContext=s,t.TrackingContextType=c,t.trackEvent=u,t.TrackingPropType=g,t.useTracking=function(){var t=a.useContext(s);if(!t||!t.tracking)throw new Error("Attempting to call `useTracking` without a ReactTrackingContext present. Did you forget to wrap the top of your component tree with `track`?");return a.useMemo(function(){return{getTrackingData:function(){return t.tracking.data},trackEvent:function(e){return t.tracking.dispatch(n(t.tracking.data,e))}}},[t.tracking.data])}});