UNPKG

@shopgate/tracking-core

Version:

Tracking core library for the Shopgate Connect PWA.

48 lines 4.85 kB
function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}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;}import SgTrackingCore from"../core/Core";import SgTrackingAppHandler from"../core/AppHandler";import dataFormatHelpers from"../helpers/formatHelpers";import eventNames from"../helpers/events";/** * Parent class for all tracking plugins that contains common public functions */var Base=/*#__PURE__*/function(){/** * Constructor * @param {string} trackerName The name of the tracker that is represented by the plugin * @param {Object} options Configuration for the plugin * @param {Object} extendedDefaults Additional default options that are * needed by the inherited class */function Base(trackerName){var _this=this;var options=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var extendedDefaults=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};_classCallCheck(this,Base);var defaults={overrideUnified:false,useNativeSdk:false,useNetPrices:false};// Create the options for the plugin this.options=_extends({},defaults,{},extendedDefaults,{},options);this.trackingDisabled=false;this.trackerName=trackerName||'';this.trackingCore=SgTrackingCore;this.appHandler=SgTrackingAppHandler;/** * Storage that contains functions to register callbacks for the different tracking events * @type {Object} */this.register={};eventNames.forEach(function(eventName){/** * Function to register a plugin for the event * * @param {Function} callback Function that is called if the event occurs * @param {Object} [optionParam] Options that will be passed to the core * @returns {RemoveListener} Function to remove the listener */_this.register[eventName]=function(callback,optionParam){return _this.registerHelper(eventName,callback,optionParam);};});}/** * Converts raw tracking event data into the unified data format * * @param {string} eventName Name of the tracking event * @param {Object} rawData Raw data from the core * @returns {*} The converted data */return _createClass(Base,[{key:"registerHelper",value:/** * Helper function to register a plugin for a specific event. Can be overwritten in the plugins. * * @param {string} eventName Name of the event * @param {Function} callback Callback from the plugin, to modify the data * @param {Object} options Additional options that will be passed to the core * @returns {RemoveListener} Function to remove the listener * @private */function registerHelper(eventName,callback,options){var _this2=this;// Register the tracking event of the plugin at the core return this.trackingCore.register[eventName](function(data,scope,blacklist,state){// Convert the tracking data into the unified format var unifiedData=Base.formatData(eventName,data);// Invoke the event callback of the plugin to enable it to extend the data var finalData=callback(unifiedData,data,scope,state);// If final data is explicitly false, it means further processing is up to the plugin only. if(finalData===false){return;}if(_this2.options.useNativeSdk&&_this2.options.overrideUnified){// Send command to the app via the appHandler _this2.appHandler[eventName](finalData,{blacklist:false,trackers:[_this2.trackerName]});}},_extends({},options,{trackerName:this.trackerName,options:this.options}));}/** * Disables the tracking for the plugin * * @returns {Base} The instance of the plugin */},{key:"disableTracking",value:function disableTracking(){this.trackingDisabled=true;return this;}/** * Enables the tracking for the plugin * * @returns {Base} The instance of the plugin */},{key:"enableTracking",value:function enableTracking(){this.trackingDisabled=false;return this;}}],[{key:"formatData",value:function formatData(eventName,rawData){// Check if a suitable conversion function is available if(typeof dataFormatHelpers[eventName]!=='function'){return rawData;}// Convert the raw data return dataFormatHelpers[eventName](rawData);}}]);}();export default Base;