I am trying to create a custom JavaScript code extension in Tealium to reduce the amount of Extensions needed to differentiate CTA's for icon links that do not have alt, or title tags. I am getting the value of my b.variable in my browser console. What I am stuck on is how to set the value of a variable and have it returned to my data-layer for later usage.
if (b.nav_icon){
var classattr = b.nav_icon;
if( classattr.indexOf('icon') >= 0 & classattr.indexOf('world-new') >= 0){
var b.nav_icon = 'nav:Language Switcher';
}
}
return b.nav_icon;
How do I set and return a variable back to the data-layer?
To start with I would recommend looking at this doc TLC - The b Object. From a JavaScript extension that is All Tags Scoped, you can only return true/false, to stop the execution of the event.
To set data for later use, you can either use b
for event level storage, or for page level storage utag.data
(if you are on the latest version of the loader file).
In the example above you can change to the following:
if (b.nav_icon){
var classattr = b.nav_icon;
if( classattr.indexOf('icon') >= 0 & classattr.indexOf('world-new') >= 0){
b.nav_icon = 'nav:Language Switcher';
}
}
Notice the removal of the var
in the internal if statement and the return
. We are now updating the value b.nav_icon
to the new value. Which can be used from other extensions or tags, in this event flow.