Skip to main content

Cart analytics

The cart analytics functions publish analytics events when the customer's cart changes. Use these functions to send cart updated, product added to cart, and product removed from cart events to your analytics destinations through the analytics bus.

In React and Vue apps, call useCartAnalytics inside the CartProvider from createCartComponents. In other frameworks, pass a cart store to trackCartAnalytics. In Vue, useCartAnalytics starts tracking when the component mounts and stops when the component unmounts.

Import trackCartAnalytics from @shopify/hydrogen, or import useCartAnalytics from @shopify/hydrogen/react or @shopify/hydrogen/vue.

Render ShopifyScripts before tracking starts. Tracking needs the analytics bus that ShopifyScripts creates.


Publishes cart analytics events when the server confirms a change to the customer's cart. In React, call useCartAnalytics.

The function publishes cart_updated when the cart's update time changes, plus product_added_to_cart or product_removed_from_cart for each added line, removed line, and quantity change. The function waits until no cart change or reload is in flight. Analytics payloads leave out cart lines without merchandise.

The function stores the last published cart update in local storage under cartLastUpdatedAt to avoid repeat events across page loads and tabs. A store that you fill from your own cart query must select updatedAt. Without the update time, the function publishes on every confirmed cart state. The function also sets window.Shopify.currency.active to the cart's currency.

The function throws when the Shopify analytics bus isn't available, including during server rendering. Render ShopifyScripts, then start tracking in a client-only effect, such as useEffect or onMounted.

Anchor to store
store
required

The cart store to track.

() => void

A function that stops tracking.

Examples

TypeScript

import { configureCartEndpoint, createCartStore, trackCartAnalytics } from "@shopify/hydrogen";

const store = createCartStore();

export function startCart() {
configureCartEndpoint("/api/cart");
store.connect();

const stopTracking = trackCartAnalytics(store);

return () => {
stopTracking();
store.destroy();
};
}

Anchor to useCartAnalytics
useCartAnalytics()

Publishes cart analytics events when the customer's cart changes.

Call the hook once near the root of your app, inside CartProvider. The hook starts tracking on mount and stops on unmount. The hook publishes cart_updated, product_added_to_cart, and product_removed_from_cart events.

The hook throws outside CartProvider, and when the Shopify analytics bus isn't available. Render ShopifyScripts before the hook runs.

Nothing. The hook tracks cart analytics for the lifetime of the calling component.

Examples

React

import { createCartComponents, useCartAnalytics } from "@shopify/hydrogen/react";
import type { ReactNode } from "react";

const { CartProvider } = createCartComponents();

function CartAnalyticsTracker() {
useCartAnalytics();
return null;
}

export function Providers({ children }: { children: ReactNode }) {
return (
<CartProvider>
<CartAnalyticsTracker />
{children}
</CartProvider>
);
}

Was this page helpful?