Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

98
Vistas
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.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar

0

There are two options.

  1. Push the whole ecommerce object again, with all fields set now. It results in a bit of a mess in DL and certain timing issues one has to keep in mind when implementing tracking.

  2. Remove/delay the first ecommerce push till you have all info and only push the ecommerce object once.

In most cases, 2 is the best option. 1 can be justified when the event relying on the ecommerce object has to fire before categories become available to the front-end.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try

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)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda