Reply
Highlighted
Visitor
Posts: 10
Registered: ‎08-22-2017
Accepted Solution

adding variables to existing datalayer

beginner question : if i have already declared utag_data variables on a page, can i still add new set of variables in future without overwriting the existing datalayer or editing the existing code on the page ? how do i do that ?  thanks!

Reply
0 Kudos
Occasional Contributor
Posts: 36
Registered: ‎11-02-2015

Re: adding variables to existing datalayer

[ Edited ]

Hello @hcerus,

 

If i understand the question correctly, you are trying to add a new variable to utag_data Javascript object that already exists on the web page.

 

utag_data on page acts like any other javascript object variable on web pages. So you can add new variables directly as part of definition as below

var utag_data = {var1: "val1", newvariable:"value"};

 

or 

var utag_data = {var1"val1"};

utag_data.newvariable = "value";

or

var utag_data = {var1"val1"};

utag_data["newvariable"] = "value";

 

Here is a link i could find more details about utag_data object

https://community.tealiumiq.com/t5/JavaScript-utag-js/How-utag-data-works/ta-p/15369

 

Hope this helps.

 

Thanks

Abraham

Visitor
Posts: 10
Registered: ‎08-22-2017

Re: adding variables to existing datalayer

Thank you Abraham,

 

That certainly answers the question. Just out of curiosity :

is there any difference or practical implication or best practice that I should be aware of when choosing one of the method above versus the other two suggested?

I am planing the implementation of the data layer and this will be good to know.thanks!

 

 

Occasional Contributor
Posts: 36
Registered: ‎11-02-2015

Re: adding variables to existing datalayer

Hello @hcerus,

 

I would prefer to use first approach, if we have the new variables data available as part of utag_data variable definition. This would help ensure, all variables are defined together at one place and before utag.js is loaded.

var utag_data = {var1: "val1", newvariable:"value"};

 

If it is like, you would like to append new variables to an existing data layer thats already defined, then we could use approach 2 or 3.

utag_data.newvariable = "value";

or 

utag_data["newvariable"] = "value";

 

I havent seen any disadvantages on the earlier provided approaches so far, looks like JavaScript's flexible syntax allows to have variables set in different ways.:)

 

Here is some additional note that might be helpful: The UDO(utag_data) can be updated with additional values outside of the declaration block, as long as the data gets set prior to loading utag.js. UDO variables set after loading utag.js will be ignored.

 

Thanks

Abraham 

Visitor
Posts: 10
Registered: ‎08-22-2017

Re: adding variables to existing datalayer

[ Edited ]

Thanks @abrahameaso, it is crystal clear now :)