recsTracking.min.js 11 KB

1
  1. define(["pubsub","ccLogger","ccRestClient","ccConstants","pageViewTracker","js/recsRequest","js/gdprConsent","pageLayout/cart","pageLayout/search","storageApi","swmRestClient","promise"],function(e,t,n,r,i,s,o,u,a,f,l){"use strict";function S(e){var t=e.items(),n,r=t.length,i=[];while(r--)n=t[r],i.push(n.productId);return i}function x(e){var t=e.items(),n,r=t.length,i=[],s=e.total();while(r--)n=t[r],i.push({productId:n.productId,quantity:n.quantity(),price:n.itemTotal()});return[i,s]}function T(e){var e=e||k(),t=u.singleInstance;t.items().length>0?e.addResource("cart",{cart:{productIds:S(t),pricelistGroupId:t.cartPriceListGroupId(),currencyCode:t.currencyCode(),totalPrice:t.total()}}):e.addResource("cart",{cart:{productIds:S(t)}})}function N(e){var t={},n=e.pageId,r=e.contextId;return n=="product"?t.productId=r:n=="home"||n=="search"||n=="cart",t}function C(){return w.site().tenantId}function k(){return b||(b=new s.RESTRequest(M)),b}function L(e,t){return e?(t&&(o.hasP13nCookieConsent(w.site())?(B(e),c.setItem(e,t)):(c.removeItem(e),c.setSessionItem(e,t)),y[e]=t),y[e]||c.getSessionItem(e)||c.getItem(e)):null}function A(e){return L(h,e)}function O(e){return L(p,e)}function M(e){var t=w.recsHostPortPath+"/"+e+"/3.0/json/"+C()+(A()?"/"+A():"")+(O()?"?sessionId="+O():"");return t}function _(e){e&&(e.visitorId&&A(e.visitorId),e.sessionId&&O(e.sessionId))}function D(t){var n={data:t,visitorId:A(),sessionId:O()};$.Topic(e.topicNames.RECS_HAVE_RECS).publish(n)}function P(e){e=e.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");var t=new RegExp("[\\?&]"+e+"=([^&#]*)"),n=t.exec(location.search);return n===null?"":decodeURIComponent(n[1].replace(/\+/g," "))}function H(e,t){return e=e||"",String.prototype.startsWith?e.startsWith(t):t.length>e.length?!1:e.substring(0,t.length)===t}function B(e){c.removeSessionItem(e),c.hasCookiesSupport()&&c.readFromCookies(e)!==null&&c.saveToCookies(e,"",-1)}function j(e){c.sessionStorage&&c.sessionStorage.removeItem(e)}function F(e,t){var n={},r={Blended:!0,"Top Sellers":"topSellers","Browsed Together":"topCobrowses","Purchased Together":"topCobuys","Most Recently Viewed":"mostRecentlyViewed","In Brand":"inBrand","In Collection":"inCategory"};return r[e]?(n.rule=[],e=="Blended"?t=="In Brand"?n.rule=["inBrand"]:t=="In Collection"&&(n.rule=["inCategory"]):e=="Top Sellers"?t=="In Brand"?n.rule=["topSellersInBrand"]:t=="In Collection"?n.rule=["topSellersInCategory"]:n.rule=["topSellers"]:(n.rule=[r[e]],r[t]&&n.rule.push(r[t])),n):(n.strategyId=JSON.parse(e).id,n)}function I(e){if(!w.recsHostPortPath)return;if(!C())return;var t=k();w.localWishlists&&Object.getOwnPropertyNames(w.localWishlists).length!=0&&t.addResource("wishlists",{wishlists:w.localWishlists}),w.slots!=null&&w.slots!=undefined&&Object.getOwnPropertyNames(w.slots).length!=0&&t.addResource("recommendations",{slots:w.slots}),t.addCallback(D,"slots");var n={url:document.location.href};E&&(n.referrer=E,E=undefined);if(e.pageId==="product"){n.productId=e.contextId;var r=P("recset");r&&t.addResource("clickThru",{click:{recSetId:r,productId:e.contextId}})}w.categoryPath&&(n.category=w.categoryPath),w.pageTitle&&(n.pageTitle=w.pageTitle),w.catalogId&&(n.storeId=w.catalogId,n.excludeDefaultStore=!0);var i=w.site().selectedPriceListGroup();i&&(i.repositoryId&&(n.pricelistGroupId=i.repositoryId),i.currency&&i.currency.currencyCode&&(n.currencyCode=i.currency.currencyCode)),w.searchText&&(n.searchText=w.searchText,w.searchText=""),w.pageContext()&&w.pageContext().layout&&w.pageContext().layout.id&&(n.layoutId=w.pageContext().layout.id),t.addResource("view",{view:n});var s=c.getSessionItem(d);s&&(t.addResource("checkout",{checkout:JSON.parse(s)}),B(d)),q(t)}function q(e){var e=e||k();if(!w.recsHostPortPath||!C())return;var t=R();t&&e.addParam({customerId:t}),o.hasP13nCookieConsent(w.site())||e.addParam({gdpr:!0}),nt(w)&&e.addParam({accountId:nt(w)}),w.locale()&&e.addParam({locale:w.locale()});var s=n.getStoredValue(r.LOCAL_STORAGE_SITE_ID);s&&e.addParam({ccSiteId:s}),i.getVisitorId()&&e.addParam({ccVisitorId:i.getVisitorId()}),e.addParam({preview:w.isPreview()}),e.addCallback(_,"tracking"),e.send(),b=undefined}function R(){if(n.profileId)return n.profileId;if(c.readFromCookies(r.SOFT_LOGIN)){var e=c.readFromCookies(r.SOFT_LOGIN).split(".");if(e.length!=3)return;var t={};try{t=JSON.parse(atob(decodeURIComponent(e[1])))}catch(i){return}if(!t||!t.sub||!t.iss||!t.exp||!t.iat)return;return t.sub}}function U(e){var t=e.items,n=[];if(t&&t.length>0){var r=0;for(r=0;r<t.length;r++)n.push(t[r].productProductId)}return n}function z(){var e=function(e){var t,n=[],r=e.items,i=0,s={};for(i=0;i<r.length;i++)t=r[i].spaceId,n=U(r[i].products),n.length>0&&(s[t]=n);Object.getOwnPropertyNames(s).length!=0&&(w.localWishlists=s)},t=function(e){};l.init(w.site().tenantId,w.isPreview(),w.locale()),l.request("GET","/swm/rs/v1/sites/{siteid}/spaces?expand=products","",e,t,{})}function W(e,t){e&&t&&(w.localWishlists||(w.localWishlists={}),w.localWishlists.hasOwnProperty(e)||(w.localWishlists[e]=[]),w.localWishlists[e].push(t))}function X(e){e&&(w.searchText=e)}function V(e,t){if(typeof e=="object"&&!Array.isArray(e)&&e.hasOwnProperty("searchEventSummary")){var n=e.searchEventSummary;if(n){var r=k();r.addResource("search",{search:n}),q(r)}}}function J(e){X(a.getInstance().searchText)}function K(e){X(this.searchText)}function Q(e){V(this,e)}function G(e){var t=x(u.singleInstance),n=k();n.addResource("checkout",{checkout:{products:t[0],totalPrice:t[1],pricelistGroupId:u.singleInstance.cartPriceListGroupId(),currencyCode:u.singleInstance.currencyCode()}}),q(n)}function Y(e){var t=x(u.singleInstance);if(t[1]>0){var n={products:t[0],totalPrice:t[1],pricelistGroupId:u.singleInstance.cartPriceListGroupId(),currencyCode:u.singleInstance.currencyCode()};c.setSessionItem(d,JSON.stringify(n))}}function Z(e){return e.reduce(function(e,t){return e.indexOf(t)<0&&e.push(t),e},[])}function et(e){if(nt(w)&&tt()!=nt(w))return;var t=k();T(t),q(t)}function tt(){var e=n.getStoredValue(r.LOCAL_STORAGE_ORGANIZATION_ID);return typeof e=="string"?JSON.parse(e):e}function nt(e){if(e&&e.user()&&e.user().currentOrganization&&e.user().currentOrganization())return e.user().currentOrganization().repositoryId}function rt(e){var t=e.id,n=e.numRecs,r=e.strategy,i=e.restriction,s=e.version;if(w.slots==undefined||w.slots==null)w.slots={};w.slots[t]=F(r,i),w.slots[t].numRecs=n,s&&(w.slots[t].version=s),w.slots[t].dataItems=["repositoryid"],w.excludeList.length>0&&!s&&(w.slots[t].exclude=w.excludeList);if(!!e.customParams){var o=Object.keys(e.customParams);for(var u=0;u<o.length;u++)w.slots[t][o[u]]=e.customParams[o[u]]}w.extraData||(w.extraData={}),w.extraData[t]||(w.extraData[t]={}),e.collections&&(w.extraData[t].collectionIds=Z(e.collections.filter(function(e){return!!e})))}function it(e){z()}function st(e){var t;w&&w.product&&w.product()&&(t=w.product().id());var n=e.spaceId;n&&t&&W(n,t)}function ot(){$.Topic(e.topicNames.SEARCH_CREATE).subscribe(J),$.Topic(e.topicNames.SEARCH_TYPEAHEAD).subscribe(K),$.Topic(e.topicNames.SEARCH_RESULTS_UPDATED).subscribe(Q),$.Topic(e.topicNames.SEARCH_RESULTS_FOR_CATEGORY_UPDATED).subscribe(Q),$.Topic(e.topicNames.ORDER_COMPLETED).subscribe(G),$.Topic(e.topicNames.PAYULATAM_WEB_CHECKOUT).subscribe(Y),$.Topic(e.topicNames.CART_UPDATED).subscribe(et),$.Topic(e.topicNames.USER_LOGIN_SUCCESSFUL).subscribe(it),$.Topic(e.topicNames.SOCIAL_SPACE_ADD_SUCCESS).subscribe(st),$.Topic(e.topicNames.RECS_WANT_RECS).subscribe(rt)}function ut(e,t,r,i,s,o,u,a){return new Promise(function(f,l){n.request(e,t,f,l,r,i,s,o,u,a)})}function at(e,t){if(e.length==0)return Promise.resolve([]);var n={categoryIds:e,multipleCategoryPaths:!0};return t&&(n.fields=t),w.catalogId&&(n.catalogId=w.catalogId),ut(r.ENDPOINT_LIST_COLLECTIONS,n).catch(function(n){var i=e.map(function(e){var n={categoryIds:e,multipleCategoryPaths:!0};return t&&(n.fields=t),w.catalogId&&(n.catalogId=w.catalogId),ut(r.ENDPOINT_LIST_COLLECTIONS,n).then(function(e){return e[0]}).catch(function(e){})});return Promise.all(i).then(function(e){return e.filter(function(e){return!!e})})})}function ft(e){if(w.isPreview())return;var t={};t.catalogId=w.catalogId,t.id=e.contextId;if(e.pageId=="product")t.type="PRODUCT";else{if(e.pageId!="category")return;t.type="COLLECTION"}ut(r.ENDPOINT_AUDIENCE_ADD_BROWSING_EVENT,{pageView:t}).catch(function(e){})}function lt(e,t){var n=[];if(Array.isArray(e)&&e.length>0){n=e,t||(t=w.catalogId);if(t){var r=t+g;n=e.filter(function(e){return H(e,r)}).map(function(e){return ct(e,t)})}}return n}function ct(e,t){var n=e;if(e&&t){var r=t+g;H(e,r)&&(n=e.substring(r.length,e.length))}return n}function ht(){return w.recsHostPortPath?Promise.resolve(w.recsHostPortPath):ut(r.ENDPOINT_MERCHANT_GET_EXTERNALDATA,null,r.EXTERNALDATA_PRODUCTION_RECS).then(function(e){var t=e.serviceData.host,n=e.serviceData.port,r=e.serviceData.path,i=e.serviceData.protocol,s="";n&&n!==null&&n!==""&&n!=="0"&&(s=":"+n);var o="";i&&(o=""+i+":");if(t&&r)return w.recsHostPortPath=o+"//"+t+s+"/"+r,w.recsHostPortPath;throw Error("Failed to get Recs hostname.")})}function pt(e){w=e,ot(),w.user().loggedIn()&&z()}function dt(n){w.pageTitle=document.title,w.catalogId=w.user().catalogId(),ft(n),w.slots=undefined,w.categoryPath=undefined,w.extraData=undefined,w.excludeList=[];if(w&&w.product&&w.product()&&w.product().relatedProducts){var i=w.product().relatedProducts,s;for(s=0;s<i.length;s++)w.excludeList.push(i[s].id)}var o={};o.pageId=n.pageId,$.Topic(e.topicNames.RECS_WHO_WANT_RECS).publish(o),ht().then(function(e){if(!w.slots||!Object.keys(w.slots)||!w.isPreview())return;var t=!1,n=Object.keys(w.slots).map(function(e){if(!w.slots[e]||!w.slots[e].version)return;t=!0;if(w.slots[e].strategyId){var n=w.slots[e].strategyId;return ut(r.ENDPOINT_GET_STRATEGY,{},n)}});return t&&n.push(ut(r.ENDPOINT_LIST_GLOBAL_RULES,{})),Promise.all(n)}).then(function(e){if(!w.slots||!Object.keys(w.slots)||!w.isPreview())return;if(!e||!e.length||e.length==0)return;var t;e.length>Object.keys(w.slots).length&&e[e.length-1]&&e[e.length-1].items&&(t=e[e.length-1].items),Object.keys(w.slots).forEach(function(n,r){var i=e[r];i&&(w.slots[n].recommendationGroups=i.recommendationGroups,w.slots[n].strategyId=undefined),w.slots[n].version&&(w.slots[n].globalRules=t,w.slots[n].version=undefined)})},function(e){t.warn("[recsTracking] Error while getting strategy data: ",e)}).then(function(){var e=[];return n.pageId=="category"&&e.push(n.contextId),w.extraData&&(e=e.concat(Object.keys(w.extraData).reduce(function(e,t){return w.extraData[t].collectionIds&&(e=e.concat(w.extraData[t].collectionIds)),e},[]))),e=Z(e),at(e,[v,m])}).then(function(e){return e.reduce(function(e,t){return e[t.repositoryId]=t,e},{})},function(e){return t.warn("[recsTracking] Error while getting multiCatalogCategoryIdPaths: ",e),{}}).then(function(e){if(Object.keys(e).length>0){if(n.pageId=="category"&&e[n.contextId]){var t=e[n.contextId].multiCatalogCategoryIdPaths;if(Array.isArray(t)&&t.length>0){var r=lt(t,w.catalogId);r.length>0&&(w.categoryPath=r[0])}}if(w.extraData){var i=Object.keys(w.extraData);for(var s=0;s<i.length;s++)if(w.extraData[i[s]]&&w.extraData[i[s]].collectionIds){var o=w.extraData[i[s]].collectionIds.reduce(function(t,n){if(e[n]&&e[n].multiCatalogCategoryIdPaths){var r=lt(e[n].multiCatalogCategoryIdPaths,w.catalogId);t=t.concat(r)}return t},[]);w.slots&&w.slots[i[s]]&&(w.slots[i[s]].categories=o)}}}}).then(function(){I(n)}).catch(function(e){t.warn("[recsTracking] Error while retrieving recommendations: ",e)})}var c=f.getInstance(),h="occsRecVisitorId",p="occsRecSessionId",d="occsRecPayUCheckoutInfo",v="repositoryId",m="multiCatalogCategoryIdPaths",g=":",y={},b,w,E=document.referrer;return{onLoad:pt,beforeAppear:dt}})