javascriptarraysgoogle-tag-managergoogle-datalayerdata-layers

Update multiple product values with one dataLayer.push


Say I have the following 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: null,
          quantity: 1,
          coupon: "disc10",
          type: "Service A"
        },
        {
          name: "Product 4",
          id: "456",
          price: 40,
          category: null,
          quantity: 1,
          coupon: "disc10",
          type: "Service B"
        }
      ]
    }
  }
}

So in the product array, category always has value null. How can I push the same value as type respectively for each product, whilst leaving everything else in the dataLayer untouched?

Ultimately the final result that I am trying to achieve would be like this:

{
  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"
        }
      ]
    }
  }
}

It be easy with a single product, but I quite can't find how to do it when multiple products.

Thanks in advance for your help.


Solution

  • If I understand your requirement correctly that you want to assign the type value in the category for each products object. If Yes, Its a straight forward.

    Working Demo :

    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);