Instrument analytics events without drop-ins
If your storefront doesn’t use the AEM Commerce drop-ins — or replaces one with a custom block — you lose the automatic Adobe Client Data Layer (ACDL) instrumentation that the drop-ins provide. This tutorial shows you how to publish some Commerce events yourself using a small helper script, so that analytics data is collected to power Commerce tooling.
What You’ll Build
Section titled “What You’ll Build”By the end of this tutorial, you’ll have a reusable scripts/acdlHelper.js file with the shared primitives for reading and writing to the ACDL. Each focused example below then defines its own event-specific context and publish functions on top of those primitives:
Prerequisites
Section titled “Prerequisites”Before starting this tutorial, make sure you have:
- A working Commerce storefront on Edge Delivery Services
- Basic understanding of JavaScript and asynchronous operations
- The following packages at version
1.17.0or later inpackage.json:
"@adobe/magento-storefront-event-collector": "^1.17.0","@adobe/magento-storefront-events-sdk": "^1.17.0"These packages are included by default in the AEM Commerce boilerplate, but the versions in an existing project may be outdated. Confirm the versions in package.json, then run npm install. If the packages are missing entirely, add them manually to package.json before running npm install.
Add the ACDL helper script
Section titled “Add the ACDL helper script”Create scripts/acdlHelper.js in your project, at the root of your AEM boilerplate:
your-project/└── scripts/ └── acdlHelper.js (create this file)This file centralizes access to the ACDL - getting the data layer, setting a context, and pushing an event - so each event-specific example can build on shared logic instead of duplicating it. It doesn’t contain any event-specific code: each focused example defines its own context and publish functions on top of these primitives, and imports only what it needs from here.
export function getAdobeDataLayer() { window.adobeDataLayer = window.adobeDataLayer || []; return window.adobeDataLayer;}
/** * Sets a context in the Adobe Client Data Layer (ACDL). * Logic based on: https://github.com/adobe/commerce-events/blob/main/packages/storefront-events-sdk/src/Base.ts#L6 */export function setContext(name, data) { const adobeDataLayer = getAdobeDataLayer();
// Clear existing context adobeDataLayer.push({ [name]: null, });
// Set new context adobeDataLayer.push({ [name]: data, });}
/** * Pushes an event to the Adobe Client Data Layer (ACDL). * Logic based on: https://github.com/adobe/commerce-events/blob/main/packages/storefront-events-sdk/src/Base.ts#L34 */export function pushEvent(event, additionalContext) { const adobeDataLayer = getAdobeDataLayer();
adobeDataLayer.push((acdl) => { const state = acdl.getState ? acdl.getState() : {};
acdl.push({ event, eventInfo: { ...state, ...additionalContext, }, }); });}Instrument specific events
Section titled “Instrument specific events”The helper script exposes the shared primitives (getAdobeDataLayer, setContext, pushEvent) used to build event-specific publish functions. Each of the following examples is a short, independent tutorial that defines its own context and publish functions on top of these primitives — instrument only the events your custom implementation actually needs, in any order:
- Instrument analytics product page view event - publish a
product-page-viewevent with a complete product context - Instrument analytics add to cart event - publish an
add-to-cartevent with the full updated cart
Implementation Checklist
Section titled “Implementation Checklist”When adding ACDL tracking to a new page or interaction:
- Copy
scripts/acdlHelper.jsinto your project’sscripts/directory. - Ensure
storefrontInstanceContextandpageContextare set before any publish call. - On product page load, call the
publishProductPageViewEventfunction from Instrument analytics product page view event with the resolved product data. - On successful add-to-cart, call the
publishAddToCartEventfunction from Instrument analytics add to cart event with the full updated cart, not just the item that was added. - On cart update (quantity change, removal), call
setShoppingCartContextdirectly to keep the context in sync without firing an event. - On variant or option selection change, call
setProductContextso the context always reflects the currently selected variant.
Troubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”-
Events push successfully but
eventInfois missing storefront or page dataConfirm
storefrontInstanceContextandpageContextare pushed to the ACDL before anypublish*call runs. See Scripts in the Analytics events reference for when these are normally seeded. -
setShoppingCartContextorsetProductContextthrows a missing field errorCheck the JSDoc on
setShoppingCartContextin Instrument analytics add to cart event, or onsetProductContextin Instrument analytics product page view event, for the required fields onitems,product, andpricing. Each function validates its input and throws before pushing an incomplete context. -
Events aren’t appearing in Adobe Experience Platform
Verify AEP forwarding is configured — see Adobe Experience Platform. Events still push to
window.adobeDataLayereven if AEP forwarding isn’t enabled, so check the browser console first before assuming the helper script is broken.
Getting Help
Section titled “Getting Help”If you encounter issues not covered here:
- Review the Analytics events reference for the full list of events and the contexts each one sets
- Review Analytics instrumentation for ACDL validation and debugging steps
- Consult the Commerce Events GitHub repository for the underlying SDK source
Summary
Section titled “Summary”In this tutorial, you learned how to:
- Add a reusable
scripts/acdlHelper.jsfile with the shared primitives for reading and writing to the ACDL - Identify where to go next to instrument specific analytics events
Continue with Instrument analytics product page view event or Instrument analytics add to cart event to see the helper script in action.