Menu
Guides
API Reference

Guides

VTEX Ads Stories Package Product Hydration

Configure product hydration for the vtex-ads-story-carousel element with Intelligent Search or a custom strategy, including price fallbacks, availability rules, and request timeouts.

5 min read

The <vtex-ads-story-carousel> element of the @vtex/ads-stories package can hydrate the product offers returned by the VTEX Ads API with current product data from your store. Hydration retrieves product prices and availability in one request after the carousel appears, then displays that data instead of the product_metadata values from the ad response.

If you haven't installed the package yet, see the VTEX Ads Stories Package Quickstart.

Story ads are off by default. Request enablement for your store through VTEX Support before displaying Stories. For the setup steps, see Before you begin.

Choose a hydration strategy

Choose how the element retrieves product data with these options:

  • Set the element's hydration property to a custom strategy object with fetcher, matcher, price, and, optionally, available members. Set the property before adding the element to the page. Setting it later makes the element request the Stories again.
  • Set the hydration="none" attribute to prevent the element from requesting product data.
  • Set neither option and provide account-name to use the default Intelligent Search strategy. It requests products from https://{account-name}.vtexcommercestable.com.br.
  • Set neither option and omit account-name to display the product data from the ad response without requesting product data.

The hydration-timeout attribute limits the product request duration in milliseconds. Its default value is 5000. For details about account-name, hydration, and hydration-timeout, see VTEX Ads Stories Package Configuration.

If your store uses a Content Security Policy, allow the Intelligent Search host or any host called by your custom fetcher. See VTEX Ads Stories Package Content Security Policy.

Configure a custom strategy

A custom hydration strategy contains the following members:

MemberTypeDescription
fetcherProductFetcherFetches the products for the Story offers in one request.
matcherProductMatchesOfferIdentifies which fetched product belongs to each offer.
priceProductPriceReaderReturns an offer's current price as { price, listPrice? }.
availableProductAvailabilityReaderOptional. Returns whether the shopper can buy the product.

This example matches products and offers by SKU. Replace fetchMyProducts with your store's function for fetching catalog products.


_11
const story = document.querySelector("vtex-ads-story-carousel");
_11
_11
story.hydration = {
_11
fetcher: async (offers, identity, { signal }) =>
_11
fetchMyProducts(offers, signal),
_11
matcher: (product, offer) => product.sku === offer.skuId,
_11
price: (product) => ({
_11
price: product.price,
_11
listPrice: product.listPrice,
_11
}),
_11
};

The fetcher receives the shopper's identity with the offers and request options. Use the identity only within the hydration strategy and don't forward it to another service or third party.

In TypeScript, use StoryHydration from @vtex/ads-stories. Replace MyProduct with the type of product your fetcher returns.


_11
import type { StoryHydration } from "@vtex/ads-stories";
_11
_11
const hydration: StoryHydration<MyProduct> = {
_11
fetcher: async (offers, identity, options) =>
_11
fetchMyProducts(offers, options?.signal),
_11
matcher: (product, offer) => product.sku === offer.skuId,
_11
price: (product) => ({
_11
price: product.price,
_11
listPrice: product.listPrice,
_11
}),
_11
};

For REST API and GraphQL fetcher and matcher examples, see VTEX Ads Core Package Custom Hydration.

Handle loading and fallback data

While the product request is pending, the price and the product panel's Add to cart button show placeholders. After the request succeeds, the element displays the price returned by the strategy. A listPrice is struck through only when it is higher than price.

The element falls back to the product's product_metadata from the ad response when a fetched price isn't available. This happens in any of these cases:

  • No hydration strategy is active.
  • The product request fails or exceeds hydration-timeout.
  • The strategy doesn't find the product.
  • The product doesn't have a price.
  • The price member doesn't return an amount.

The fallback uses promotional_price as the current price when the product is on sale and price as its original price. If neither value is available, the card displays only the product name.

Configure availability

Add the optional available(product, offer) member to a custom strategy when the store needs to determine whether a product can be bought. When it returns false:

  • The Story keeps playing.
  • The product price remains visible. If there is no price, Sold out appears instead.
  • The Add to cart button becomes a disabled Product unavailable button.
  • View product remains available and opens the product page.

When available is omitted or doesn't return a value, the element treats the product as available. For the corresponding player behavior, see Sold-out products.

The default Intelligent Search strategy reads AvailableQuantity. It treats a SKU with an AvailableQuantity value of 0 or less as sold out. This can incorrectly mark a product as Sold out when the store allows backorders or doesn't track stock. To keep the Intelligent Search price and product lookup, reuse its fetcher, matcher, and price reader and supply your own available function.

If you installed the element through npm, add @vtex/ads-core as a direct dependency to import those three functions. Keep the element's account-name attribute set so the fetcher can query your store's catalog.


_10
npm install @vtex/ads-core

Replace isAvailableInMyStore with a function that returns whether the shopper can buy the SKU from the offer's seller.


_14
import {
_14
intelligentSearchFetcher,
_14
intelligentSearchMatcher,
_14
intelligentSearchPrice,
_14
} from "@vtex/ads-core";
_14
_14
const story = document.querySelector("vtex-ads-story-carousel");
_14
_14
story.hydration = {
_14
fetcher: intelligentSearchFetcher,
_14
matcher: intelligentSearchMatcher,
_14
price: intelligentSearchPrice,
_14
available: isAvailableInMyStore,
_14
};

If your store treats every product as available, omit available from the custom strategy. If you use only the CDN script, implement fetcher, matcher, and price yourself, plus available if needed. The CDN script doesn't expose the Intelligent Search functions.

Next steps