I set the data element as JS Object and leave it as a place holder. In my event rule, I tried to dynamically get the value and then use _satellite.setVar function to set the value of my data element. But it didn't work for me. Then I tested in console,
It seems that _satellite.setVar function didn't work for me. Anyone know why?
The short answer:
Data Elements defined within the interface have a different namespace/scope than Data Elements defined on-the-fly with _satellite.setVar(), so they are evaluated/referenced from separate sources.
When a Data Element is defined within the interfaced, it is stored within _satellite.dataElements
When a Data Element is defined on-the-fly using _satellite.setVar(), it is stored within _satellite.data.customVars
When you use _satellite.getVar() to get the value of a Data Element, DTM first looks in _satellite.dataElements for the Data Element. If it is found, it evaluates the Data Element based on its configuration (e.g. JS Object, CSS Selector, Custom Code).
If the Data Element is not found, DTM then evaluates the Data Element name to see if it is a Built-In Data Element (e.g. _satellite.getVar('hostname') will return location.hostname)
If the Data Element is still not found, DTM then looks for it in _satellite.data.customVars and returns the value if found, which is where Data Elements set from _satellite.setVar() are stored.
So the main two takeaways from all this are:
a) You cannot set pre-defined Data Elements values using _satellite.setVar(). If you think about it, it doesn't really make sense to be able to do this, because it negates the purpose of configuring them in the interface to populate according to your configuration!
b) Take precaution in the Data Element names you choose to use when configuring them in the interface vs. on-the-fly with _satellite.setVar(), because you may get unexpected values returned if you use the same Data Element name in the interface as from _satellite.setVar().
Additional Note: I may be wrong, but I'm going to take a guess and assume maybe you created the Data Element within the interface as a placeholder so that it will appear in the dropdown fields within rules when using %data_element% syntax. Unfortunately, Data Elements created using _satellite.setVar() will not appear in those dropdowns. However, you can still reference them in the fields using %data_element% syntax. Internally DTM makes a _satellite.getVar() call just like you would in a custom js code box, so it will return the value from _satellite.data.customVars if not found elsewhere.
Interesting. Thanks for your answer.