> ## Documentation Index
> Fetch the complete documentation index at: https://docs.loveful.app/llms.txt
> Use this file to discover all available pages before exploring further.

# React to saves

> Show a toast, update your own UI and send wishlist activity to your analytics from Loveful's storefront events.

Every save, remove and clear fires an event on `window.loveful`, wherever it happened: a product page button, a card heart, a wishlist card, or another tab. This guide uses them for a toast, a badge and analytics.

## 1. Wait for the bus

Your script may run before or after Loveful's (`loveful.js` loads with `defer`). Check for the bus, and wait for `loveful:ready` when it isn't there yet:

```js assets/wishlist-extras.js theme={null}
const onLoveful = (callback) => {
  if (window.loveful) window.loveful.ready.then(() => callback(window.loveful));
  else document.addEventListener("loveful:ready", (e) => callback(e.detail.loveful), { once: true });
};
```

Load your script from `theme.liquid` with `defer`:

```liquid layout/theme.liquid theme={null}
<script src="{{ 'wishlist-extras.js' | asset_url }}" defer></script>
```

## 2. Show a toast on save

```js theme={null}
onLoveful((loveful) => {
  loveful.addEventListener("item_added", (e) => {
    if (e.detail.tab_id !== loveful.tabId) return; // saved in another tab
    const { title } = e.detail.snapshot;
    showToast(title ? `${title} saved to your wishlist` : "Saved to your wishlist");
  });

  loveful.addEventListener("item_removed", (e) => {
    if (e.detail.tab_id !== loveful.tabId) return;
    showToast("Removed from your wishlist");
  });
});

function showToast(message) {
  const toast = document.createElement("div");
  toast.className = "wishlist-toast";
  toast.setAttribute("role", "status");
  toast.textContent = message;
  document.body.append(toast);
  setTimeout(() => toast.remove(), 3000);
}
```

Changes made in the shopper's other tabs are relayed to this one, so check `tab_id` against `loveful.tabId` to react only to changes made here. A card heart's snapshot can have an empty `title`: it's filled from the product as it saves, and stays empty if that request fails.

Loveful's own translations are on the bus too. Use them for wording the merchant already set:

```js theme={null}
showToast(loveful.t("loveful.added_to_wishlist", "Added to your wishlist"));
```

## 3. Update your own badge

For a count Loveful's [`<loveful-count>`](https://docs.loveful.app/developers/web-components#loveful-count) can't render, such as a number inside an SVG or a theme component, read `state.size`:

```js theme={null}
onLoveful((loveful) => {
  const badge = document.querySelector("[data-wishlist-badge]");
  const render = () => {
    badge.textContent = loveful.state.size;
    badge.hidden = loveful.state.size === 0;
  };
  render();
  for (const event of ["item_added", "item_removed", "list_cleared"]) {
    loveful.addEventListener(event, render);
  }
});
```

`state` holds the variant ids on the shopper's default list. To mark something else as saved, such as a custom product card, use `loveful.state.has(variantId)` or `loveful.productSaved(productId)`:

```js theme={null}
onLoveful((loveful) => {
  const mark = () => {
    for (const card of document.querySelectorAll("[data-product-id]")) {
      card.classList.toggle("is-saved", loveful.productSaved(card.dataset.productId));
    }
  };
  mark();
  loveful.addEventListener("item_added", mark);
  loveful.addEventListener("item_removed", mark);
});
```

## 4. Send saves to your analytics

There are two ways in, depending on where your tracking runs.

> [tab] Custom pixel
>
> Loveful publishes [Shopify customer events](https://docs.loveful.app/developers/javascript-api.md#shopify-customer-events), so a custom pixel (**Settings > Customer events** in Shopify admin) can subscribe to them like Shopify's own:
>
> ```js Custom pixel theme={null}
> analytics.subscribe("loveful:item_added", (event) => {
>   const { variant_id, product_id, item_count } = event.customData;
>   gtag("event", "add_to_wishlist", {
>     items: [{ item_id: product_id, item_variant: variant_id }],
>     wishlist_size: item_count,
>   });
> });
> ```
>
> They're published once, in the tab where the change was made, and respect the pixel's customer privacy settings like any other event.

> [tab] Theme script
>
> Where your tracking already runs in the theme (Google Tag Manager's `dataLayer`, for example), listen on the bus:
>
> ```js theme={null}
> onLoveful((loveful) => {
>   loveful.addEventListener("item_added", (e) => {
>     if (e.detail.tab_id !== loveful.tabId) return; // counted in the tab it happened in
>     const { variant_id, snapshot } = e.detail;
>     window.dataLayer?.push({
>       event: "add_to_wishlist",
>       ecommerce: {
>         currency: snapshot.currency,
>         value: Number(snapshot.price) || undefined,
>         items: [{ item_id: snapshot.product_id, item_variant: variant_id, item_name: snapshot.title }],
>       },
>     });
>   });
> });
> ```

> [!NOTE]
>
> Loveful can also send wishlist events to the merchant's GA4, Klaviyo, Meta and TikTok itself (**Settings > Integrations**). Don't track the same save twice.

## 5. Track sharing

`<loveful-share-link>` dispatches bubbling DOM events when the shopper copies, resets or emails their link:

```js theme={null}
document.addEventListener("loveful:share_link_copied", (e) => {
  window.dataLayer?.push({ event: "share", method: "link", content_type: "wishlist" });
});

document.addEventListener("loveful:share_emailed", () => {
  window.dataLayer?.push({ event: "share", method: "email", content_type: "wishlist" });
});
```

## Server side

Storefront events only reach scripts on the page. To act on saves in your own systems (a CRM, a warehouse, Zapier), use [webhooks](https://docs.loveful.app/developers/webhooks.md): signed events for every signed-in shopper's save, remove, share and wishlist purchase.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.