Skip to content

Commit

Permalink
Fixes for Prex events
Browse files Browse the repository at this point in the history
  • Loading branch information
herzog31 committed Oct 9, 2024
1 parent a6b6505 commit c1b1434
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
30 changes: 19 additions & 11 deletions blocks/product-recommendations/product-recommendations.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function renderItems(block, results) {
inViewObserver.disconnect();
}
});
});
}, { threshold: 0.5 });
inViewObserver.observe(block);
}

Expand Down Expand Up @@ -152,8 +152,13 @@ async function loadRecommendation(block, context, visibility, filters) {
return;
}

if (!unitsPromise) {
const storeViewCode = await getConfigValue('commerce-store-view-code');
const storeViewCode = await getConfigValue('commerce-store-view-code');

if (unitsPromise) {
return;
}

unitsPromise = new Promise((resolve, reject) => {
// Get product view history
try {
const viewHistory = window.localStorage.getItem(`${storeViewCode}:productViewHistory`) || '[]';
Expand All @@ -176,16 +181,19 @@ async function loadRecommendation(block, context, visibility, filters) {
dl.push({ event: 'recs-api-request-sent', eventInfo: { ...dl.getState() } });
});

unitsPromise = performCatalogServiceQuery(recommendationsQuery, context);
const { recommendations } = await unitsPromise;

window.adobeDataLayer.push((dl) => {
dl.push({ recommendationsContext: { units: recommendations.results.map(mapUnit) } });
dl.push({ event: 'recs-api-response-received', eventInfo: { ...dl.getState() } });
performCatalogServiceQuery(recommendationsQuery, context).then(({ recommendations }) => {
window.adobeDataLayer.push((dl) => {
dl.push({ recommendationsContext: { units: recommendations.results.map(mapUnit) } });
dl.push({ event: 'recs-api-response-received', eventInfo: { ...dl.getState() } });
});
resolve(recommendations);
}).catch((error) => {
console.error('Error fetching recommendations', error);

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

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
reject(error);
});
}
});

let { results } = (await unitsPromise).recommendations;
let { results } = await unitsPromise;
results = results.filter((unit) => (filters.typeId ? unit.typeId === filters.typeId : true));

renderItems(block, results);
Expand Down
8 changes: 3 additions & 5 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,9 @@ async function loadEager(doc) {
totalQuantity: 0,
},
});
if (pageType !== 'Product') {
window.adobeDataLayer.push((dl) => {
dl.push({ event: 'page-view', eventInfo: { ...dl.getState() } });
});
}
window.adobeDataLayer.push((dl) => {
dl.push({ event: 'page-view', eventInfo: { ...dl.getState() } });
});

const main = doc.querySelector('main');
if (main) {
Expand Down

0 comments on commit c1b1434

Please sign in to comment.