简体   繁体   English

为 GTM 自定义 javascript 变量数组添加过滤器

[英]Add a filter to GTM custom javascript variable array

We have a ecommerce system that pushes through a datalayer.我们有一个通过数据层推送的电子商务系统。 Unfortunately, the datalayer includes the 'building blocks' of our products.不幸的是,数据层包含了我们产品的“构建块”。 Resulting in tags that register more information than I would like.导致标签注册的信息比我想要的更多。 I would like to exclude these 'building blocks' through a custom javascript variable.我想通过自定义 javascript 变量排除这些“构建块”。 See an example of the datalayer below.请参阅下面的数据层示例。

    checkout: {
      actionField: {step: 1},
      currencyCode: 'EUR',
      products: [
        {
          name: 'product name',
          id: '40291',
          price: '149,00',
          category: 'Transportation/Other',
          quantity: 2
        },
        {
          name: 'building block 1',
          id: '20231',
          price: '0,00',
          category: 'Transportation/Other',
          quantity: 2
        },
        {
          name: 'building block 2',
          id: '12302',
          price: '0,00',
          category: 'Transportation/Other',
          quantity: 2

I've made a CJS variables that makes the products.id, .name, .price and.quantity an array (see below), but I am unable to figure out how to add a filter() that excludes from the array the values where corresponding product.name = "building block 1" && "building block 2".我已经创建了一个 CJS 变量,它使 products.id、.name、.price 和 .quantity 成为一个数组(见下文),但我无法弄清楚如何添加一个从数组中排除值的 filter()其中对应的 product.name = "building block 1" && "building block 2"。

  var products = {{dlv - product}};
  return products.reduce(function(arr, prod) { return arr.concat(prod.id); }, []);

Would anybody be able to help me out with this part of the function?有人能帮我解决 function 的这一部分吗? Alternatively, I was thinking I might be able to achieve the same with a conditional if() statement.或者,我在想我可以通过条件 if() 语句来实现同样的效果。

Thanks in advance!提前致谢!

I'm not sure how GTM works, but if you want to filter an array with JS you could use something like this:我不确定 GTM 是如何工作的,但如果你想用 JS 过滤数组,你可以使用这样的东西:

checkout.products.filter(product => !product.name.includes('building block'))

Let me know if this helps or I'm missing something.让我知道这是否有帮助或我错过了什么。

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

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