@shopgate/pwa-tracking
Version:
Tracking library for the Shopgate Connect PWA.
19 lines • 2.66 kB
JavaScript
import'rxjs/add/operator/switchMap';import'rxjs/add/observable/of';import{Observable}from'rxjs/Observable';import{routeDidEnter$,pwaDidAppear$}from'@shopgate/engage/core/streams';import{hex2bin}from'@shopgate/engage/core/helpers';import{ROOT_CATEGORY_PATTERN,CATEGORY_PATTERN}from'@shopgate/engage/category/constants';import{getProductsResult}from'@shopgate/pwa-common-commerce/product/selectors/product';import{receivedRootCategories$}from'@shopgate/engage/category/streams';import{getRootCategories}from'@shopgate/engage/category/selectors';import{getIsAppWebViewVisible}from'@shopgate/engage/core/selectors';import{productsReceived$}from"./product";/**
* Emits when the root category was entered.
*/var rootCategoryDidEnter$=routeDidEnter$.filter(function(_ref){var action=_ref.action;return action.route.pattern===ROOT_CATEGORY_PATTERN;});/**
* Emits when a regular category was entered.
*/var categoryDidEnter$=routeDidEnter$.filter(function(_ref2){var action=_ref2.action;return action.route.pattern===CATEGORY_PATTERN;});/**
* Emits when the root category data has been received.
*/var rootCategoryLoaded$=rootCategoryDidEnter$.switchMap(function(){return receivedRootCategories$;});/**
* Emits when a root category's data is already available.
*/var rootCategoryPreloaded$=rootCategoryDidEnter$.filter(function(_ref3){var getState=_ref3.getState;var rootCategories=getRootCategories(getState());return rootCategories?!!rootCategories.length:false;});/**
* Emits when a category's data is available.
*/var categoryDataLoaded$=categoryDidEnter$// Do not track while PWA webview is in the background
.filter(function(_ref4){var getState=_ref4.getState;return getIsAppWebViewVisible(getState());}).switchMap(function(data){var action=data.action,getState=data.getState;var categoryId=action.route.params.categoryId;// Check if products for the current route are already available within Redux.
var productsLoaded=getProductsResult(getState(),{categoryId:hex2bin(categoryId)}).totalProductCount!==null;if(!productsLoaded){// Wait for incoming products if they are not available yet.
return productsReceived$.first();}return Observable.of(data);});/**
* Emits when the category route comes active again after a legacy page was active.
*/var categoryRouteReappeared$=pwaDidAppear$.filter(function(_ref5){var action=_ref5.action;return[CATEGORY_PATTERN,ROOT_CATEGORY_PATTERN].includes(action.route.pattern);});/**
* Emits when a category or root category is ready to be tracked,
* considering loaded or preloaded data.
*/export var categoryIsReady$=categoryDataLoaded$.merge(rootCategoryLoaded$,rootCategoryPreloaded$,categoryRouteReappeared$);