@shopgate/tracking-core
Version:
Tracking core library for the Shopgate Connect PWA.
82 lines (76 loc) • 2.91 kB
JavaScript
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import { sendDataRequest } from "../../helpers/helper";
import BasePlugin from "../Base";
/**
* Tracking Plugin for our unified tracking system
*/
let Unified = /*#__PURE__*/function (_BasePlugin) {
/**
* @param {Object} [options] configuration
*/
function Unified(options = {}) {
var _this;
const trackerName = options.trackerName || 'unified';
// Invoke the parent constructor
_this = _BasePlugin.call(this, trackerName, options) || this;
_this.register.viewContent(data => data);
_this.register.setCampaignWithUrl(data => data);
_this.register.purchase(data => data);
_this.register.addToCart(data => data);
_this.register.initiatedCheckout(data => data);
_this.register.completedRegistration(data => data);
_this.register.addToWishlist(data => data);
_this.register.search(data => data);
_this.register.addedPaymentInfo(data => data);
_this.trackingCore.register.removeTracker(() => {
// Send request to server to remove the tracker
sendDataRequest('remove_unified_trackers');
}, {
trackerName: _this.trackerName,
options: _this.options
});
_this.trackingCore.register.addTracker(() => {
// Send request to server to add the tracker again
sendDataRequest('add_unified_trackers');
}, {
trackerName: _this.trackerName,
options: _this.options
});
return _this;
}
/**
* 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
*/
_inheritsLoose(Unified, _BasePlugin);
var _proto = Unified.prototype;
_proto.registerHelper = function registerHelper(eventName, callback) {
// Register the tracking event of the plugin at the core
return this.trackingCore.register[eventName]((data, scope, blacklist, state) => {
if (typeof this.appHandler[eventName] !== 'function') {
console.warn(`this.appHandler[${eventName}] is not a function`);
return;
}
// Convert the tracking data into the unified format
const unifiedData = BasePlugin.formatData(eventName, data);
// Invoke the event callback of the plugin to enable it to extend the data
const finalData = callback(unifiedData, data, scope, state);
// Send command to the app via the appHandler
this.appHandler[eventName](finalData, {
blacklist: true,
trackers: blacklist
});
}, {
trackerName: this.trackerName,
options: this.options
});
};
return Unified;
}(BasePlugin);
window.SgUnifiedTracking = Unified;
export default Unified;