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.
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
hydrationproperty to a custom strategy object withfetcher,matcher,price, and, optionally,availablemembers. 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-nameto use the default Intelligent Search strategy. It requests products fromhttps://{account-name}.vtexcommercestable.com.br. - Set neither option and omit
account-nameto 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:
| Member | Type | Description |
|---|---|---|
fetcher | ProductFetcher | Fetches the products for the Story offers in one request. |
matcher | ProductMatchesOffer | Identifies which fetched product belongs to each offer. |
price | ProductPriceReader | Returns an offer's current price as { price, listPrice? }. |
available | ProductAvailabilityReader | Optional. 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.
_11const story = document.querySelector("vtex-ads-story-carousel");_11_11story.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.
_11import type { StoryHydration } from "@vtex/ads-stories";_11_11const 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
pricemember 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 cartbutton becomes a disabledProduct unavailablebutton. View productremains 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.
_10npm install @vtex/ads-core
Replace isAvailableInMyStore with a function that returns whether the shopper can buy the SKU from the offer's seller.
_14import {_14 intelligentSearchFetcher,_14 intelligentSearchMatcher,_14 intelligentSearchPrice,_14} from "@vtex/ads-core";_14_14const story = document.querySelector("vtex-ads-story-carousel");_14_14story.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
- VTEX Ads Stories Package Configuration: Configure
account-name, hydration, and request timeouts. - VTEX Ads Stories Package Layout and Player: Understand how product prices and availability appear in the carousel and player.
- VTEX Ads Stories Package Content Security Policy: Allow the hosts used by Intelligent Search or a custom product fetcher.