用一个 dataLayer.push 更新多个产品值

Update multiple product values with one dataLayer.push

假设我有以下数据层:

{
  ecommerce: {
    currencyCode: "USD",
    purchase: {
      actionField: {
        id: "1a6d5021",
        affiliation: "Online Store",
        revenue: 40,
        tax: 0,
        shipping: "",
        coupon: ""
      },
      products: [
        {
          name: "Product 1",
          id: "123",
          price: 40,
          category: null,
          quantity: 1,
          coupon: "disc10",
          type: "Service A"
        },
        {
          name: "Product 4",
          id: "456",
          price: 40,
          category: null,
          quantity: 1,
          coupon: "disc10",
          type: "Service B"
        }
      ]
    }
  }
}

因此在 product 数组中,category 始终具有值 null。我怎样才能为每个产品分别推送与 type 相同的值,同时保持 dataLayer 中的其他所有内容不变?

最终我想要达到的最终结果是这样的:

{
  ecommerce: {
    currencyCode: "USD",
    purchase: {
      actionField: {
        id: "1a6d5021",
        affiliation: "Online Store",
        revenue: 40,
        tax: 0,
        shipping: "",
        coupon: ""
      },
      products: [
        {
          name: "Product 1",
          id: "123",
          price: 40,
          category: "Service A",
          quantity: 1,
          coupon: "disc10",
          type: "Service A"
        },
        {
          name: "Product 4",
          id: "456",
          price: 40,
          category: "Service B",
          quantity: 1,
          coupon: "disc10",
          type: "Service B"
        }
      ]
    }
  }
}

单个产品很容易,但是当多个产品时我完全找不到如何做。

在此先感谢您的帮助。

尝试

function createCategoryFn(category) {
  
  return (properties) => {
    return {
     name: "",
     id: "",
     price: 0,
     category: category,
     quantity: 1,
     coupon: "",
     type: category,
     ...properties
    };
  };      
}

const createSportsProduct = createCategoryFn('Sports');

const tennisProduct = createSportsProduct({ name: 'tennis racket', id: 1, price: 100 });
const basketballProduct = createSportsProduct({ name: 'basketball', id: 2, price: 100 });

console.log(tennisProduct);
console.log(basketballProduct)

有两种选择。

  1. 再次推送整个电子商务对象,现在设置所有字段。它会导致 DL 有点混乱,并且在实施跟踪时必须牢记某些时间问题。

  2. Remove/delay 第一次电子商务推送,直到您拥有所有信息并且只推送一次电子商务对象。

在大多数情况下,2 是最佳选择。当依赖于电子商务对象的事件 必须 在类别对 front-end 可用之前触发时,1 是合理的。

如果我正确理解您的要求,即您希望为每个产品对象分配类别中的类型值。如果是,那就直接了。

工作演示:

const productObj = {
    ecommerce: {
        currencyCode: "USD",
        purchase: {
            actionField: {
                id: "1a6d5021",
                affiliation: "Online Store",
                revenue: 40,
                tax: 0,
                shipping: "",
                coupon: ""
            },
            products: [{
                    name: "Product 1",
                    id: "123",
                    price: 40,
                    category: null,
                    quantity: 1,
                    coupon: "disc10",
                    type: "Service A"
                },
                {
                    name: "Product 4",
                    id: "456",
                    price: 40,
                    category: null,
                    quantity: 1,
                    coupon: "disc10",
                    type: "Service B"
                }
            ]
        }
    }
};

productObj.ecommerce.purchase.products.forEach((obj) => obj.category = obj.type);

console.log(productObj);