Inside GTM, I have a CSS selector variable on DOM. It's for real estate and the variable is the price. I need to remove the characters ($) and (,) in the variable. I need this to ply with one part of dynamic remarketing.
Example:
MLS_Price = $599,000
How do I "search and replace" those characters in GTM? Custom javascript? I'm using this, but can't get it to work. Video Explanation:
function() {
var MLS_Price = "{{MLS_Price}}";
var MLS_Price = str.replace("$", "");
var MLS_Price = "{{MLS_Price}}";
var MLS_Price = str.replace(",", "");
}
Inside GTM, I have a CSS selector variable on DOM. It's for real estate and the variable is the price. I need to remove the characters ($) and (,) in the variable. I need this to ply with one part of dynamic remarketing.
Example:
MLS_Price = $599,000
How do I "search and replace" those characters in GTM? Custom javascript? I'm using this, but can't get it to work. Video Explanation: http://screencast-o-matic./watch/cbno3V6XuO
function() {
var MLS_Price = "{{MLS_Price}}";
var MLS_Price = str.replace("$", "");
var MLS_Price = "{{MLS_Price}}";
var MLS_Price = str.replace(",", "");
}
Share
Improve this question
edited Feb 15, 2017 at 22:59
JAB
21.1k6 gold badges72 silver badges80 bronze badges
asked Feb 15, 2017 at 22:04
Abel MaciasAbel Macias
411 silver badge3 bronze badges
2 Answers
Reset to default 3You don't need to enclose your variables with quotes, because this would just return the stringified version of your variable name. If using a Custom JS variable, you would need a return value as well. You can also chain the string transformations (for example):
function(){
var str = {{MLS_Price}};
return str.substring(1).replace(",", '');
}
Make sure you put in proper error handling and checks and test it out before you publish.
Instead of using a CUSTOM HTML TAG the best way to do will be making an VARIABLE (Custom Js) so that you can reuse the values easily next time.
The code will be similar to that of Abel.
function() {
var MLS_Price = {{MLS_Price}};
var MLS_Price_1 = MLS_Price.replace("$", "");
var fullPrice = MLS_Price_1.replace(",", "");
return fullPrice; }