javascript - Google Tag manager find and replace characters in variable - Stack Overflow

Inside GTM, I have a CSS selector variable on DOM. It's for real estate and the variable is the pr

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
Add a ment  | 

2 Answers 2

Reset to default 3

You 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;   }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745356925a4624175.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信