How can I map a JS value based on certain DOM conditions?

Bronze Contributor
Bronze Contributor

I want to check for an DOM element's existence, then create/set a variable based on that condition, then map this variable value to a tag. Should this be done in a JS extension? If so how would I create the data layer variable to map (or do I have to do that)?

 

 

6 REPLIES 6

How can I map a JS value based on certain DOM conditions?

Tealium Employee

Further to the answer from @Srinivasan - you can actually do this using just a set data values extension, with a condition.  Then just take the output from the extension (in this case, test_trait) and map it to the required tag. 

 

If you need to create a new data source you can add it directly from the extension, by pressing the + button in the middle of the configuration box (see below)

 

You could also scope the extension to the tag, if you only want that data to be available for that exact tag. 

 

 

Example Set Data Values extension

Connecting data systems since the 1980s.

How can I map a JS value based on certain DOM conditions?

Bronze Contributor
Bronze Contributor

I don't think the "set data values" extension will allow custom JS.  I will need that to parse the DOM and set my value.  

How can I map a JS value based on certain DOM conditions?

Bronze Contributor
Bronze Contributor

One other thing I forgot to mention, I noticed that there is no conditional configuration option available on the custom JS extension.  Is there a way to isolate the custom JS to a specific URL (aside from the custom JS to detect the path, etc.)?

How can I map a JS value based on certain DOM conditions?

Bronze Contributor
Bronze Contributor

Can I use a ternary conditional in the JS value field of the "Set Data Value" extension?

 

something like

 

[if DOM element exists] ? 'value one' : 'value two';

 

 

How can I map a JS value based on certain DOM conditions?

Tealium Employee

You can use a ternary conditional in the JS value field, yes.  

 

Connecting data systems since the 1980s.
Public