> For the complete documentation index, see [llms.txt](https://help.littledata.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.littledata.io/partner-recipes/rebuy/tracking-rebuy-smartcart.md).

# Track Rebuy Smart Cart in GA4

Add a short script to your Shopify theme to send Rebuy Smart Cart interactions to GA4, so you can see how the cart affects purchasing.

[Rebuy's](https://www.rebuyengine.com/) [Smart Cart](https://www.rebuyengine.com/product/smart-cart) adds upsells and promotions to your Shopify cart. This page shows you how to send Smart Cart interactions to Google Analytics 4 (GA4). Together with Littledata's [Shopify tracking in GA4](https://help.littledata.io/integrations/shopify-to-google-analytics), you can see how those interactions lead to purchases.

{% embed url="<https://www.loom.com/share/c3fc68810fa64f50acdde082db3e7db7?sid=791b3ed8-6f20-4d77-a94d-47d6e12367d8>" %}

### What you can learn

With Smart Cart events in GA4, you can:

* See how many users open the Smart Cart.
* See if users who switch an item to a subscription go on to buy that subscription.
* See if users who pick an upsell in the Smart Cart have a higher average order value.
* See how many products sold came from quantity increases in the Smart Cart.

### Events tracked

Once you add the script below, these events are sent to GA4:

| GA4 event name                             | When it fires                         |
| ------------------------------------------ | ------------------------------------- |
| `rebuy_smartcart_shown`                    | The Smart Cart is opened              |
| `rebuy_smartcart_hidden`                   | The Smart Cart is closed              |
| `rebuy_smartcart_item_increase`            | An item quantity is increased         |
| `rebuy_smartcart_item_decrease`            | An item quantity is decreased         |
| `rebuy_smartcart_item_removed`             | A customer removes an item            |
| `rebuy_smartcart_switched_to_subscription` | An item is switched to a subscription |
| `rebuy_smartcart_switched_to_onetime`      | An item is switched to one-time       |

Events about a cart item also include `sku`, `product_id`, `variant_id` and `product_title`.

{% hint style="success" %}
Littledata already tracks add-to-cart and remove-from-cart events that happen outside the Smart Cart, as part of its [Shopify tracking in GA4](https://help.littledata.io/integrations/shopify-to-google-analytics).
{% endhint %}

### Add the script

{% hint style="info" %}
You need access to edit your Shopify theme code. Contact Littledata support if you need help.
{% endhint %}

The script [listens for Smart Cart events](https://developers.rebuyengine.com/reference/smart-cart-event-listeners) and sends each one to GA4 using `gtag`. It uses the `gtag` function that Littledata's tracking script sets up on your storefront. If the Google tag library hasn't finished loading, `gtag` queues the events and sends them once it's ready.

{% stepper %}
{% step %}
**Open your theme code**

In your Shopify admin, go to **Online Store** > **Themes**. Next to your current theme, click **...** > **Edit code**.
{% endstep %}

{% step %}
**Paste the script into theme.liquid**

Open `layout/theme.liquid` and paste this script just before the closing `</body>` tag. You can also add it to a JavaScript asset that your theme loads.

```html
<script>
  (function () {
    const eventsToTrack = [
      { rebuy: "rebuy:smartcart.show", ga: "rebuy_smartcart_shown" },
      { rebuy: "rebuy:smartcart.hide", ga: "rebuy_smartcart_hidden" },
      { rebuy: "rebuy:smartcart.line-item-increase", ga: "rebuy_smartcart_item_increase" },
      { rebuy: "rebuy:smartcart.line-item-decrease", ga: "rebuy_smartcart_item_decrease" },
      { rebuy: "rebuy:smartcart.line-item-removed", ga: "rebuy_smartcart_item_removed" },
      { rebuy: "rebuy:smartcart.item-switch-to-subscription", ga: "rebuy_smartcart_switched_to_subscription" },
      { rebuy: "rebuy:smartcart.item-switch-to-one-time", ga: "rebuy_smartcart_switched_to_onetime" },
    ];

    eventsToTrack.forEach(function (event) {
      document.addEventListener(event.rebuy, function (props) {
        let params = {};
        const item = props?.detail?.item;

        if (item) {
          const { sku, product_id, variant_id, product_title } = item;
          params = { sku, product_id, variant_id, product_title };
        }

        if (typeof window.gtag === "function") {
          window.gtag("event", event.ga, params);
        }
      });
    });
  })();
</script>
```

{% endstep %}

{% step %}
**Save and test**

Click **Save**. Open your store, add a product, open the Smart Cart and change the quantity. You should see the events in a browser debugger for GA4, or in GA4 **Realtime** reports.

<div data-with-frame="true"><figure><img src="https://3956158252-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1QhTbrMraNWciQeDvu1b%2Fuploads%2Fgit-blob-f7a7a392698315d3c2dc2b68edbaef5f4cdb42bb%2Frebuytracking.png?alt=media" alt="Rebuy Smart Cart on a Shopify store next to a GA4 debugger showing rebuy_smartcart events"><figcaption></figcaption></figure></div>
{% endstep %}
{% endstepper %}

### Use the events in GA4

Within 24 hours, the events show up under **Reports** > **Engagement** > **Events**.

To see how many people who opened the Smart Cart went on to buy, build a [segment](https://help.littledata.io/google-analytics/behavior/segments-in-google-analytics-4) of users with a `rebuy_smartcart_shown` event and compare it with users who didn't have one. You can also check whether more Smart Cart interactions lead to more purchases.

### Related articles

* [Overview: Rebuy integration](https://help.littledata.io/partner-recipes/rebuy/overview-integrating-littledata-with-rebuy)
* [Segments in GA4](https://help.littledata.io/google-analytics/behavior/segments-in-google-analytics-4)
* [Shopify to GA4 connection](https://help.littledata.io/integrations/shopify-to-google-analytics)
