Last active
April 23, 2021 09:25
-
-
Save gabrielwalt/a8d9d869eaf1359996968e4b4457e8ef to your computer and use it in GitHub Desktop.
Generic Core Component ACDL event tracking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.