Race condition in data elements
Hi everyone!
Sometimes we have a situation where we reference the value of a different data element in a separate data element i.e. we define a variable in custom code using _satellite.getVar().
I was wondering if this introduces a race condition meaning that you end up with a data element that doesn't return a value at the point when it's requested even though there may be one as the functions that set the values of the data elements don't run in a specific order.
Use case is when we create fallbacks to account for any issues with data layer properties.
For example, we pass an order ID to the data layer on sale, but this value is also presented in an element in the DOM. So we have two data elements:
- For the text value of the order reference element (DOM attribute)
- For the value in the data layer OR DOM with precedence given to returning the value from the data layer (custom code)
var orderReferenceElement = _satellite.getVar("orderReferenceElement") // Data element with value from DOM
if(digitalData && digitalData.order && digitalData.order.id){ // Checking if data layer property is available
var dataLayerOrderReference = digitalData.order.id;
}
return (typeof dataLayerOrderReference !== "undefined") ? dataLayerOrderReference : orderReferenceElementThere shouldn't ever be a situation where the value is unspecified in Analytics as the page load rule fires on window loaded, meaning that if the data layer isn't ready, there's definitely the element in the DOM at that point.
Any help would be greatly appreciated.