简体   繁体   English

Google跟踪代码管理器查找并替换变量中的字符

[英]Google Tag manager find and replace characters in variable

Inside GTM, I have a CSS selector variable on DOM. 在GTM内,我在DOM上有一个CSS选择器变量。 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 comply with one part of dynamic remarketing. 我需要遵守动态再营销的一部分。
Example: 例:

MLS_Price = $599,000

How do I "search and replace" those characters in GTM? 如何在GTM中“搜索并替换”这些字符? Custom javascript? 自定义JavaScript? I'm using this, but can't get it to work. 我正在使用它,但是无法正常工作。 Video Explanation: http://screencast-o-matic.com/watch/cbno3V6XuO 视频说明: http : //screencast-o-matic.com/watch/cbno3V6XuO

function() {
    var MLS_Price = "{{MLS_Price}}";
    var MLS_Price = str.replace("$", "");

    var MLS_Price = "{{MLS_Price}}";
    var MLS_Price = str.replace(",", "");
 }

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. 如果使用Custom JS变量,则还需要一个返回值。 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. 最好不要使用CUSTOM HTML TAG,而最好使用VARIABLE(自定义Js),以便下次可以轻松重用这些值。

The code will be similar to that of Abel. 代码将类似于Abel。

function() {
var MLS_Price = {{MLS_Price}};
var MLS_Price_1 = MLS_Price.replace("$", "");
var fullPrice = MLS_Price_1.replace(",", "");
return fullPrice;   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM