# Tracking Add-to-Cart and Checkout Events on Shopify with analytics.subscribe

Canonical: https://joinagentready.com/blog/track-add-to-cart-checkout-shopify-analytics-subscribe

[By Dylan Hunt](https://joinagentready.com/authors/dylan-hunt) May 13th, 2026 [How-to notes](https://joinagentready.com/blog/topics/how-to-guides)

Written by the Caffeine and Commerce team. For build work, see [caffeinecommerce.com](https://caffeinecommerce.com/).

Every tracking migration ends at the same question: what exactly do Shopify's customer events give me, and what do I forward to my analytics? This is the reference I wish existed when I started. The funnel events that matter, their real payload shapes, and the GA4 wiring for each.

If you haven't created a pixel yet, start with [adding a custom pixel without an app](https://joinagentready.com/blog/add-custom-pixel-shopify-without-app), then come back. Shopify's own [standard events reference](https://shopify.dev/docs/api/web-pixels-api/standard-events) is the canonical field list; what follows is the practical subset.

## The funnel in four events

```js
analytics.subscribe("product_viewed", (event) => { /* PDP view */ });
analytics.subscribe("product_added_to_cart", (event) => { /* ATC */ });
analytics.subscribe("checkout_started", (event) => { /* begin checkout */ });
analytics.subscribe("checkout_completed", (event) => { /* purchase */ });
```

Every handler receives the same envelope: `event.context` is a snapshot of where it happened (page, navigator, window) and `event.data` is the commerce payload, different per event. You also get `event.timestamp` and `event.id` for deduplication.

## product\_added\_to\_cart

The payload is a `cartLine`:

```js
analytics.subscribe("product_added_to_cart", (event) => {
  const line = event.data.cartLine;

  line.quantity;                          // how many were just added
  line.cost.totalAmount.amount;           // line cost, e.g. "39.98"
  line.cost.totalAmount.currencyCode;     // "USD"
  line.merchandise.product.title;         // parent product
  line.merchandise.title;                 // variant title
  line.merchandise.sku;                   // variant SKU
  line.merchandise.price.amount;          // unit price
});
```

And the GA4 mapping:

```js
gtag("event", "add_to_cart", {
  currency: line.cost.totalAmount.currencyCode,
  value: Number(line.cost.totalAmount.amount),
  items: [{
    item_id: line.merchandise.sku || line.merchandise.product.id,
    item_name: line.merchandise.product.title,
    item_variant: line.merchandise.title,
    quantity: line.quantity,
    price: Number(line.merchandise.price.amount),
  }],
});
```

## checkout\_started and checkout\_completed

Both carry `event.data.checkout`. Same shape, two moments in time:

```js
analytics.subscribe("checkout_completed", (event) => {
  const checkout = event.data.checkout;

  checkout.order?.id;                  // the order, only on completed
  checkout.totalPrice.amount;          // grand total after discounts + tax
  checkout.subtotalPrice.amount;       // before shipping and tax
  checkout.currencyCode;
  checkout.discountApplications;       // applied discounts, with codes
  checkout.lineItems;                  // [{ title, quantity, variant, ... }]
  checkout.email;                      // when provided and consented
});
```

The purchase event, mapped:

```js
analytics.subscribe("checkout_completed", (event) => {
  const checkout = event.data.checkout;
  gtag("event", "purchase", {
    transaction_id: checkout.order?.id,
    currency: checkout.currencyCode,
    value: Number(checkout.totalPrice.amount),
    coupon: checkout.discountApplications?.[0]?.title,
    items: checkout.lineItems.map((item) => ({
      item_id: item.variant?.sku || item.variant?.product?.id,
      item_name: item.title,
      quantity: item.quantity,
      price: Number(item.variant?.price?.amount ?? 0),
    })),
  });
});
```

Two things worth internalizing before you ship this:

-   **`checkout_completed` fires when the thank-you page renders.** A buyer who closes the tab a beat early is a real order your pixel never saw. Browser-side numbers run a little under Shopify's order count for everyone. That's the nature of pixels, not a bug in yours. Treat the Shopify order admin as the source of truth and your analytics as directional.
-   **Consent gates delivery.** If the visitor hasn't granted the consent categories your pixel declared, the events simply don't arrive. Don't burn an afternoon debugging what is actually your privacy settings working as intended.

## Custom events when the standard ones aren't enough

The sandbox can't watch your DOM. For anything Shopify doesn't publish, like size-guide opens or quiz completions, publish from theme code where the interaction happens:

```js
// In the theme (has DOM access):
document.querySelector("#size-guide")?.addEventListener("click", () => {
  Shopify.analytics.publish("size_guide_opened", {
    productHandle: window.location.pathname.split("/").pop(),
  });
});
```

```js
// In the pixel:
analytics.subscribe("size_guide_opened", (event) => {
  gtag("event", "size_guide_opened", {
    product_handle: event.customData?.productHandle,
  });
});
```

That split, theme publishes and pixel subscribes, keeps every outbound network call in one auditable place. The publish API is documented in the [Web Pixels API reference](https://shopify.dev/docs/api/web-pixels-api).

## Verify against reality

After wiring, place a test order and check that three numbers agree: Shopify's order total, the `purchase` event's `value` in your tracker's debug view, and the line-item count. If they match on one real order, your mapping is right. If conversions later drift low by a few percent, re-read the first note above before rewriting anything.

The same structured thinking applies to how AI agents read your store. Events are how you measure; structured data is how you're understood. The [Customer Events migration guide](https://joinagentready.com/blog/migrate-shopify-tracking-to-customer-events-web-pixels) covers the first, and [AgentReady](https://app.joinagentready.com/) covers the second.

Quick answers

## Frequently asked questions

**Which Shopify customer event fires on purchase?**

checkout\_completed. It fires once when the thank-you page renders, and its payload carries the order id, total price, currency, discounts, and full line items. Everything a purchase event needs.

**Why is my add\_to\_cart event not firing?**

The two usual causes: the pixel is saved but not Connected in Settings > Customer events, or the visitor hasn't given the consent your pixel's privacy settings require. Test in a region without consent requirements first, then with consent granted.

**Can I track custom events that Shopify doesn't publish?**

Yes. From theme or app code with DOM access, call Shopify.analytics.publish with your event name and payload, then subscribe to that name in your pixel. The pixel sandbox can't watch the DOM, so publishing is how you bridge into it.

Dylan Hunt

Author

[Dylan Hunt](https://joinagentready.com/authors/dylan-hunt)

Builds Shopify catalog readability and AI answer visibility at AgentReady and Caffeine and Commerce.

[About Dylan →](https://joinagentready.com/authors/dylan-hunt)

## Comments

Every comment here comes from a verified email. Write yours, confirm from your inbox, and it's live.

Loading comments…

Share [X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fjoinagentready.com%2Fblog%2Ftrack-add-to-cart-checkout-shopify-analytics-subscribe&text=Tracking%20Add-to-Cart%20and%20Checkout%20Events%20on%20Shopify%20with%20analytics.subscribe) [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fjoinagentready.com%2Fblog%2Ftrack-add-to-cart-checkout-shopify-analytics-subscribe) [Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fjoinagentready.com%2Fblog%2Ftrack-add-to-cart-checkout-shopify-analytics-subscribe)

Written by [Dylan Hunt](https://joinagentready.com/authors/dylan-hunt), Founder, AgentReady. AgentReady audits Shopify structured data and checks approved changes on the live storefront. [See plans](https://joinagentready.com/pricing).

For Shopify today

### Schema for AI

Make your Shopify catalog easier for AI to understand.

From $9/mo

[Run free audit →](https://joinagentready.com/tools/shopify-ai-readiness-checker) [Explore Schema for AI](https://joinagentready.com/features/structured-data)

Free guide

### The Shopify Functions Playbook

25 real things you can build with Functions, with the plan requirements and limits stated plainly. Emailed to you, readable immediately.

More in How-to notes

-   [Shoppers can now write reviews and add photos from your product page, without a script on first paint.](https://joinagentready.com/blog/shoppers-write-reviews-and-photos-from-product-page)
-   [How to Offer AI Shopping Readiness as a Service to Your Clients](https://joinagentready.com/blog/offer-ai-shopping-readiness-as-a-service)
-   [Rebuy Review: The Smart Cart Platform, Priced and Pulled Apart (2026)](https://joinagentready.com/blog/rebuy-review)
-   [How DTC Brands Cut Subscription Churn with Stay AI](https://joinagentready.com/blog/reduce-subscription-churn-stay-ai)

[See all in How-to notes →](https://joinagentready.com/blog/topics/how-to-guides)
