我正在尝试将最近的html标记(带有id)的文本内容添加到我的facebook像素标准事件参数中 . 它应该适用于任何html标签,我希望它是灵活的,无论文本是否在div,span,p或其他内 .
下面是我添加到GTM标签的jQuery . 请注意,{}是本机GTM变量 . 另请注意,我用来触发GTM标记的GTM触发器(因此下面的jQuery代码)已设置,因此在单击“buy”类时会启动脚本(请参阅HTML代码) .
<script>
// look for the content_name
var presentElement = {{Click Element}};
var product_name_element = presentElement;
var cond = true;
while(cond){
if(product_name_element.id=="product_name"){
cond = false;
}else{
product_name_element = product_name_element.previousElementSibling;
}
}
var product_name = product_name_element.textContent;
fbq('track', 'AddToCart', {
content_name: product_name
});
</script>
和HTML:
<p id="product_name">Product 1</p>
<p><a href="#" class="buy">Order</a></p>
<p id="product_name">Product 2</p>
<p><a href="#" class="buy">Order</a></p>
-
所以基本上,在我的FB像素事件中,我应该有以下内容:
-
content_name:产品1
单击第一个Order链接时 -
和
-
content_name:产品2
单击第二个Order链接时 -
.
任何帮助都会非常高兴!
非常感谢 .
1 回答
在您的情况下,您需要创建触发器:
事件类型:点击 - 只需链接
点击元素;匹配css选择器;
.buy
然后创建标签:
标记类型:自定义HTML
HTML
<script> // look for the content_name var presentElement = {{Click Element}}; var product_name_element = presentElement.parentElement; var cond = true; while(cond && product_name_element!= null){ if(product_name_element.id=="product_name"){ cond = false; }else{ product_name_element = product_name_element.previousElementSibling; } } if (product_name_element != null) { var product_name = product_name_element.textContent; fbq('track', 'AddToCart', { content_name: product_name }); } </script>