From c1b1434049ab2c84b7513f75b1c0f34d67cc593b Mon Sep 17 00:00:00 2001 From: "Mark J. Becker" Date: Wed, 9 Oct 2024 13:18:05 +0200 Subject: [PATCH] Fixes for Prex events --- .../product-recommendations.js | 30 ++++++++++++------- scripts/scripts.js | 8 ++--- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/blocks/product-recommendations/product-recommendations.js b/blocks/product-recommendations/product-recommendations.js index 07f1303d5f..ec0eb5ad4d 100644 --- a/blocks/product-recommendations/product-recommendations.js +++ b/blocks/product-recommendations/product-recommendations.js @@ -117,7 +117,7 @@ function renderItems(block, results) { inViewObserver.disconnect(); } }); - }); + }, { threshold: 0.5 }); inViewObserver.observe(block); } @@ -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`) || '[]'; @@ -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); + 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); diff --git a/scripts/scripts.js b/scripts/scripts.js index 34facd47a7..843a49e5ed 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -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) {