Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!