Skip to content

Commit

Permalink
only initialize event collection when not prerendering
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Rugh <[email protected]>
  • Loading branch information
sirugh committed Nov 7, 2024
1 parent a3b5808 commit c0a370f
Showing 1 changed file with 40 additions and 30 deletions.
70 changes: 40 additions & 30 deletions scripts/delayed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,49 @@ import { getConfigValue } from './configs.js';
import { getUserTokenCookie } from './initializers/index.js';
import { getConsent } from './scripts.js';

// Load Commerce events SDK and collector
if (getConsent('commerce-collection')) {
const config = {
environmentId: await getConfigValue('commerce-environment-id'),
environment: await getConfigValue('commerce-environment'),
storeUrl: await getConfigValue('commerce-store-url'),
websiteId: parseInt(await getConfigValue('commerce-website-id'), 10),
websiteCode: await getConfigValue('commerce-website-code'),
storeId: parseInt(await getConfigValue('commerce-store-id'), 10),
storeCode: await getConfigValue('commerce-store-code'),
storeViewId: parseInt(await getConfigValue('commerce-store-view-id'), 10),
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: 'EDS',
};
async function initAnalytics() {
// Load Commerce events SDK and collector
if (getConsent('commerce-collection')) {
const config = {
environmentId: await getConfigValue('commerce-environment-id'),
environment: await getConfigValue('commerce-environment'),
storeUrl: await getConfigValue('commerce-store-url'),
websiteId: parseInt(await getConfigValue('commerce-website-id'), 10),
websiteCode: await getConfigValue('commerce-website-code'),
storeId: parseInt(await getConfigValue('commerce-store-id'), 10),
storeCode: await getConfigValue('commerce-store-code'),
storeViewId: parseInt(await getConfigValue('commerce-store-view-id'), 10),
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: 'EDS',
};

window.adobeDataLayer.push(
{ storefrontInstanceContext: config },
{ eventForwardingContext: { commerce: true, aep: false } },
{
shopperContext: {
shopperId: getUserTokenCookie() ? 'logged-in' : 'guest',
window.adobeDataLayer.push(
{ storefrontInstanceContext: config },
{ eventForwardingContext: { commerce: true, aep: false } },
{
shopperContext: {
shopperId: getUserTokenCookie() ? 'logged-in' : 'guest',
},
},
},
);
);

// Load events SDK and collector
import('./commerce-events-sdk.js');
import('./commerce-events-collector.js');
// Load events SDK and collector
import('./commerce-events-sdk.js');
import('./commerce-events-collector.js');
}
}

if (document.prerendering) {
document.addEventListener('prerenderingchange', initAnalytics, {
once: true,
});
} else {
initAnalytics();
}

// add delayed functionality here

0 comments on commit c0a370f

Please sign in to comment.