---
title: Analytics event tracking with Hydrogen
description: >-
  Learn how to set up analytics with Hydrogen and Oxygen, and transmit metrics
  events to Shopify analytics.
source_url:
  html: >-
    https://shopify.dev/docs/storefronts/headless/legacy/hydrogen/analytics/tracking?framework=hydrogen&extension=javascript
  md: >-
    https://shopify.dev/docs/storefronts/headless/legacy/hydrogen/analytics/tracking.md?framework=hydrogen&extension=javascript
api_name: hydrogen
---

Legacy Hydrogen

These pages refer to Hydrogen versions 2026-04 and earlier. For versions 2026-10 and later, refer to the latest [Hydrogen docs](https://shopify.dev/docs/storefronts/hydrogen). If you're not using the latest version of Hydrogen, then we recommend that you migrate your storefront. To learn more, refer to [Migrate to the latest version of Hydrogen](https://shopify.dev/docs/storefronts/hydrogen/migrate).

# Analytics event tracking with Hydrogen

Learn how to implement analytics events in your Hydrogen project and send tracked events to Shopify analytics and, optionally, third-party services.

**Tip:** Hydrogen includes analytics by default as of version 2024.4.3. You only need to do these tasks if you're upgrading an older version.

## Requirements

[Hydrogen channel installed](https://apps.shopify.com/hydrogen)

[Complete “Getting started with Hydrogen and Oxygen”](https://shopify.dev/docs/storefronts/headless/legacy/hydrogen/getting-started)

[Configure Customer Privacy API settings](https://shopify.dev/docs/storefronts/headless/legacy/hydrogen/analytics/consent)

## Project

## Update root.jsx

To start sending analytics events, you need to set up the `Analytics` component in your Hydrogen project's root route.

Import the `Analytics` component and the `getShopAnalytics` utility from Hydrogen.

The `Analytics` component automatically checks for [consent](https://shopify.dev/docs/storefronts/headless/legacy/hydrogen/analytics/consent) before collecting any event data.

Error loading file. Please try again later.



**Caution:** If you haven’t [configured consent](https://shopify.dev/docs/storefronts/headless/legacy/hydrogen/analytics/consent) through the Customer Privacy API, then analytics events won't fire and no data is tracked.

Update the root loader function to destructure the `env` object from the Hydrogen context.

Error loading file. Please try again later.

Return the `shop` object by calling the `getShopAnalytics` utility. This function automatically collects the credentials required to send analytics events to Shopify.

[get​Shop​Analytics](https://shopify.dev/docs/api/hydrogen/2026-04/utilities/getshopanalytics)

Error loading file. Please try again later.

Return the `consent` object with its required values. If you're using Shopify's cookie banner, then make sure `withPrivacyBanner` is set to `true`.

Error loading file. Please try again later.

Update the root component to wrap your Hydrogen app with the Analytics provider component.

Error loading file. Please try again later.



At this point, your app is set up to send analytics events to Shopify, but there aren't any events configured. In the next step, we'll add events to your storefront routes.

## Update routes with analytics subcomponents

To track page views, you need to add pageview components to each route you want to track.

In your product details page route, import the `Analytics` component from Hydrogen.

[Analytics.Provider](https://shopify.dev/docs/api/hydrogen/2026-04/components/analytics/analytics-provider)

Error loading file. Please try again later.

Add the `Analytics.ProductView` component to your nested route. This component sends a pageview event to Shopify when the route is loaded. The `ProductView` subcomponent takes a `data` prop with the product data to send to Shopify.

[Analytics.Product​View](https://shopify.dev/docs/api/hydrogen/2026-04/components/analytics/analytics-productview)

Error loading file. Please try again later.

Repeat this pattern for each route you want to track. Different routes have different preset analytics subcomponents, and require different data payloads.

Follow the same pattern for collections routes...

[Analytics.Collection​View](https://shopify.dev/docs/api/hydrogen/2026-04/components/analytics/analytics-collectionview)

Error loading file. Please try again later.

...the search results route...

[Analytics.Search​View](https://shopify.dev/docs/api/hydrogen/2026-04/components/analytics/analytics-searchview)

Error loading file. Please try again later.

...and the cart route.

[Analytics.Cart​View](https://shopify.dev/docs/api/hydrogen/2026-04/components/analytics/analytics-cartview)

Error loading file. Please try again later.

If your cart is a side panel, you can publish the `cart_viewed` event with `useAnalytics`.

Error loading file. Please try again later.

If you get the following error message in your browser console, make sure your cart query includes the`updatedAt` field.

``[h2:error:CartAnalytics] Can't set up cart analytics events because the `cart.updatedAt` value is missing from your GraphQL cart query. In standard Hydrogen projects, the cart query is contained in `app/lib/fragments.js`. Make sure it includes `cart.updatedAt`.``

Error loading file. Please try again later.

## Optional: Implement custom analytics with third-party services

Using the Analytics provider component, you can create your own subcomponents to send events to third-party analytics services, in addition to Shopify.

In your custom component file, import the `useAnalytics` hook from Hydrogen.

Error loading file. Please try again later.

Export your custom component so it can be used in your Hydrogen routes.

Error loading file. Please try again later.

The `subscribe` function provided by `useAnalytics` listens for standard analytics events from your routes, and provides a callback function with the corresponding data.

[Web Pixels API standard events](https://shopify.dev/docs/api/web-pixels-api/standard-events)

Error loading file. Please try again later.

Register the analytics integration with `<Analytics.Provider>`. This will prevent any analytics events from being sent until this integration is ready.

Error loading file. Please try again later.

For each event you want to track in your custom analytics component, use the subscribe function to listen for the event and send the analytics data to your third-party analytics platform.

This example just logs the event data to the console. It's up to you to replace this with your own custom analytics logic, depending on the platform you're using.

Error loading file. Please try again later.

Notify `<Analytics.Provider>` that this analytics integration is ready to receive events.

Error loading file. Please try again later.

Once you've created your custom analytics component, import it into your root route.

Error loading file. Please try again later.

Add your custom component to your app's root layout. Your component needs to be a child of the `Analytics.Provider` component in order to subscribe to events and receive analytics data.

Error loading file. Please try again later.

Error loading file. Please try again later.

### Next steps

[Validate and troubleshoot Hydrogen analytics\
\
](https://shopify.dev/docs/storefronts/headless/legacy/hydrogen/analytics/validation)

[Test that your analytics are working and check for common errors](https://shopify.dev/docs/storefronts/headless/legacy/hydrogen/analytics/validation)
