I\'m looking for a way to dynamically update a pre-defined data element in DTM. Once the page loads and the data elements are initialized it is not possible to update them,
In order to set a persistent data element to a new value, you need to first update whatever source the data element is based off of, and then use _satellite.getVar('element_name_here')
to force evaluation of the data element.
The key here is that on page load, the order of operations for a persistent data element is basically:
But when you explicitly call _satellite.getVar('Example')
, the order of operations is instead:
Data Element Example:
Name: Example
Type: JS Object
Path: someVariable
Default Value: default value
Remember this value for: Session
So let's say I have on the page the following (set prior to DTM script tag):
someVariable = 'foo';
This will make available a data element named "Example" which you can reference with %Example%
or _satellite.getVar('Example')
, depending on the context.
For the duration of the session, the data element will have that value "foo". So to change it, you would do the following:
someVariable='bar';
_satellite.getVar('Example');
You can call this within your own script somewhere, or if you want to throw this into the mix for e.g. a page load rule, you can add it as a condition inside a Criteria > Data > Custom
code box (note: if you put it here, add a return true;
as 3rd line), etc..
As an alternative, if you are feeling more adventurous, or need a work-around for your data element setup (e.g. your data element is type Custom Script and you need to sidestep the logic in there)..
When you have a data element configured to be persistent, it sets a cookie named
_sdsat_[data element name]
So in the example above, it sets a cookie named
_sdsat_Example
So, if you really wanted to, you can instead just update the cookie with the new value, and just let the default order of operations return the (now updated) cookie value. (note: since it's not possible with javascript to get the expiration of a cookie, if you are setting the scope to "Visitor", you can set the expiration to 2 years, which is what DTM does)