Skip to content

Instantly share code, notes, and snippets.

@gabrielwalt
Last active April 23, 2021 09:25
Show Gist options
  • Save gabrielwalt/a8d9d869eaf1359996968e4b4457e8ef to your computer and use it in GitHub Desktop.
Save gabrielwalt/a8d9d869eaf1359996968e4b4457e8ef to your computer and use it in GitHub Desktop.
Generic Core Component ACDL event tracking
// The following function generates an event handler that returns the eventInfo.path, and allows to filter events by properties.
function getEventPathHandler(callback, filter) {
return function (eventData) {
if (eventData.hasOwnProperty("eventInfo") && eventData.eventInfo.hasOwnProperty("path")) {
const eventObject = this.getState(eventData.eventInfo.path);
if (eventObject != null) {
for (const property in filter) {
if (!eventObject.hasOwnProperty(property) || (filter[property] !== null && filter[property] !== eventObject[property])) {
return;
}
}
callback(eventObject, eventData);
}
}
}
}
// The following function can forward the data to Analytics, or perform further tracking logic.
function myEventHandler(data, event) {
console.log({
"data": data,
"event": event
});
}
// Now let's register our event handler that is decorated by the getEventPathHandler to listen to all ACDL events.
window.adobeDataLayer = window.adobeDataLayer || [];
window.adobeDataLayer.push(function(dl) {
dl.addEventListener("adobeDataLayer:event", getEventPathHandler(myEventHandler));
});
@gabrielwalt
Copy link
Author

This script registers to all events of the data layer and resolves the eventInfo.path that the events generated by the AEM Core Components use to reference the data already present in the data layer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment