Track cart events in Hydrogen with Shopify analytics

In addition to tracking page views in Hydrogen, you can trigger a Shopify analytics event when a user adds a product to their cart.

While page view events rely on collecting analytics data from Remix loader functions, add-to-cart events, which mutate API state, are tracked in Remix action functions. Consult Hydrogen’s built-in cart component for an example of an Add to Cart button that uses a Remix action.