Skip to content

Commit

Permalink
Add viewHistory and cartSkus
Browse files Browse the repository at this point in the history
  • Loading branch information
herzog31 committed Mar 6, 2024
1 parent ec1f23b commit ac5c938
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
31 changes: 15 additions & 16 deletions blocks/product-recommendations/product-recommendations.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,30 +90,23 @@ async function loadRecommendation(block, context) {
// Only proceed if all required data is available
if (!context.pageType
|| (context.pageType === 'Product' && !context.currentSku)
|| (context.pageType === 'Category' && !context.category)) {
|| (context.pageType === 'Category' && !context.category)
|| (context.pageType === 'Cart' && !context.cartSkus)) {
return;
}

if (recommendationsPromise) {
return;
}

// Get user view history
// TODO
/* let productViews = window
.adobeDataLayer.getState('productContext', [-10, 0], { flatten: false }) || [];
if (!Array.isArray(productViews) && productViews) {
productViews = [productViews];
// Get product view history
try {
const viewHistory = window.sessionStorage.getItem('productViewHistory') || '[]';
context.userViewHistory = JSON.parse(viewHistory).map((sku) => ({ sku }));
} catch (e) {
window.sessionStorage.removeItem('productViewHistory');
console.error('Error parsing product view history', e);

Check warning on line 108 in blocks/product-recommendations/product-recommendations.js

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
}
context.userViewHistory = productViews
.map(({ sku }) => ({ sku }))
.reduce((acc, current) => {
const x = acc.find((p) => p.sku === current.sku);
if (!x) {
return acc.concat([current]);
}
return acc;
}, []); */

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

function handleCartChanges({ shoppingCartContext }) {
context.cartSkus = shoppingCartContext.items.map(({ product }) => product.sku);
loadRecommendation(block, context);
}

window.adobeDataLayer.push((dl) => {
dl.addEventListener('adobeDataLayer:change', handlePageTypeChanges, { path: 'pageContext' });
dl.addEventListener('adobeDataLayer:change', handleProductChanges, { path: 'productContext' });
dl.addEventListener('adobeDataLayer:change', handleCategoryChanges, { path: 'categoryContext' });
dl.addEventListener('adobeDataLayer:change', handleCartChanges, { path: 'shoppingCartContext' });
});
}
12 changes: 12 additions & 0 deletions scripts/commerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,18 @@ export async function getProduct(sku) {
return productPromise;
}

// TODO: Listen to placeOrder events to store purchase history
// Store product view history in session storage
window.adobeDataLayer.push((dl) => {
dl.addEventListener('adobeDataLayer:change', (event) => {
const viewHistory = JSON.parse(window.sessionStorage.getItem('productViewHistory') || '[]');
if (!viewHistory.includes(event.productContext.sku)) {
viewHistory.push(event.productContext.sku);
window.sessionStorage.setItem('productViewHistory', JSON.stringify(viewHistory.slice(-10)));
}
}, { path: 'productContext' });
});

/* PLP specific functionality */

// TODO
Expand Down

0 comments on commit ac5c938

Please sign in to comment.