UNPKG

@shopgate/tracking-core

Version:

Tracking core library for the Shopgate Connect PWA.

89 lines • 7.77 kB
function _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;}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{SGAction}from"../helpers/helper";/** * Handler for the communication between the tracking plugins and the app. */var AppHandler=/*#__PURE__*/function(){function AppHandler(){_classCallCheck(this,AppHandler);}return _createClass(AppHandler,[{key:"viewContent",value:/** * Log a pageview * * @param {UnifiedPageview} data Tracking data for this event * @param {UnifiedRestrictions} [restrictions] Info about the restrictions * @returns {AppHandler} Instance of SgTrackingAppHandler */function viewContent(data,restrictions){SGAction.analyticsLogPageview(AppHandler.prepareTrackingData(data,restrictions));return this;}/** * AnalyticsLogEvent * @param {UnifiedPageview} data Tracking data for this event * @param {UnifiedRestrictions} [restrictions] Info about the restrictions * @returns {AppHandler} Instance of SgTrackingAppHandler */},{key:"logEvent",value:function logEvent(data,restrictions){SGAction.analyticsLogEvent(AppHandler.prepareTrackingData(data,restrictions));return this;}/** * Log a purchase * * @param {UnifiedPurchase} data Tracking data for this event * @param {UnifiedRestrictions} [restrictions] Info about the restrictions * @returns {AppHandler} Instance of SgTrackingAppHandler */},{key:"purchase",value:function purchase(data,restrictions){SGAction.analyticsLogPurchase(AppHandler.prepareTrackingData(data,restrictions));return this;}/** * Log when a product was added to the cart * * @param {UnifiedAddToCart} data Tracking data for this event * @param {UnifiedRestrictions} [restrictions] Info about the restrictions * @returns {AppHandler} Instance of SgTrackingAppHandler */},{key:"addToCart",value:function addToCart(data,restrictions){SGAction.analyticsLogAddToCart(AppHandler.prepareTrackingData(data,restrictions));return this;}/** * Log when a payment info was added * * @param {UnifiedPaymentInfo} data Tracking data for this event * @param {UnifiedRestrictions} [restrictions] Info about the restrictions * @returns {AppHandler} Instance of SgTrackingAppHandler */},{key:"addedPaymentInfo",value:function addedPaymentInfo(data,restrictions){SGAction.analyticsLogAddedPaymentInfo(AppHandler.prepareTrackingData(data,restrictions));return this;}/** * Log when a payment info was selected * * @param {UnifiedPaymentInfo} data Tracking data for this event * @param {UnifiedRestrictions} [restrictions] Info about the restrictions * @returns {AppHandler} Instance of SgTrackingAppHandler */},{key:"selectedPaymentInfo",value:function selectedPaymentInfo(data,restrictions){SGAction.analyticsLogAddedPaymentInfo(AppHandler.prepareTrackingData(data,restrictions));return this;}/** * Log when a checkout is initiated * * @param {UnifiedInitiatedCheckout} data Tracking data for this event * @param {UnifiedRestrictions} [restrictions] Info about the restrictions * @returns {AppHandler} Instance of SgTrackingAppHandler */},{key:"initiatedCheckout",value:function initiatedCheckout(data,restrictions){SGAction.analyticsLogInitiatedCheckout(AppHandler.prepareTrackingData(data,restrictions));return this;}/** * Log if a registration was completed * * @param {UnifiedCompletedRegistration} data Tracking data for this event * @param {UnifiedRestrictions} [restrictions] Info about the restrictions * @returns {AppHandler} Instance of SgTrackingAppHandler */},{key:"completedRegistration",value:function completedRegistration(data,restrictions){SGAction.analyticsLogCompletedRegistration(AppHandler.prepareTrackingData(data,restrictions));return this;}/** * Log when a product was added to the wishlist * * @param {UnifiedAddToWishlist} data Tracking data for this event * @param {UnifiedRestrictions} [restrictions] Info about the restrictions * @returns {AppHandler} Instance of SgTrackingAppHandler */},{key:"addToWishlist",value:function addToWishlist(data,restrictions){SGAction.analyticsLogAddToWishlist(AppHandler.prepareTrackingData(data,restrictions));return this;}/** * Log when a search happened * * @param {UnifiedSearched} data Tracking data for this event * @param {UnifiedRestrictions} [restrictions] Info about the restrictions * @returns {AppHandler} Instance of SgTrackingAppHandler */},{key:"search",value:function search(data,restrictions){SGAction.analyticsLogSearch(AppHandler.prepareTrackingData(data,restrictions));return this;}/** * Log when Deeplink and etc opened * @param {Object} data Tracking data for this event * @param {UnifiedRestrictions} [restrictions] Info about the restrictions * @returns {AppHandler} Instance of SgTrackingAppHandler */},{key:"setCampaignWithUrl",value:function setCampaignWithUrl(data,restrictions){SGAction.analyticsSetCampaignWithUrl(AppHandler.prepareTrackingData(data,restrictions));return this;}/** * Log an itemview * * @param {UnifiedItemView} data Tracking data for this event * @param {UnifiedRestrictions} [restrictions] Info about the restrictions * @returns {AppHandler} Instance of SgTrackingAppHandler */},{key:"itemView",value:function itemView(data,restrictions){SGAction.analyticsLogItemView(AppHandler.prepareTrackingData(data,restrictions));return this;}/** * Log a login * * @param {UnifiedLogin} data Tracking data for this event * @param {UnifiedRestrictions} [restrictions] Info about the restrictions * @returns {AppHandler} Instance of SgTrackingAppHandler */},{key:"login",value:function login(data,restrictions){SGAction.analyticsLogLogin(AppHandler.prepareTrackingData(data,restrictions));return this;}/** * Add the properties restriction, blacklist and trackers to the tracking event data if needed * * @param {Object} data The tracking event data * @param {UnifiedRestrictions} [restrictions] Info about the restrictions * @returns {Object} Tracking data with restrictions * @private */}],[{key:"prepareTrackingData",value:function prepareTrackingData(data,restrictions){// Clone the data to avoid issues with references. var clonedData=_extends({},data);if(restrictions&&restrictions.trackers){var trackerParamName='trackers';// There is a Bug in the iOS app. Lib Version <= 9.0 expects "tracker" as the param name. if(window.LibShopgateClient&&window.LibShopgateClient.isIos()&&window.LibShopgateVersion&&window.LibShopgateVersion.isLibVersionAtMost(9)){trackerParamName='tracker';}// Add the restrictions to the tracking event data. clonedData.restrictions=_defineProperty({blacklist:restrictions.blacklist},trackerParamName,restrictions.trackers);}return clonedData;}}]);}();export default new AppHandler();