Skip to content

Instantly share code, notes, and snippets.

@gabrielwalt
Last active April 19, 2021 09:32
Show Gist options
  • Save gabrielwalt/3219bf24da844f892aafec841f69462a to your computer and use it in GitHub Desktop.
Save gabrielwalt/3219bf24da844f892aafec841f69462a to your computer and use it in GitHub Desktop.
ACDL eventing with getState registered late
// 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() {
console.log(dl.getState());
});
});
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:
// {x: 2, y: "b"}
// {x: 2, y: "b"}
@gabrielwalt
Copy link
Author

See how getState in the event handler returns the state after everything has been executed, because it has been registered too late.

@gabrielwalt
Copy link
Author

gabrielwalt commented Apr 19, 2021

How the data layer works for event that are registered late cannot really be different, because the state has already been computed with all previous pushes.

You can disable the event handling for past events and only get future events (see [addEventListener](https://github.com/adobe/adobe-client-data-layer/wiki#addeventlistener) and options.scope parameter). But if you register for past events (which is the default), then knowing how the state was previously would the data layer to either require to memorize how the state was at very step (with a huge memory impact), or to recompute every past step when registering events that listens for past events (with a huge performance impact).

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