Item data on Store API cart lines

Last updated ·

GTM Kit. Requires WooCommerce.

Every cart line the WooCommerce Store API returns carries the GA4 item GTM Kit built for that product, so code that reads the Store API never has to rebuild item data from sku, name and prices.

Where it is

On each entry of items in any wc/store/v1/cart* response, under extensions.gtmkit.item:

{
  "key": "f6c46f4c14f6e612b5138903dab9b5f5",
  "id": 717,
  "quantity": 2,
  "sku": "2267160000",
  "extensions": {
    "gtmkit": {
      "item": "{\"id\":\"2267160000\",\"item_id\":\"2267160000\",\"item_name\":\"Racket Bag\",\"currency\":\"DKK\",\"price\":999,\"item_brand\":\"Adidas\",\"google_business_vertical\":\"retail\",\"item_category\":\"Bags\",\"item_category2\":\"Adidas\"}"
    }
  }
}

The value is a JSON string, so parse it:

const line = cart.items.find( ( entry ) => entry.id === productId );
const item = JSON.parse( line.extensions.gtmkit.item );

The Store API product endpoints (wc/store/v1/products) carry the same item under extensions.gtmkit.item, there as an object rather than a string.

What it contains

The same fields GTM Kit puts on its own view_item, add_to_cart, view_cart and purchase events, built by the same code:

  • id and item_id: the SKU when “Use SKU instead of ID” is on and the product has one, otherwise the product id, with the configured prefix. For a variation, the variation’s SKU or id.
  • item_name
  • currency and price: the price as GTM Kit resolves it, including or excluding tax according to the “Exclude tax” setting.
  • item_brand: when a brand attribute or taxonomy is configured.
  • item_category through item_category5: the primary category path.
  • item_variant: for a variation, its attribute values.
  • google_business_vertical: when configured.

Quantity is not included; the cart line’s own quantity is the live value.

Shaping it

The gtmkit_datalayer_item_data filter runs on every item GTM Kit builds, including these. It receives the item array, the WC_Product, and a context string that tells you which surface asked for it ('product_list', 'view_item', 'remove_from_cart', 'view_cart', 'begin_checkout', 'custom_list', or an empty string for the Store API and the product page):

add_filter( 'gtmkit_datalayer_item_data', function ( array $item, WC_Product $product, string $context ): array {
    $item['item_category_path'] = implode( '/', wp_list_pluck( get_the_terms( $product->get_id(), 'product_cat' ) ?: [], 'name' ) );
    return $item;
}, 10, 3 );

Whatever you add here reaches every event, so the item a custom theme reports from a Store API response matches the purchase the shop reports later.

Using it

  • With GTM Kit Premium, hand the Store API response to gtmkit.cart.sync() and GTM Kit reports the cart change for you, item data included. See “Track cart changes from a custom theme”.
  • With the custom product list tracking API, pass the parsed item to gtmkit.lists.addToCart() together with the quantity and the list context.
  • Without either, push it yourself through window.gtmkit.events.push(), preceded by { ecommerce: null }, so consent gating still applies.

← Previous

Previous article

Next →

Next article