@shopgate/pwa-tracking
Version:
Tracking library for the Shopgate Connect PWA.
27 lines • 2.08 kB
JavaScript
import{createSelector}from'reselect';import{getCurrentPathname,makeGetRouteParam}from'@shopgate/engage/core/selectors';import{isDev}from'@shopgate/engage/core/helpers';import{getPageConfigById}from'@shopgate/engage/page/selectors';import{shopNumber}from'@shopgate/pwa-common/helpers/config';/**
* The tracking base URL.
* @type {string}
*/var baseUrl="https://rapid.shopgate.com".concat(isDev?'/php/shopgate':'',"/sg_app_resources");/**
* The mapping of page names to tracking page names.
* @type {Object}
*/var pageNameMap={'':'startpage',product:'productDetails',item:'productDetails',category:'productList'};/**
* Selects the current tracking URL.
* @param {Object} state The current state.
* @returns {string} The URL.
*/var getTrackingUrl=createSelector(getCurrentPathname,function(pathname){return"".concat(baseUrl,"/").concat(shopNumber).concat(pathname);});/**
* Extracts the name of the current path.
* @param {Object} state The current state.
* @returns {string} The name.
*/var getPageName=createSelector(getCurrentPathname,function(pathname){return pathname.split('?')[0].split('/')[1];});/**
* Selects the tracking page name based on the current page name.
* @param {Object} state The current state.
* @returns {string} The page name.
*/var getPageTrackingName=createSelector(getPageName,function(pageName){return pageNameMap[pageName]||pageName;});/**
* Creates a selector that retrieves a page config for the current route from the store.
* @param {string} name The name of the desired parameter.
* @returns {Function}
*/export var makeGetRoutePageConfig=function makeGetRoutePageConfig(){var getPageIdRouteParam=makeGetRouteParam('pageId');return createSelector(function(state){return state;},getPageIdRouteParam,function(state,pageId){return pageId?getPageConfigById(state,{pageId:pageId}):null;});};/**
* Selects the page information.
* @param {Object} state The current state.
* @returns {Object} The page information.
*/export default createSelector(getTrackingUrl,getPageTrackingName,function(link,name){return{link:link,name:name};});