UNPKG

3.97 kBJavaScriptView Raw
1import t from"hoist-non-react-statics";import r from"prop-types";import n from"deepmerge";import e,{Component as a,useContext as i,useMemo as o}from"react";function c(t){Object.keys(t).length>0&&(window.dataLayer=window.dataLayer||[]).push(t)}var s=r.shape({data:r.object,dispatch:r.func,process:r.func}),p=e.createContext({});function u(r,e){void 0===r&&(r={}),void 0===e&&(e={});var i=e.dispatch;void 0===i&&(i=c);var o=e.dispatchOnMount;void 0===o&&(o=!1);var s=e.process;return function(e){var c=e.displayName||e.name||"Component",u=function(t){function a(r,e){var a=this;t.call(this,r,e),this.trackEvent=function(t){a.getTrackingDispatcher()(n(a.trackingData||{},t||{}))},e.tracking&&e.tracking.process&&s&&console.error("[react-tracking] options.process should be defined once on a top-level component"),this.computeTrackingData(r,e),this.tracking={trackEvent:this.trackEvent,getTrackingData:function(){return a.trackingData}}}return t&&(a.__proto__=t),(a.prototype=Object.create(t&&t.prototype)).constructor=a,a.prototype.componentDidMount=function(){var t=this.context.tracking,r=t&&t.process;if("function"==typeof r&&"function"==typeof o)this.trackEvent(n(r(this.ownTrackingData)||{},o(this.trackingData)||{}));else if("function"==typeof r){var e=r(this.ownTrackingData);(e||!0===o)&&this.trackEvent(e)}else"function"==typeof o?this.trackEvent(o(this.trackingData)):!0===o&&this.trackEvent()},a.prototype.componentWillReceiveProps=function(t,r){this.computeTrackingData(t,r)},a.prototype.getContextForProvider=function(){var t=this.context.tracking;return{tracking:{data:this.trackingData,dispatch:this.getTrackingDispatcher(),process:t&&t.process||s}}},a.prototype.getTrackingDispatcher=function(){var t=this.context.tracking;return t&&t.dispatch||i},a.prototype.computeTrackingData=function(t,e){this.ownTrackingData="function"==typeof r?r(t):r,this.contextTrackingData=e.tracking&&e.tracking.data||{},this.trackingData=n(this.contextTrackingData||{},this.ownTrackingData||{}),this.contextForProvider=this.getContextForProvider()},a.prototype.render=function(){return h(p.Provider,{value:this.contextForProvider},h(e,Object.assign({},this.props,{tracking:this.tracking})))},a}(a);return u.displayName="WithTracking("+c+")",u.contextType=p,t(u,e),u}}function f(t){return void 0===t&&(t={}),r=function(r){return function(){for(var n=this,e=[],a=arguments.length;a--;)e[a]=arguments[a];var i=function(){for(var r=[],a=arguments.length;a--;)r[a]=arguments[a];if(n.props&&n.props.tracking&&"function"==typeof n.props.tracking.trackEvent){var i="function"==typeof t?t(n.props,n.state,e,r):t;i&&n.props.tracking.trackEvent(i)}},o=Reflect.apply(r,this,e);return Promise&&Promise.resolve(o)===o?o.then(i.bind(this)).catch(function(t){throw i(null,t),t}):(i(),o)}},function(t,n,e){var a=e.configurable,i=e.enumerable,o=e.value,c=e.get,s=e.initializer;if(o)return{configurable:a,enumerable:i,value:r(o)};if(c||s)return{configurable:a,enumerable:i,get:function(){if(this===t)return null;var e=s?Reflect.apply(s,this,[]):Reflect.apply(c,this,[]),o=r(e).bind(this);return Reflect.defineProperty(this,n,{configurable:a,enumerable:i,value:o}),o}};throw new Error("called makeClassMemberDecorator on unsupported descriptor")};var r}function g(t,r){return function(){for(var n=[],e=arguments.length;e--;)n[e]=arguments[e];return 1===n.length?u(t,r).apply(void 0,n):f(t).apply(void 0,n)}}var k=r.shape({trackEvent:r.func,getTrackingData:r.func});function v(){var t=i(p);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 o(function(){return{getTrackingData:function(){return t.tracking.data},trackEvent:function(r){return t.tracking.dispatch(n(t.tracking.data,r))}}},[t.tracking.data])}module.exports=g;export{g as track,u as withTracking,p as ReactTrackingContext,s as TrackingContextType,f as trackEvent,k as TrackingPropType,v as useTracking};