@shopgate/tracking-core
Version:
Tracking core library for the Shopgate Connect PWA.
34 lines • 4.47 kB
JavaScript
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;}/* global _gaq */ /**
* Tracking plugin for google analytics classic accounts
*/var GaClassic=/*#__PURE__*/function(){/**
* Constructor
* @param {Object} options Common Tracking Configuration
* @param {boolean} [options.overrideUnified] If true -> overrides our unified tracking system
* @param {boolean} [options.useNativeSdk] If true -> send data via our unified tracking system
* to the native sdk
* @param {Object} [options.config] Configuration for google analytics classic tracking
*/function GaClassic(options){_classCallCheck(this,GaClassic);// eslint-disable-next-line no-underscore-dangle
window._gaq=window._gaq||[];this.merchantAccounts=options.config.merchant;/**
* List of functions that can be used to filter data
* @type {Object}
*/this.filters={addTrans:function addTrans(data){return[data.orderNumber,data.shopName,data.amountCompleteFloat,data.amountTaxCompleteFloat,data.amountShippingFloat,data.city,'',// State
data.countryId];},addItem:function addItem(data){return[data.orderNumber,data.productNumber,data.name,'',data.unitAmount,data.quantity];},trackEvent:function trackEvent(data){return[data.eventCategory,data.eventAction,data.eventLabel,data.eventValue,data.nonInteraction];}};this.initPlugin();}/**
* Initiate and setup the SDK
*/return _createClass(GaClassic,[{key:"initPlugin",value:function initPlugin(){// Load the SDK
/* eslint-disable eslint-comments/no-unlimited-disable */ /* eslint-disable */if(typeof window._gaq==='undefined'||typeof window._gaq._getAsyncTracker==='undefined'||global&&global.it){(function(){var gaJs=document.createElement('script');gaJs.type='text/javascript';gaJs.async=true;gaJs.src='https://ssl.google-analytics.com/ga.js';var s=document.getElementsByTagName('script')[0];s.parentNode.insertBefore(gaJs,s);})();}/* eslint-enable */ // Setup merchant accounts
this.merchantAccounts.forEach(function(account,index){var prefix="".concat(GaClassic.buildMerchantPrefix(index?account.id:''),".");_gaq.push(["".concat(prefix,"_setAccount"),account.id],["".concat(prefix,"_setAllowLinker"),true]);});// Set global options
_gaq.push(['_gat._anonymizeIp',true],['_gat._forceSSL',true]);}/**
* Helper to generate the prefix for an account
*
* @param {string} id Id of the account
* @returns {string} Prefix for the account
*/},{key:"send",value:/**
* Function that sends the given command with the payload to the GA SDK
* @param {string} command Name of the command
* @param {Object|Array|string|Function} payload Data for the command,
* or a function that returns data
* @param {Object} [scope={}] Info if the event is for merchant and/or shopgate account
*/function send(command,payload){var _this=this;var scope=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};var defaults={merchant:true,shopgate:true};var mergedScope=_extends({},defaults,{},scope);/**
* Since we migrates all our classic account to universal,
* Only the merchant can have classic accounts
*/if(mergedScope.merchant){this.merchantAccounts.forEach(function(account,index){var data=typeof payload==='function'?payload(account):payload;data=typeof _this.filters[command]!=='undefined'?_this.filters[command](data):data;var cmd="".concat(GaClassic.buildMerchantPrefix(index?account.id:''),"._").concat(command);if(Array.isArray(data)){_gaq.push([cmd].concat(data));}else{_gaq.push([cmd,data]);}});}}}],[{key:"buildMerchantPrefix",value:function buildMerchantPrefix(id){return"merchant_".concat(id);}}]);}();export default GaClassic;