Last active
April 19, 2021 09:32
-
-
Save gabrielwalt/3219bf24da844f892aafec841f69462a to your computer and use it in GitHub Desktop.
ACDL eventing with getState registered late
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
// 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/[email protected]/dist/adobe-client-data-layer.js"; | |
document.body.append(scriptElement); | |
// Console output will be: | |
// {x: 2, y: "b"} | |
// {x: 2, y: "b"} |
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
See how
getState
in the event handler returns the state after everything has been executed, because it has been registered too late.