# Tracking Rebuy's Smart Cart in Google Analytics

One of [Rebuy's](https://www.rebuyengine.com/) most popular products is the [Smart Cart](https://www.rebuyengine.com/product/smart-cart), which adds upsell and promotional potential to Shopify.

If your store uses the Smart Cart here's a simple way to track who is interacting with the cart and, in conjunction with Littledata's [Google Analytics tracking for Shopify](broken://pages/BJnSNIXZTnuy6ncEONuJ), how this leads to purchasing.

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

## Tracking Rebuy Smart Cart

Using the integration below you can move beyond Rebuy's own analytics to track interactions with the Smart Cart.

In Google Analytics you can use the events tracked to:

1. See how many users open the Smart Cart
2. See if users who switch to subscription go on to checkout that subsciption product
3. See if users who selected an upsell in the Smart Cart have a higher Average Order Value
4. See how many products sold came from quantity increases in the Smart Cart

## Events tracked

After added the script below to your store, you will see these useful events to Google Analytics:

| Google Analytics Event Name                | Action                                         |
| ------------------------------------------ | ---------------------------------------------- |
| `rebuy_smartcart_shown`                    | Smart Cart is shown / opened                   |
| `rebuy_smartcart_hidden`                   | Smart Cart is hidden / closed                  |
| `rebuy_smartcart_item_increase`            | An item quantity has been increased            |
| `rebuy_smartcart_item_decrease`            | An item quantity has been decreased            |
| `rebuy_smartcart_item_removed`             | A customer removes an item from the Smart Cart |
| `rebuy_smartcart_switched_to_subscription` | An item is switched to subscription            |
| `rebuy_smartcart_switched_to_onetime`      | An item is switched to one-time                |

{% hint style="success" %}
Adds to Cart and Remove from Cart that happen outside of the Smart Cart are already tracked by [Littledata tracking for Shopify](broken://pages/BJnSNIXZTnuy6ncEONuJ).
{% endhint %}

You'll be able to see those events using a Google Analytics debugger on the browser:

![Rebuy events in Google Analytics](/files/SbXg97XJ0BtaLsZTzXA0)

## Using the events in Google Analytics

You'll be able to see chat events in the realtime reports for testing, and under **Reports > Engagement > Events** within 24 hours.

If you want to see how many people who viewed the Smart Cart went on to purchase you can build a customer segment of those that have a `rebuy_smartcart_shown` event and compare with those users which did not.

Or look at the volume of events received and whether a certain volume of messaging drives purchasing.

## Adding the script

{% hint style="info" %}
You'll need developer access to the Shopify theme to add this integration currently. Please get in touch if you need help implementing this.
{% endhint %}

You need to paste this script anywhere into the Shopify `theme.liquid` file, or assets loaded from the theme. It [listens for the Smart Cart events](https://developers.rebuyengine.com/reference/smart-cart-event-listeners), and passes those events onto Google Analytics using the `gtag` library.

If the `gtag` library has not yet loaded the events will be queued until Google Analytics is ready.

```html
<script>
	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 = {
					...params,
					sku,
					product_id,
					variant_id,
					product_title,
				};
			}
			gtag && gtag("event", event.ga, params);
		});
	});
</script>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.littledata.io/partner-recipes/rebuy/tracking-rebuy-smartcart.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
