From 0ea26244ea8056e6bfcde6985aed0d50425e0ca6 Mon Sep 17 00:00:00 2001 From: "Mark J. Becker" Date: Fri, 15 Mar 2024 13:58:20 +0100 Subject: [PATCH] Add storefront context directly to ACDL --- scripts/commerce-events.js | 62 -------------------------------------- scripts/delayed.js | 31 +++++++++++++++++-- 2 files changed, 29 insertions(+), 64 deletions(-) delete mode 100644 scripts/commerce-events.js diff --git a/scripts/commerce-events.js b/scripts/commerce-events.js deleted file mode 100644 index 7f685cf58e..0000000000 --- a/scripts/commerce-events.js +++ /dev/null @@ -1,62 +0,0 @@ -import { getConfigValue } from './configs.js'; - -async function getMagentoStorefrontEvents(callback) { - return new Promise((resolve) => { - if (window.magentoStorefrontEvents) { - if (callback) { - callback(window.magentoStorefrontEvents); - } - resolve(window.magentoStorefrontEvents); - return; - } - - const eventHandler = ({ data }) => { - if (data === 'magento-storefront-events-sdk') { - window.removeEventListener('message', eventHandler); - if (callback) { - callback(window.magentoStorefrontEvents); - } - resolve(window.magentoStorefrontEvents); - } - }; - window.addEventListener('message', eventHandler); - }); -} - -let eventsSDKInitialized = false; -export default async function init() { - if (eventsSDKInitialized) { - return; - } - eventsSDKInitialized = true; - - // Load events SDK and collector - import('./commerce-events-sdk.js'); - import('./commerce-events-collector.js'); - - const config = { - environmentId: await getConfigValue('commerce-environment-id'), - environment: await getConfigValue('commerce-environment'), - storeUrl: await getConfigValue('commerce-store-url'), - websiteId: await getConfigValue('commerce-website-id'), - websiteCode: await getConfigValue('commerce-website-code'), - storeId: await getConfigValue('commerce-store-id'), - storeCode: await getConfigValue('commerce-store-code'), - storeViewId: await getConfigValue('commerce-store-view-id'), - storeViewCode: await getConfigValue('commerce-store-view-code'), - websiteName: await getConfigValue('commerce-website-name'), - storeName: await getConfigValue('commerce-store-name'), - storeViewName: await getConfigValue('commerce-store-view-name'), - baseCurrencyCode: await getConfigValue('commerce-base-currency-code'), - storeViewCurrencyCode: await getConfigValue('commerce-base-currency-code'), - storefrontTemplate: 'Franklin', - }; - - getMagentoStorefrontEvents((mse) => { - mse.context.setStorefrontInstance(config); - mse.context.setEventForwarding({ - commerce: true, - aep: false, - }); - }); -} diff --git a/scripts/delayed.js b/scripts/delayed.js index 4604c09c4c..1f8687eb3c 100644 --- a/scripts/delayed.js +++ b/scripts/delayed.js @@ -1,9 +1,36 @@ // eslint-disable-next-line import/no-cycle import { sampleRUM } from './aem.js'; -import initCommerceEvents from './commerce-events.js'; +import { getConfigValue } from './configs.js'; // Core Web Vitals RUM collection sampleRUM('cwv'); // add more delayed functionality here -initCommerceEvents(); + +// Load Commerce events SDK and collector +const config = { + environmentId: await getConfigValue('commerce-environment-id'), + environment: await getConfigValue('commerce-environment'), + storeUrl: await getConfigValue('commerce-store-url'), + websiteId: await getConfigValue('commerce-website-id'), + websiteCode: await getConfigValue('commerce-website-code'), + storeId: await getConfigValue('commerce-store-id'), + storeCode: await getConfigValue('commerce-store-code'), + storeViewId: await getConfigValue('commerce-store-view-id'), + storeViewCode: await getConfigValue('commerce-store-view-code'), + websiteName: await getConfigValue('commerce-website-name'), + storeName: await getConfigValue('commerce-store-name'), + storeViewName: await getConfigValue('commerce-store-view-name'), + baseCurrencyCode: await getConfigValue('commerce-base-currency-code'), + storeViewCurrencyCode: await getConfigValue('commerce-base-currency-code'), + storefrontTemplate: 'Franklin', +}; + +window.adobeDataLayer.push( + { storefrontInstanceContext: config }, + { eventForwardingContext: { commerce: true, aep: false } }, +); + +// Load events SDK and collector +import('./commerce-events-sdk.js'); +import('./commerce-events-collector.js');