Skip to content

Commit ac5c938

Browse files
committed
Add viewHistory and cartSkus
1 parent ec1f23b commit ac5c938

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

blocks/product-recommendations/product-recommendations.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,30 +90,23 @@ async function loadRecommendation(block, context) {
9090
// Only proceed if all required data is available
9191
if (!context.pageType
9292
|| (context.pageType === 'Product' && !context.currentSku)
93-
|| (context.pageType === 'Category' && !context.category)) {
93+
|| (context.pageType === 'Category' && !context.category)
94+
|| (context.pageType === 'Cart' && !context.cartSkus)) {
9495
return;
9596
}
9697

9798
if (recommendationsPromise) {
9899
return;
99100
}
100101

101-
// Get user view history
102-
// TODO
103-
/* let productViews = window
104-
.adobeDataLayer.getState('productContext', [-10, 0], { flatten: false }) || [];
105-
if (!Array.isArray(productViews) && productViews) {
106-
productViews = [productViews];
102+
// Get product view history
103+
try {
104+
const viewHistory = window.sessionStorage.getItem('productViewHistory') || '[]';
105+
context.userViewHistory = JSON.parse(viewHistory).map((sku) => ({ sku }));
106+
} catch (e) {
107+
window.sessionStorage.removeItem('productViewHistory');
108+
console.error('Error parsing product view history', e);
107109
}
108-
context.userViewHistory = productViews
109-
.map(({ sku }) => ({ sku }))
110-
.reduce((acc, current) => {
111-
const x = acc.find((p) => p.sku === current.sku);
112-
if (!x) {
113-
return acc.concat([current]);
114-
}
115-
return acc;
116-
}, []); */
117110

118111
recommendationsPromise = performCatalogServiceQuery(recommendationsQuery, context);
119112
const { recommendations } = await recommendationsPromise;
@@ -140,9 +133,15 @@ export default async function decorate(block) {
140133
loadRecommendation(block, context);
141134
}
142135

136+
function handleCartChanges({ shoppingCartContext }) {
137+
context.cartSkus = shoppingCartContext.items.map(({ product }) => product.sku);
138+
loadRecommendation(block, context);
139+
}
140+
143141
window.adobeDataLayer.push((dl) => {
144142
dl.addEventListener('adobeDataLayer:change', handlePageTypeChanges, { path: 'pageContext' });
145143
dl.addEventListener('adobeDataLayer:change', handleProductChanges, { path: 'productContext' });
146144
dl.addEventListener('adobeDataLayer:change', handleCategoryChanges, { path: 'categoryContext' });
145+
dl.addEventListener('adobeDataLayer:change', handleCartChanges, { path: 'shoppingCartContext' });
147146
});
148147
}

scripts/commerce.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,18 @@ export async function getProduct(sku) {
335335
return productPromise;
336336
}
337337

338+
// TODO: Listen to placeOrder events to store purchase history
339+
// Store product view history in session storage
340+
window.adobeDataLayer.push((dl) => {
341+
dl.addEventListener('adobeDataLayer:change', (event) => {
342+
const viewHistory = JSON.parse(window.sessionStorage.getItem('productViewHistory') || '[]');
343+
if (!viewHistory.includes(event.productContext.sku)) {
344+
viewHistory.push(event.productContext.sku);
345+
window.sessionStorage.setItem('productViewHistory', JSON.stringify(viewHistory.slice(-10)));
346+
}
347+
}, { path: 'productContext' });
348+
});
349+
338350
/* PLP specific functionality */
339351

340352
// TODO

0 commit comments

Comments
 (0)