最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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;   }
发布评论

评论列表(0)

  1. 暂无评论