Skip to content

Instantly share code, notes, and snippets.

@gabrielwalt
Last active April 16, 2021 13:46
Show Gist options
  • Save gabrielwalt/b337afd08c58ce40822a2fd80c6e73f0 to your computer and use it in GitHub Desktop.
Save gabrielwalt/b337afd08c58ce40822a2fd80c6e73f0 to your computer and use it in GitHub Desktop.
ACDL eventing with event parameter
// In a browswer window with URL about:blank
// Open the JS console and paste this code
window.adobeDataLayer = [];
adobeDataLayer.push({
x: 1
});
adobeDataLayer.push({
event: "test",
y: "a"
});
adobeDataLayer.push({
x: 2
});
adobeDataLayer.push({
event: "test",
y: "b"
});
adobeDataLayer.push(function (dl) {
dl.addEventListener("test", function(e) {
console.log(e);
});
});
var scriptElement = document.createElement('script');
scriptElement.src = "https://unpkg.com/@adobe/[email protected]/dist/adobe-client-data-layer.js";
document.body.append(scriptElement);
// Console output will be:
// {event: "test", y: "a"}
// {event: "test", y: "b"}
@gabrielwalt
Copy link
Author

gabrielwalt commented Apr 16, 2021

A more robust pattern than to use getState is to use the event payload passed to the event listener function, this will always return the data submitted at the moment of the event, even if the event listener has been registered late.

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