Manipulating data source values?

Gold Contributor
Gold Contributor
We have an issue where a certain tag wants us to pass them product names. This is fine because I'm already passing that as a data source array (t_product_name). The issue I've come across is that many of our product names have non-alphanumeric characters such as single quotes ('). Since code releases are a no-go until after the holiday season, I've been trying to come up with a way of manipulating this with JS. I've created a new JavaScript extension and added the function I'd like to use to "clean" up each product name. The thing is, I can't find any documentation in the Scope Vars field or Output field. Should the Scope Vars contain the data source variable name t_product_name? Here is the JS I'm using (without the slashes): //function cleanProductId(t_product_name) { // var cleanProductName = decodeURIComponent(t_product_name); // cleanHtml = cleanHtml.replace(/"/g, '').replace(/'/g, '').replace(/</g, '').replace(/>/g, '').replace(/&/g, ''); // cleanHtml = cleanHtml.replace(/[^a-zA-Z0-9]/g,''); // return cleanProductName; //}
9 REPLIES 9

Manipulating data source values?

Employee Emeritus
Hey Larry, I am doing something similar for our breadcrumb value. I use the Set Value Extension to remove white space within a variable in which I am just setting the variable equal to the code. Not sure if this is best practice but it works. page.breadcrumb (js) = JS Code --- b['page.breadcrumb'].replace(/\s+/g, "");

Manipulating data source values?

Gold Contributor
Gold Contributor
Thanks James! So, based on your example, what I have should work. What do you use for the Scope Vars and Output field? Thanks, Larry

Manipulating data source values?

Employee Emeritus

Well, I am using the Set Values extension instead of the Javascript Extension. In my case page.breadcrumb is a data source that I have mapped and I am just replacing that value with a cleaner version of itself. Here is a link to a thread on scope vars. I haven't read through it but maybe it will help. https://community.tealiumiq.com/t5/Tealium-iQ/How-to-use-Scope-Vars/m-p/6104

Manipulating data source values?

Employee Emeritus
Hey Larry, You could use something like this the attached below. I can't get Bloomfire to upload the code. This will loop through each value in the t_product_name array and clear out any character that is contained in the square brackets. This will then set the variable back to the b object so it can be used by the Ecommerce and other extensions as needed. The Scoped vars and Output lines are deprecated pieces of functionality that are no longer needed, it just has not been removed. We used to have to declare variables that were used in the JS extension so that the syntax of the code could be validated. Using b.variable would have thrown an error so "b" would have needed to be declared. This field can be left blank.

Manipulating data source values?

Tealium Employee
The "Scope Vars" and "Output" fields are deprecated so no need to enter values there. I just want to add that the way the data is set in the UDO is important. Let me explain. Here is a sample UDO that is incorrect: var utag_data = { language : "en-us", page_name : "tealium-test", t_product_name : ["32" belt","some t-shirt"], t_product_category : ['belt','t-shirt's'], var1:"Confirmation Page" }; Notice that neither 32" belt nor t-shirt's is escaped. This will cause the UDO to not be set as these are syntax errors. I'm assuming the data is getting set correctly on the page such as: var utag_data = { language : "en-us", page_name : "tealium-test", t_product_name : ["32\" belt","some t-shirt"], t_product_category : ['belt','t-shirt\'s'], var1:"Confirmation Page" }; This will evaluate correctly. If set as such, then Tealium and the browser will automatically handle encoding and sending that data appropriately to the tag vendors. So it seems like you shouldn't have to do anything special. Just wanted to throw that out there.

Manipulating data source values?

Gold Contributor
Gold Contributor
Hi Dan, Yes, I see what you mean. This is actually an issue with many other tools we use. That's why we always use 2 single quotes instead of double quotes for all product and site copy. For example: Red ''Blah'' T-Shirt instead of: Red "Blah" T-Shirt The issue I'm having is with a DoubleClick Floodlight tag. It sees the first single quote, replaces it with ' and then ignores everything after it. Thanks, Larry

Manipulating data source values?

Tealium Employee
Gotcha, let us know if Jared's solution works. If not, we may need to step through the JS code to see where/when the other quotes are being ignored so we can figure out how to adjust his code properly.

Manipulating data source values?

Gold Contributor
Gold Contributor
Sorry, that should have been "replaces it with "& # 039 ;"... (no spaces)

Manipulating data source values?

Gold Contributor
Gold Contributor
Ok, after some tinkering, I finally got it to work. The one issue I had was that "& # 039 ;" (no spaces) was coming across as 039. So, I decided to just remove that as well. We'll never have a product with 039 in it's name so this should be fine. I really appreciate everyone's help on this. Thanks, Larry
Public