JavaScript events

Ignite announces what’s happening on the page, such as items added to the cart or a new variant chosen, so your own code or an app can react without editing the theme’s files.

Updated 3 min read

Where to add your code

Don’t edit the theme’s own JavaScript files. Your changes would be lost when you update. Use one of these instead:

  • No code editing: go to Theme settings > Extras and paste a script into <body> tag code. It’s added to every page and carries over when you update.
  • Larger scripts: create a new file in Edit code > Assets, such as custom.js, and load it from the same setting.
Theme settings > Extras > <body> tag code
<script>
  document.addEventListener('cart-add', (event) => {
    const { variantId, cartData } = event.detail;
    console.log('Added', cartData.product_title, 'variant', variantId);
  });
</script>
Loading your own file
<script src="{{ 'custom.js' | asset_url }}" defer></script>

Events

cart-add

A product is added to the cart from a product form, such as the product page or quick add.

  • variantId: The ID of the variant that was added.
  • cartData: Shopify’s reply to the add request, describing the item that was added.
JavaScript
document.addEventListener('cart-add', (event) => {
  const { variantId, cartData } = event.detail;
  // Your code here
});

cart-update

The quantity of an item changes in the cart or cart drawer, or items are added from a bulk order list.

  • variantId: The ID of the variant that changed, when there is one.
  • cartData: The cart after the change.
JavaScript
document.addEventListener('cart-update', (event) => {
  const { variantId, cartData } = event.detail;
  // Your code here
});

cart-remove

An item is removed from the cart or cart drawer.

  • cartData: The cart after the change.
JavaScript
document.addEventListener('cart-remove', (event) => {
  const { cartData } = event.detail;
  // Your code here
});

cart-error

Adding to the cart fails, for example because the item is out of stock or the quantity is too high.

  • productVariantId: The ID of the variant that couldn’t be added.
  • message: A short error message from Shopify.
  • errors: The full error details.
JavaScript
document.addEventListener('cart-error', (event) => {
  const { productVariantId, message, errors } = event.detail;
  console.error(message, errors);
});

cart-drawer-open

The cart drawer has finished opening.

It doesn’t send any data.

JavaScript
document.addEventListener('cart-drawer-open', () => {
  // Your code here
});

cart-drawer-closed

The cart drawer has finished closing.

It doesn’t send any data.

JavaScript
document.addEventListener('cart-drawer-closed', () => {
  // Your code here
});

variant-change

A different variant is chosen and the page has finished updating, whether the options are dropdowns, buttons or swatches.

  • sectionId: The ID of the section where the variant changed.
  • variant: The full Shopify variant object.
JavaScript
document.addEventListener('variant-change', (event) => {
  const { sectionId, variant } = event.detail;
  // Your code here
});

quick-view-open

The quick add popup has finished opening.

It doesn’t send any data.

JavaScript
document.addEventListener('quick-view-open', () => {
  // Your code here
});

quick-view-closed

The quick add popup has finished closing.

It doesn’t send any data.

JavaScript
document.addEventListener('quick-view-closed', () => {
  // Your code here
});

Feature events

Some features send their own events. Their names include a colon.

EventSent whenData
compare:changeThe product compare list changes.items: the products in the list.
compare:toggleThe compare drawer is shown or hidden.visible: true or false.
compatibility-finder:changeA customer saves or removes a vehicle or device in the Compatibility finder.storageKey: where the list is saved in the browser.

Example: track add to cart

Send an event to your analytics tool whenever something is added to the cart:

JavaScript
document.addEventListener('cart-add', (event) => {
  const { variantId, cartData } = event.detail;

  window.dataLayer = window.dataLayer || [];
  window.dataLayer.push({
    event: 'add_to_cart',
    variant_id: variantId,
    product: cartData.product_title,
    price: cartData.price / 100,
  });
});