Sub topic 7
Variants and grouping with item_group_id
This chapter assumes you can read the product feed XML from the earlier chapters, know what a SKU and a GTIN are, and can run a short Python script. It covers how to describe one product that comes in several versions, so that shopping systems treat those versions as one product and not as unrelated listings.
What a variant is
A variant is a version of a product that differs in a customer-visible way, such as color or size. The jacket in the earlier examples is one product sold as Blue/M, Blue/L, Black/M, and so on. In your catalog each combination is usually its own SKU. In a feed, each combination is its own item.
Shopping systems need to know which items belong together. Without that, a shopper asking for "a waterproof trail jacket under $100" may see six near-identical results, or the system may pick one size at random. Grouping lets the system show one product, offer the colors and sizes that exist, and choose the variant that fits the request.
How item_group_id works
The item_group_id attribute holds a value shared by every variant of one product. Each variant keeps its own unique id.
ididentifies one sellable variant. It must be unique in the feed and should stay stable over time.item_group_ididentifies the parent product. All variants of the jacket use the same value.
Here are two variants of one jacket:
<item>
<g:id>SKU-1001-BLU-M</g:id>
<g:title>Acme Trail Runner Jacket - Men's, Blue, Medium</g:title>
<g:link>https://www.example.com/p/trail-runner-jacket?color=blue&size=m</g:link>
<g:image_link>https://www.example.com/img/trail-runner-jacket-blue.jpg</g:image_link>
<g:price>89.00 USD</g:price>
<g:gtin>00012345678905</g:gtin>
<g:item_group_id>SKU-1001</g:item_group_id>
<g:color>Blue</g:color>
<g:size>M</g:size>
</item>
<item>
<g:id>SKU-1001-BLK-L</g:id>
<g:title>Acme Trail Runner Jacket - Men's, Black, Large</g:title>
<g:link>https://www.example.com/p/trail-runner-jacket?color=black&size=l</g:link>
<g:image_link>https://www.example.com/img/trail-runner-jacket-black.jpg</g:image_link>
<g:price>94.00 USD</g:price>
<g:gtin>00012345678912</g:gtin>
<g:item_group_id>SKU-1001</g:item_group_id>
<g:color>Black</g:color>
<g:size>L</g:size>
</item>(The snippet omits description, availability, condition, and brand for space. A real item needs them.)
The group value can be any stable string. Many merchants use the parent SKU, as above. The value only has to be identical across the variants and different from the value used by any other product.
Variant attributes
The group says which items belong together. The variant attributes say how they differ. Common ones are:
| Attribute | Typical use |
|---|---|
| `color` | Apparel, electronics, home goods |
| `size` | Apparel, shoes, mattresses |
| `material` | Furniture, jewelry, bags |
| `pattern` | Fabrics, rugs, clothing |
| `gender` | Apparel and shoes |
| `age_group` | Apparel and shoes |
| `size_type`, `size_system` | Clarify sizing such as "plus" or "US" |
Two rules matter most:
- Variants in one group should differ in at least one of these attributes. Two items with the same group and identical attributes look like duplicates.
- Apparel has stricter requirements. The specification asks for
color,size,gender,age_group, anditem_group_idon apparel in many countries. Check the current specification for your target country and category.
What must differ per variant, and what may not
A variant is a separate offer, so most attributes belong to the variant and not to the group.
- Per variant:
id,link,image_link,gtin,color,size,availability, and oftenprice. - Usually shared:
brand,description(unless a variant needs its own wording),product_type, anditem_group_id. - Title: include the variant attributes, as in "Blue, Medium", so a title alone distinguishes the item.
Each variant needs its own GTIN, because the manufacturer assigns a separate barcode to each color and size. Do not copy the parent's GTIN onto all variants. A repeated GTIN across different variants confuses matching and may trigger a disapproval.
The link should open the page for that variant, or the parent page with that variant preselected. A shopper who clicked "Blue, Medium" and lands on a page showing black and large will see a mismatch. This is a consistency problem, covered in a later chapter.
Mirroring the grouping on the product page
Page markup should reflect the same structure. schema.org provides ProductGroup for the parent and hasVariant for the variants. The productGroupID value should equal the feed's item_group_id.
{
"@context": "https://schema.org",
"@type": "ProductGroup",
"name": "Acme Trail Runner Jacket",
"description": "Lightweight water-resistant running jacket with a packable hood and reflective trim.",
"brand": { "@type": "Brand", "name": "Acme" },
"productGroupID": "SKU-1001",
"variesBy": ["https://schema.org/color", "https://schema.org/size"],
"hasVariant": [
{
"@type": "Product",
"name": "Acme Trail Runner Jacket - Men's, Blue, Medium",
"sku": "SKU-1001-BLU-M",
"gtin14": "00012345678905",
"color": "Blue",
"size": "M",
"image": "https://www.example.com/img/trail-runner-jacket-blue.jpg",
"offers": {
"@type": "Offer",
"url": "https://www.example.com/p/trail-runner-jacket?color=blue&size=m",
"priceCurrency": "USD",
"price": "89.00",
"availability": "https://schema.org/InStock"
}
}
]
}A real page lists every variant in hasVariant, and each variant's sku matches the feed id. The example shows one variant to keep it short.
Checking groups before you submit
The validation script from the earlier chapter checks single items. Group problems show up only when you compare items with each other. The script below reads a feed and reports groups that have one member, items with no variant attributes, duplicate attribute combinations, and which attributes vary within each group.
import sys
import xml.etree.ElementTree as ET
from collections import defaultdict
NS = {"g": "http://base.google.com/ns/1.0"}
VARIANT_ATTRS = ["color", "size", "material", "pattern", "gender", "age_group"]
def text(item, tag):
node = item.find("g:" + tag, NS)
return node.text.strip() if node is not None and node.text else ""
def main(path):
groups = defaultdict(list)
for item in ET.parse(path).getroot().iter("item"):
group_id = text(item, "item_group_id")
if group_id:
groups[group_id].append(item)
problems = 0
for group_id, items in sorted(groups.items()):
if len(items) == 1:
print(group_id + ": only one item in this group")
seen = {}
for item in items:
item_id = text(item, "id") or "(no id)"
combo = tuple(text(item, a) for a in VARIANT_ATTRS)
if not any(combo):
problems += 1
print(group_id + "/" + item_id + ": no variant attributes")
elif combo in seen:
problems += 1
print(group_id + "/" + item_id + ": same attributes as " + seen[combo])
else:
seen[combo] = item_id
varying = [
a for a in VARIANT_ATTRS
if len({text(item, a) for item in items}) > 1
]
print(group_id + ": " + str(len(items)) + " items, varies by "
+ (", ".join(varying) or "nothing"))
print("problems found: " + str(problems))
return 1 if problems else 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1]))Run it with python check_groups.py feed.xml. A group that "varies by nothing" is the clearest sign of a duplicate listing or a missing attribute.
Common errors
- Reusing one group ID for different products. If a jacket and a hat share
SKU-1001, systems may merge them or reject the group. Keep one group per product. - Grouping items that are different products. A 500 ml and a 1 L bottle may be variants. A basic model and a "Pro" model with different features are usually separate products with their own groups.
- Changing itemgroupid between feed runs. A new value looks like a new product and can reset the product's history. Treat it like
id: assign once and keep it. - Putting variant data only in the title. "Blue, Medium" in the title does not replace the
colorandsizeattributes. Systems read the attributes to build the group. - Using one image for every variant. If the Blue and Black jackets use the same photo, the color attribute and the picture disagree.
- Leaving out-of-stock variants unexplained. Keep out-of-stock variants in the feed with
availabilityset toout_of_stock, so the group stays complete and the system knows the size exists.
Exercise
A feed has four items. Items A and B have itemgroupid "TEE-9", color Red, and sizes S and M. Item C has itemgroupid "TEE-9", color Red, size M, and a different id from B. Item D has itemgroupid "TEE-9" and no color or size. Using the script in this chapter, which items does it report, and why?
Show answer
Item C is reported as having the same attributes as B, because the combination (Red, M) repeats within the group. Item D is reported as having no variant attributes. Items A and B are not reported. The summary line for TEE-9 shows 4 items that vary by color and size only if the colors or sizes differ across the group. Here the attributes that differ are size (S, M, and empty), so the script prints "varies by size".
Exercise
A merchant sells a mug in two colors and two sizes. They publish four feed items that all use the same id "MUG-1" and the same GTIN, and no itemgroupid. Name two changes that make this feed describe one grouped product correctly.
Show answer
Give each item a unique id, such as MUG-1-RED-S, and a GTIN specific to its color and size. Add itemgroupid "MUG-1" to all four items, with color and size set on each. Separate links and images per variant are also recommended.