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

# Storefront events on a headless store

> Send product views, cart adds and searches from a Hydrogen, Next.js or custom Shopify storefront, so that browse and cart automations work there.

TextYess starts browse and cart automations from what shoppers do on your storefront: product views, cart adds, collection views and searches. On a theme store, the TextYess Shopify pixel sends these events. On a headless storefront (Hydrogen, Next.js, or another custom front end), Shopify runs no pixel, so TextYess receives only the checkout events.

The TextYess storefront script closes this gap. Your storefront code tells the script what the shopper does, and the script sends it to TextYess. The checkout keeps the TextYess Shopify pixel. The storefront and the checkout share one browser id, so TextYess knows that the two belong to the same shopper.

This guide is for the developer of the storefront. The setup takes about half a day.

## Before you start

You need your TextYess organization ID. Find it in the [Developers section](https://ai.textyess.com/developers).

## Install the script

**1. Add the script to every page**

Put this code in the `<head>` of every page, for example in the root layout. Replace `YOUR_ORGANIZATION_ID` with your organization ID.

```html theme={null}
<script>
  window.textyess = window.textyess || [];
  window.textyess.push(["init", { organizationId: "YOUR_ORGANIZATION_ID" }]);
</script>
<script async src="https://cdn.textyess.com/v2/textyess-widget-components/storefront-events.js"></script>
```

You can push commands before the script loads. The script runs them when it loads.

> [!NOTE]
>
> If your storefront has a Content Security Policy, add `https://cdn.textyess.com` to `script-src` and `https://pixel.textyess.com` to `connect-src`. In Hydrogen, add them to `createContentSecurityPolicy`. The policy also blocks the inline `init` script. Give it your page's nonce (in Hydrogen, `useNonce()`), or put its two lines in a script file that the policy allows.

**2. Send the shopper's consent**

The script sends nothing and sets no cookie until the shopper allows analytics and marketing.

If your storefront loads Shopify's Customer Privacy API (`window.Shopify.customerPrivacy`), the script reads the consent there. Do nothing more.

If your storefront uses another consent banner, push the shopper's choice to the script. Push it on each page load when the banner has a saved choice. Push it again each time the shopper changes the choice.

```js theme={null}
window.textyess.push(["consent", { analytics: true, marketing: true }]);
```

For iubenda, see [Consent from iubenda](#consent-from-iubenda).

> [!WARNING]
>
> Do not push a "no" before your banner knows the shopper's choice. A "no" that lasts 10 seconds deletes the browser id, and TextYess then forgets which contact the browser belongs to.

**3. Send the events**

Push one command each time the shopper does one of these actions. The next section gives the fields of each command.

```js theme={null}
window.textyess = window.textyess || [];
window.textyess.push([
  "product_viewed",
  {
    productId: product.id,
    variantId: selectedVariant.id,
    productTitle: product.title,
    variantTitle: selectedVariant.title,
    price: selectedVariant.price,
    image: selectedVariant.image,
  },
]);
```

In React, push `product_viewed` from an effect that runs once for each product that the page shows. Push `product_added_to_cart` after the cart accepts the add.

## Commands

| Command | When to push it | Fields |
| - | - | - |
| `product_viewed` | A product page opens. | `productId` (required), `variantId`, `productTitle`, `variantTitle`, `price`, `currency`, `image`, `url` |
| `product_added_to_cart` | The cart accepts an add. | The fields of `product_viewed`, plus `quantity`, `cartTotal` and `cartCurrency` |
| `collection_viewed` | A collection page opens. | `collectionId` (required), `collectionTitle`, `url` |
| `search_submitted` | A page of search results opens. | `query` (required), `resultsCount`, `productIds` (the first three results) |
| `customer` | A shopper logs in. Push `null` after a logout. | `id`, `email`, `phone`, `firstName`, `lastName` |
| `consent` | The banner has the shopper's choice. | `analytics`, `marketing` |

The script accepts the values of the Shopify Storefront API as they are:

* **Ids** are Shopify ids. A Storefront API id (`gid://shopify/Product/123`) and the number (`123`) both work. A SKU or a handle is not a Shopify id: the script ignores the event and writes a warning in the browser console.
* **Prices** are a number, a decimal string (`"19.90"`), or a `MoneyV2` object. Send `price` as the price of one unit. For a cart add, the script sends the price of all the units added, as the Shopify pixel does. When you send a `MoneyV2`, the script reads the currency from it.
* **`image`** is an image URL or an `Image` object.
* **`url`** is the address of the current page when you do not send one.

Some automations need more than the required fields:

* A search starts a search automation only when it has a `resultsCount` of 1 or more and `productIds`.
* A condition on the cart total needs `cartTotal`: the total of the whole cart after the add.

When the last query comes again within 30 minutes, for example another page of results, a sort or a filter, the script does not send it again.

## Consent from iubenda

iubenda keeps one choice for each purpose. Purpose 4 is measurement, which TextYess reads as analytics. Purpose 5 is marketing. iubenda calls its `onPreferenceExpressedOrNotNeeded` callback on each page that has a saved choice, and each time the shopper changes the choice.

Add this code to the `onPreferenceExpressedOrNotNeeded` callback of your iubenda configuration. Keep the code that the callback already has.

```js theme={null}
if (preference) {
  (window.textyess = window.textyess || []).push([
    "consent",
    { analytics: preference.purposes[4] === true, marketing: preference.purposes[5] === true },
  ]);
}
```

If the callback already pushes the purposes to the `dataLayer`, you can do this in Google Tag Manager instead. For example, the callback pushes `{ event: "iubenda_preferences", preferences: preference.purposes }`:

1. Create a Data Layer Variable `DLV - preferences` for `preferences`.
2. Create a Custom HTML tag with the code below. Fire it on a Custom Event trigger with the event name `iubenda_preferences`.

```html theme={null}
<script>
  (function (purposes) {
    if (!purposes) return;
    (window.textyess = window.textyess || []).push([
      "consent",
      { analytics: purposes[4] === true, marketing: purposes[5] === true },
    ]);
  })({{DLV - preferences}});
</script>
```

## Install with Google Tag Manager

If your storefront already pushes GA4 ecommerce events to the `dataLayer`, you can install everything in Google Tag Manager. Each item of these events must carry the Shopify product id. GA4's `item_id` is usually the SKU, which is not a Shopify id.

1. Create a Custom HTML tag with the code of the first step. Fire it on "Initialization - All Pages".
2. Create a Custom HTML tag that pushes the `consent` command. Fire it on the `dataLayer` event of your consent banner. For iubenda, use the tag in [Consent from iubenda](#consent-from-iubenda).
3. Create a Data Layer Variable `DLV - ecommerce` for `ecommerce`, and `DLV - search_term` for `search_term`.
4. Create a Custom HTML tag with the code below. Fire it on a Custom Event trigger with the event name `^(view_item|add_to_cart|search)$` and "Use regex matching".

```html theme={null}
<script>
  (function (event, ecommerce, searchTerm) {
    var item = ecommerce && ecommerce.items && ecommerce.items[0];
    var currency = ecommerce && ecommerce.currency ? String(ecommerce.currency).toUpperCase() : undefined;
    var push = function (command) {
      (window.textyess = window.textyess || []).push(command);
    };
    var product = item && {
      productId: item.shopify_id,
      productTitle: item.item_name,
      variantTitle: item.item_variant,
      price: item.price,
      currency: currency,
    };
    if (event === "view_item" && product) push(["product_viewed", product]);
    if (event === "add_to_cart" && product) {
      product.quantity = item.quantity;
      push(["product_added_to_cart", product]);
    }
    if (event === "search" && searchTerm) push(["search_submitted", { query: searchTerm }]);
  })({{Event}}, {{DLV - ecommerce}}, {{DLV - search_term}});
</script>
```

Replace `item.shopify_id` with the item field that holds the Shopify product id in your `dataLayer`.

GA4's `search` event has no results, and its `add_to_cart` event has no cart total. With this tag, TextYess records the searches and the cart adds, but a search starts no search automation, and a condition on the cart total does not match. If your `dataLayer` has these values, add `resultsCount` and `productIds` to `search_submitted`, and `cartTotal` to `product_added_to_cart`.

## Make sure that it works

1. Open your storefront in a normal browser and accept the consent banner.
2. Open the developer tools of the browser. In the Network tab, filter on `pixel.textyess.com`.
3. Open a product page. Make sure that a `product-viewed` request goes out with the status 200.
4. If no request goes out, read the console. The script writes its warnings with the prefix `[TextYess]`.

> [!NOTE]
>
> TextYess refuses requests from automated browsers, such as headless Chrome. They get the status 403, so do this check in a normal browser.

## What the script does not do

* It sends no checkout events. The TextYess Shopify pixel sends them from the Shopify checkout.
* It sets one cookie, `_textyess_cid`, on the main domain of your store. The checkout pixel uses the same cookie. The script sets it only after the shopper's consent.
* It makes no request to your storefront or to Shopify.


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