UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

25 lines 2.5 kB
import get from'lodash/get';import{OS_ALL,TYPE_PHONE}from"../../constants/Device";import{TRACKING_TARGET_ALL,TRACKING_TARGET_APPS,TRACKING_TARGET_APPS_SMARTPHONE}from"../../constants/Tracking";/** * Extracts the action from the pathname. * @param {string} pathName The ULR pathname. * @returns {string} The action. */export var getPathAction=function getPathAction(pathName){return pathName.split('/')[1];};/** * Selector that extracts the pathname from the current path. * @param {Object} state The current state. * @returns {string} The action. */export var pathActionSelector=function pathActionSelector(state){return getPathAction(state.history.pathname);};/** * Checks if a given target is supported by the type * @param {string} target Tracking target * @param {Object} clientInformation Information about the current client * @returns {boolean} */export function isTargetSupported(target,clientInformation){var type=clientInformation.type;// Always use 'all' and 'apps' var validTargets=[TRACKING_TARGET_ALL,TRACKING_TARGET_APPS];// Add more valid targets depending on the platform/device if(type===TYPE_PHONE){validTargets.push(TRACKING_TARGET_APPS_SMARTPHONE);}// Check if the given target matches any valid target return validTargets.indexOf(target)!==-1;}/** * Collects tracking configurations from the sgxs configuration, which match to the current client. * @param {Object} sgxsConfig The SGXS configuration * @param {Object} clientInformation Information about the current client * @return {Array} */export var getRelevantConfigs=function getRelevantConfigs(){var sgxsConfig=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var clientInformation=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var _sgxsConfig$stage=sgxsConfig.stage,sgxsStage=_sgxsConfig$stage===void 0?'':_sgxsConfig$stage,_sgxsConfig$trackers=sgxsConfig.trackers,trackers=_sgxsConfig$trackers===void 0?[]:_sgxsConfig$trackers;return trackers.filter(function(_ref){var stage=_ref.stage,shopgateAccount=_ref.shopgateAccount,os=_ref.os,target=_ref.target;/** * Only Shopgate tracking configurations contain a stage property. In case of the merchant * configurations we only get entries which match the stage, on which the client is running. */var validStage=!shopgateAccount||stage===sgxsStage;var validOs=!os||os===get(clientInformation,'os')||os===OS_ALL;var validTarget=isTargetSupported(target,clientInformation);return validStage&&validOs&&validTarget;});};