@shopgate/tracking-core
Version:
Tracking core library for the Shopgate Connect PWA.
194 lines (178 loc) • 7.04 kB
JavaScript
import { SGAction } from "../helpers/helper";
/**
* Handler for the communication between the tracking plugins and the app.
*/
let AppHandler = /*#__PURE__*/function () {
function AppHandler() {}
var _proto = AppHandler.prototype;
/**
* Log a pageview
*
* @param {UnifiedPageview} data Tracking data for this event
* @param {UnifiedRestrictions} [restrictions] Info about the restrictions
* @returns {AppHandler} Instance of SgTrackingAppHandler
*/
_proto.viewContent = 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
*/;
_proto.logEvent = 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
*/;
_proto.purchase = 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
*/;
_proto.addToCart = 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
*/;
_proto.addedPaymentInfo = 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
*/;
_proto.selectedPaymentInfo = 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
*/;
_proto.initiatedCheckout = 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
*/;
_proto.completedRegistration = 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
*/;
_proto.addToWishlist = 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
*/;
_proto.search = 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
*/;
_proto.setCampaignWithUrl = 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
*/;
_proto.itemView = 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
*/;
_proto.login = 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
*/;
AppHandler.prepareTrackingData = function prepareTrackingData(data, restrictions) {
// Clone the data to avoid issues with references.
const clonedData = {
...data
};
if (restrictions && restrictions.trackers) {
let 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 = {
blacklist: restrictions.blacklist,
[trackerParamName]: restrictions.trackers
};
}
return clonedData;
};
return AppHandler;
}();
export default new AppHandler();