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

109
Views
Count the number of in-stock product variants from the dataLayer

I am working in Google Tag Manager on an ecomm site.

On the product pages I need to know the number of products that are in stock.

The dataLayer has sizes nested under each color option. Each size has a value showing if the size is in stock ("available": "false" or "true").

The code I am providing is what shows in the dataLayer. In this example, 3 of the 4 sizes are in stock, so I looking for the JS to run that will count the number of ""available": "true" - in this case the answer would be 3.

I am hoping for a solution that can be used in a variable in Google Tag Manager.

{
"product": {
    "variants": [
        {
            "colorID": "03",
            "colorName": "MEDIUM DENIM",
            "sizes": [
                {
                    "sizeID": "10",
                    "sizeName": "33",
                    "available": "false",
                    "price": "24.0"
                },
                {
                    "sizeID": "1",
                    "sizeName": "24",
                    "available": "true",
                    "price": "24.0"
                }
            ]
        },
        {
            "colorID": "06",
            "colorName": "LIGHT DENIM",
            "sizes": [
                {
                    "sizeID": "1",
                    "sizeName": "24",
                    "available": "true",
                    "price": "24.0"
                },
                {
                    "sizeID": "2",
                    "sizeName": "25",
                    "available": "true",
                    "price": "24.0"
                }
            ]
        }
    ]
}

}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If I understand correctly what result is expected

const data = {"product": {"variants": [
  {"colorID": "03","colorName": "MEDIUM DENIM","sizes": [
    {"sizeID": "10","sizeName": "33","available": "false","price": "24.0"},
    {"sizeID": "1","sizeName": "24","available": "true","price": "24.0"}]},
  {"colorID": "06","colorName": "LIGHT DENIM","sizes": [
    {"sizeID": "1","sizeName": "24","available": "true","price": "24.0"},
    {"sizeID": "2","sizeName": "25","available": "true","price": "24.0"}]}]}};
    

const result = {
  product: {
    variants: data.product.variants.map((variant) => (
      { 
        ...variant,  
        sizes: variant.sizes.filter(({ available }) => available === "true"),
      }
    ))
  }
}



console.dir(result, {depth: null})
.as-console-wrapper{min-height: 100%!important; top: 0}

---- update ---

const data = {"product": {"variants": [
  {"colorID": "03","colorName": "MEDIUM DENIM","sizes": [
    {"sizeID": "10","sizeName": "33","available": "false","price": "24.0"},
    {"sizeID": "1","sizeName": "24","available": "true","price": "24.0"}]},
  {"colorID": "06","colorName": "LIGHT DENIM","sizes": [
    {"sizeID": "1","sizeName": "24","available": "true","price": "24.0"},
    {"sizeID": "2","sizeName": "25","available": "true","price": "24.0"}]}]}};
    
const result = data.product.variants
  .flatMap((variant) => variant.sizes
    .filter(({ available }) => available === "true")
  ).length;

console.dir(result, {depth: null})
.as-console-wrapper{min-height: 100%!important; top: 0}

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!