Skip to content

Instantly share code, notes, and snippets.

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

If the events that push x and yare not the same, but you still want to consolidate the state between those two, then you can use the generic adobeDataLayer:event event, and filter inside the callbacks to the desired events.

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