VTEX Ads Stories Package Events
Handle the add-to-cart and remove-from-cart events dispatched by the vtex-ads-story-carousel element, and learn when it sends Story ad impression, view, and click events.
The <vtex-ads-story-carousel> element of the @vtex/ads-stories package works with two kinds of events. It dispatches commerce events to your storefront when a shopper adds a product to the cart or removes it in the player, and it sends each Story's impression, view, and click events to VTEX Ads. This guide explains how to handle the commerce events in your store and when the element sends each ad event.
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.
Commerce events
The element has no cart of its own. It notifies your store with custom events, and your store updates the cart. The events bubble and are composed, so you can listen for them on the element, on document, or on any ancestor of the element.
The following table lists the two events and when the element dispatches each one:
| Event | detail | When the element dispatches it |
|---|---|---|
vtex-ads-story:add-to-cart | { sku, seller, quantity } | The shopper clicks Add to cart or the plus control of the quantity stepper in the player. |
vtex-ads-story:remove-from-cart | { sku, seller, quantity } | The shopper clicks the minus or trash control of the quantity stepper in the player. |
Both events have the same detail fields:
| Field | Description |
|---|---|
sku | The Story product's product_sku, as the VTEX Ads API returned it. |
seller | The Story product's seller_id, as the VTEX Ads API returned it. The field is left out when the VTEX Ads API sends no seller. |
quantity | How many items to add or remove. It is always 1. |
Neither event has a default action. Your store handles the cart:
vtex-ads-story:add-to-cart: The store adds the item to the cart. The player stays open.vtex-ads-story:remove-from-cart: The store removes the item or lowers its quantity by one.
Handle the events in your store
Listen for both events and update your store's cart. In the following example, addToMyCart and removeFromMyCart stand for your store's own cart functions:
_10document.addEventListener("vtex-ads-story:add-to-cart", (event) => {_10 const { sku, seller, quantity } = event.detail;_10 addToMyCart(sku, quantity, seller);_10});_10_10document.addEventListener("vtex-ads-story:remove-from-cart", (event) => {_10 const { sku, seller, quantity } = event.detail;_10 removeFromMyCart(sku, quantity, seller);_10});
The player's quantity stepper doesn't read the store's cart. To understand how the player counts what the shopper added, see Cart stepper.
Use the TypeScript types
After you import the package, listeners for these events are typed as StoryAddToCartEvent and StoryRemoveFromCartEvent on any element, document, and window. The ADD_TO_CART_EVENT and REMOVE_FROM_CART_EVENT constants hold the event names, and StoryAddToCartDetail and StoryRemoveFromCartDetail type each event's detail:
_27import {_27 ADD_TO_CART_EVENT,_27 REMOVE_FROM_CART_EVENT,_27 type StoryAddToCartDetail,_27 type StoryRemoveFromCartDetail,_27} from "@vtex/ads-stories";_27_27function addToMyCart({ sku, seller, quantity }: StoryAddToCartDetail) {_27 // ..._27}_27_27function removeFromMyCart({_27 sku,_27 seller,_27 quantity,_27}: StoryRemoveFromCartDetail) {_27 // ..._27}_27_27// `event` is a StoryAddToCartEvent: no cast needed._27document.addEventListener(ADD_TO_CART_EVENT, (event) => {_27 addToMyCart(event.detail);_27});_27_27document.addEventListener(REMOVE_FROM_CART_EVENT, (event) => {_27 removeFromMyCart(event.detail);_27});
The CDN script doesn't include the commerce event names. If you load the element from a CDN, use the strings
"vtex-ads-story:add-to-cart"and"vtex-ads-story:remove-from-cart". See CDN.
Ad events
The element sends each Story's impression, view, and click events to the URLs that the VTEX Ads API returned for that Story (impression_url, view_url, and click_url). Your store doesn't need to send them.
A store that calls the VTEX Ads API without the element sends these events itself. See Integrating Story ads.
The impression and view rules on this page apply to Story ads only. They differ from the rules for other ad formats, which are described in the Impressions and Views guides and continue to apply to those formats.
For the endpoints that receive these events, see POST Track ad impressions, POST Track ad views, and POST Track ad clicks in the API Reference.
If your store modifies the element's code, your store becomes responsible for keeping all the rules in this section. They include the impression, view, and click rules and the rules for how the events are sent.
Impression
The element sends a Story's impression when at least 30% of the Story's card is on screen, after the loading skeleton leaves.
- Only the Stories that the shopper can see send an impression. Rendering the carousel doesn't send one for every Story.
- A card that is scrolled out of the carousel or is below the fold sends its impression only when it comes into view.
- The element sends nothing while the skeleton is displayed.
View
The element sends a Story's view when the video of a card, or of the open player, plays for 2 continuous seconds while at least 50% of the video is on screen and the page is visible.
The following don't count as a view:
- A card that shows only its cover.
- A paused video.
- Cards behind the expanded player.
Click
The element sends a Story's click on the first touch on either of the following:
- A card: Its product, call to action, and sound button are included.
- The player's video: Its play and sound buttons, product, and call to action are included.
The previous and next buttons and the buttons that close or expand the player don't count as a click.
Browsers without IntersectionObserver
Browsers without IntersectionObserver can't tell what is on screen. In these browsers, the element sends the impression of every card shown, after the skeleton leaves, and it doesn't send views.
How the element sends the events
The element sends each event at most once per Story and session-id.
Each event is a POST request with a JSON body that contains session_id and, when the user-id attribute is set, user_id. Neither ID should be personal data. For the rules that apply to both IDs, see Session and user IDs. To learn how VTEX Ads uses session_id and user_id in ad events, see Understanding ads events.
The element sends the request with navigator.sendBeacon, or with a fetch request with keepalive when the browser can't queue the beacon, so the event isn't lost when the shopper leaves the page. A failed fetch request is logged to the console and isn't sent again.
The VTEX Ads API also deduplicates the events by session.
Next steps
- VTEX Ads Stories Package Layout and Player: Understand how the player, the product panel, and the cart stepper behave.
- VTEX Ads Stories Package Configuration: Review the rules for the
session-idanduser-idattributes that the events carry. - Understanding ads events: Learn how VTEX Ads identifies sessions and users in ad events.