@shopgate/tracking-core
Version:
Tracking core library for the Shopgate Connect PWA.
20 lines • 5.22 kB
JavaScript
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 _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a 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);}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor;}function _callSuper(_this,derived,args){function isNativeReflectConstruct(){if(typeof Reflect==="undefined"||!Reflect.construct)return false;if(Reflect.construct.sham)return false;if(typeof Proxy==="function")return true;try{return!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}));}catch(e){return false;}}derived=_getPrototypeOf(derived);return _possibleConstructorReturn(_this,isNativeReflectConstruct()?Reflect.construct(derived,args||[],_getPrototypeOf(_this).constructor):derived.apply(_this,args));}function _possibleConstructorReturn(self,call){if(call&&(_typeof(call)==="object"||typeof call==="function")){return call;}return _assertThisInitialized(self);}function _assertThisInitialized(self){if(self===void 0){throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return self;}function _getPrototypeOf(o){_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function _getPrototypeOf(o){return o.__proto__||Object.getPrototypeOf(o);};return _getPrototypeOf(o);}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);}import{sendDataRequest}from"../../helpers/helper";import BasePlugin from"../Base";/**
* Tracking Plugin for our unified tracking system
*/var Unified=/*#__PURE__*/function(_BasePlugin){/**
* @param {Object} [options] configuration
*/function Unified(){var _this2;var options=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};_classCallCheck(this,Unified);var trackerName=options.trackerName||'unified';// Invoke the parent constructor
_this2=_callSuper(this,Unified,[trackerName,options]);_this2.register.viewContent(function(data){return data;});_this2.register.setCampaignWithUrl(function(data){return data;});_this2.register.purchase(function(data){return data;});_this2.register.addToCart(function(data){return data;});_this2.register.initiatedCheckout(function(data){return data;});_this2.register.completedRegistration(function(data){return data;});_this2.register.addToWishlist(function(data){return data;});_this2.register.search(function(data){return data;});_this2.register.addedPaymentInfo(function(data){return data;});_this2.trackingCore.register.removeTracker(function(){// Send request to server to remove the tracker
sendDataRequest('remove_unified_trackers');},{trackerName:_this2.trackerName,options:_this2.options});_this2.trackingCore.register.addTracker(function(){// Send request to server to add the tracker again
sendDataRequest('add_unified_trackers');},{trackerName:_this2.trackerName,options:_this2.options});return _this2;}/**
* Helper function to register a plugin for a specific event. Overwrites the parent function with
* special logic for the blacklist system.
*
* @param {string} eventName Name of the event
* @param {Function} callback Callback from the plugin, to modify the data
* @returns {RemoveListener} Function to remove the listener
* @private
*/_inherits(Unified,_BasePlugin);return _createClass(Unified,[{key:"registerHelper",value:function registerHelper(eventName,callback){var _this3=this;// Register the tracking event of the plugin at the core
return this.trackingCore.register[eventName](function(data,scope,blacklist,state){if(typeof _this3.appHandler[eventName]!=='function'){console.warn("this.appHandler[".concat(eventName,"] is not a function"));return;}// Convert the tracking data into the unified format
var unifiedData=BasePlugin.formatData(eventName,data);// Invoke the event callback of the plugin to enable it to extend the data
var finalData=callback(unifiedData,data,scope,state);// Send command to the app via the appHandler
_this3.appHandler[eventName](finalData,{blacklist:true,trackers:blacklist});},{trackerName:this.trackerName,options:this.options});}}]);}(BasePlugin);window.SgUnifiedTracking=Unified;export default Unified;