adobe / adobe/adobe-client-data-layer
Pass the full state on specific events
- Dominant language
- JavaScript
- Stars
- 122
- Forks
- 28
- PR merge metrics
- No merged PRs in 30d
Description
As discussed with Benedikt Wedenik, for events that depend on data in the state that isn't provided by the event payload, when the event listener is registered late, it is impossible to recover how exactly the state was at the moment of the event.
In following example, see how there's no easy way for the `"test"` event listener to know that at the moment of the event `x` was equal to `1`:
```javascript
window.adobeDataLayer = window.adobeDataLayer || [];
adobeDataLayer.push({
x: 1
});
adobeDataLayer.push({
event: "test",
y: 1
});
adobeDataLayer.push({
x: 2
});
adobeDataLayer.push(function () {
this.addEventListener("test", function(event) {
console.log(this.getState());
});
});
// Console output will be: {x: 2, y: 1}
```
If the event listener could have been registered before the other `push`, then `this.getState()` would have returned `{x: 1, y: 1}`, but this is often not possible as the event listeners get registered asynchronously, via Adobe Launch for example.
It would be nice if events that get registered late had a chance to access the full state as it was at the moment of the event, like by passing it as a parameter to the event:
```javascript
adobeDataLayer.push(function () {
this.addEventListener("test", function(event, state) {
console.log(state);
});
});
// Console output would then be: {x: 1, y: 1}
```
Or, if that's too much of a performance concern, then we should have a way to optionally request the full event for specific events:
```javascript
adobeDataLayer.push({
x: 1
});
adobeDataLayer.push({
event: "test",
y: 1,
"adobeDataLayer:fullState": true
});
adobeDataLayer.push({
x: 2
});
adobeDataLayer.push(function () {
this.addEventListener("test", function(event, state) {
console.log(state);
});
});
// Console output will be: {x: 1, y: 1}
```
Contributor guide
Assessment
This issue has not been assessed yet.