Menu
Guides
API Reference

Guides

VTEX Ads Stories Package Quickstart

Install the @vtex/ads-stories package to render Story ads with the vtex-ads-story-carousel web component on any HTML5 storefront, through npm or a pinned CDN script.

6 min read

The @vtex/ads-stories package is the web component of the VTEX Ads JavaScript SDK for Story ads, a video ad format. Its <vtex-ads-story-carousel> element requests the Stories of a placement from the VTEX Ads API, shows them in a carousel of 9:16 videos that open in a player, and sends the impression, view, and click events. It also dispatches an event to your storefront when a shopper adds a product to or removes a product from the cart.

The element is framework-agnostic. It works on any HTML5 page, including VTEX IO, FastStore, and non-VTEX storefronts. You can load it from npm or, without a build step, from a CDN. The element requests ads through @vtex/ads-core.

To build your own carousel or a native app, call the VTEX Ads API directly instead of using the element. See Retrieving ads and Integrating Story ads.

You can explore the element's variants, attributes, and CSS variables in the Stories Storybook.

Before you begin

Story ads are off by default. Before your store can show them, open a ticket with VTEX Support to request Story enablement for your store. For a network, it's enabled once for the whole network.

After Story is enabled for your store, go to the VTEX Ads platform and complete these steps:

  1. In Settings > Ad types, select Story and save.
  2. In Settings > Advertisers, add Story to the ad types of each advertiser that can run Story campaigns.

After this setup, advertisers can create Story campaigns, which run once the store approves them.

You can install the package and build your integration before the store is enabled. Until the placement has eligible Stories, the element renders nothing.

You also need your VTEX Ads publisher ID, a Story placement name of your choice (for example, home_stories), and the session ID your storefront already uses for VTEX Ads events. Registering the placement on the VTEX Ads platform is optional and lets you track performance by placement.

Using VTEX IO

If your store runs on VTEX IO, you can use the vtex-ads-stories block of the VTEX Ads app (vtex.vtex-ads) instead of installing the package. For the setup and the block props, see the app documentation.

Using FastStore

If your store runs on FastStore 4.7 or later with the CMS, you can use the VTEX Ads Stories section of the @vtex/faststore-plugin-ads plugin instead of installing the package. See Implementing VTEX Ads Stories in FastStore.

Install the package

Choose npm or the CDN script.

npm

Install the package:


_10
npm install @vtex/ads-stories

Register the element once, on the client:


_10
import { defineVtexAdsStoryCarousel } from "@vtex/ads-stories";
_10
_10
defineVtexAdsStoryCarousel();

Importing the package doesn't register anything. The element is registered only when you call defineVtexAdsStoryCarousel(). The package publishes ESM (import) and CommonJS (require) builds with type declarations.

Use this path for FastStore storefronts that don't use the plugin, such as stores on Headless CMS (legacy). For the server-side rendering considerations, see Server-side rendering.

CDN

The same build is available as a standalone script from unpkg and jsDelivr. The script registers <vtex-ads-story-carousel> when it loads, so you don't need any JavaScript in the page.

Always pin an exact version in the URL. A pinned URL never changes, so your storefront doesn't run code you haven't reviewed. Replace {version} with a version published on npm.


_10
<script
_10
src="https://unpkg.com/@vtex/ads-stories@{version}"
_10
integrity="sha384-{hash}"
_10
crossorigin="anonymous"
_10
></script>

Keep in mind the following:

  • The full file URL also works: https://unpkg.com/@vtex/ads-stories@{version}/dist/cdn/vtex-ads-stories.js.

  • jsDelivr serves the same file at https://cdn.jsdelivr.net/npm/@vtex/ads-stories@{version}.

  • The integrity attribute is optional. When you set it, the browser refuses the file if its content isn't exactly the published one. To get the hash for a version, run the following command. It prints the hash only if the download succeeds, so a wrong version shows an error instead of the hash of an error page.


    _10
    curl -fsSLo vtex-ads-stories.js https://unpkg.com/@vtex/ads-stories@{version}/dist/cdn/vtex-ads-stories.js \
    _10
    && openssl dgst -sha384 -binary vtex-ads-stories.js | openssl base64 -A

  • If your store has a Content Security Policy, allow the CDN host (https://unpkg.com or https://cdn.jsdelivr.net) in script-src, in addition to the hosts the element needs to request ads, send events, and load media.

  • The script exposes window.VtexAdsStories, with VtexAdsStoryCarouselElement, defineVtexAdsStoryCarousel, and TAG_NAME. It doesn't include the commerce event names. Use the strings "vtex-ads-story:add-to-cart" and "vtex-ads-story:remove-from-cart" to listen to those events.

Add the element to the page

Place <vtex-ads-story-carousel> where you want the carousel to appear. These attributes are required:

AttributeDescription
publisher-idYour VTEX Ads publisher ID.
session-idThe shopper's session ID, the same opaque value you use for other VTEX Ads events. Don't use personal data such as an email address, phone number, or name.
placementA Story placement name of your choice, for example home_stories.

_10
<vtex-ads-story-carousel
_10
publisher-id="your-publisher-id"
_10
session-id="the-store-session-id"
_10
placement="home_stories"
_10
></vtex-ads-story-carousel>

When you use the CDN, add this element after the <script> tag shown in the previous section.

Check the result

Open the storefront page with the carousel. While the element requests Stories, it shows a loading skeleton. If the placement returns eligible Stories, the skeleton gives way to the carousel. Select the highlighted card to open its video in the player.

If the carousel doesn't appear, check the following.

  1. In the browser console, run customElements.get("vtex-ads-story-carousel"). It returns the element's constructor if registration succeeded. If it returns undefined, check that your npm integration calls defineVtexAdsStoryCarousel() in the browser or, if you use the CDN, that the script loaded.
  2. Inspect the element and confirm that publisher-id, session-id, and placement each have a value.
  3. In the browser's network panel, inspect the ad request. If it succeeds but returns an empty list for your placement, confirm that your store has enabled Story ads and has an approved, eligible Story campaign.
  4. If the request fails, check the browser console for the error. A timeout also leaves the carousel empty, but the element doesn't log it. Compare the request duration with the configured timeout (2 seconds by default).

For details, see Loading and errors.

Server-side rendering

You can import the package during server-side rendering, for example in FastStore, which is built on Next.js. The element is built with Lit, and in Node, Lit installs a minimal DOM shim. After the import, globalThis.customElements is also defined on the server.

Because of that shim, don't use typeof customElements to tell the server from the browser. Use typeof window instead:


_10
import { defineVtexAdsStoryCarousel } from "@vtex/ads-stories";
_10
_10
if (typeof window !== "undefined") {
_10
// Browser-only code
_10
}
_10
_10
defineVtexAdsStoryCarousel();

Calling defineVtexAdsStoryCarousel() on the server is safe. It registers the element in Lit's server-side registry and renders nothing.

TypeScript and React

document.createElement("vtex-ads-story-carousel") and document.querySelector("vtex-ads-story-carousel") are typed as VtexAdsStoryCarouselElement out of the box.

To use <vtex-ads-story-carousel> in .tsx files, enable its JSX types once in your project. This requires @types/react 18.2.6 or newer, including 19.


_10
// In your app's entry file or in a .d.ts file
_10
import type {} from "@vtex/ads-stories/react";
_10
_10
export const Ad = () => (
_10
<vtex-ads-story-carousel
_10
publisher-id="your-publisher-id"
_10
session-id="the-store-session-id"
_10
placement="home_stories"
_10
/>
_10
);

The JSX types include the element's attributes. As in HTML, publisher-id, session-id, and placement are required, and channel accepts only site or app.

Alternatively, add "@vtex/ads-stories/react" to compilerOptions.types in tsconfig.json. Without either option, TypeScript reports Property 'vtex-ads-story-carousel' does not exist on type 'JSX.IntrinsicElements'. The types are opt-in so projects without React don't need @types/react.

Next steps

  • Configuration: Learn about the element's attributes, language, and CSS variables.
  • Layout and player: Understand how Stories are displayed and played.
  • Product hydration: Learn where product prices and availability come from.
  • Events: Handle the cart events in your store and understand the ad events the element sends.
  • Content Security Policy: See the hosts and directives your store needs to allow.
  • Integrating Story ads: Call the VTEX Ads API directly to build your own carousel or a native app.