--- gid: 8559f2f8-940b-45e1-a897-9c49798bb285 title: Analytics event tracking with Hydrogen description: Learn how to set up analytics with Hydrogen and Oxygen, and transmit metrics events to Shopify analytics. --- Learn how to implement analytics events in your Hydrogen project and send tracked events to Shopify analytics and, optionally, third-party services. 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. ## Update root.tsxroot.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](/docs/storefronts/headless/hydrogen/analytics/consent) before collecting any event data.
If you haven’t [configured consent](/docs/storefronts/headless/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. Return the `shop` object by calling the `getShopAnalytics` utility. This function automatically collects the credentials required to send analytics events to Shopify. - [getShopAnalytics](/docs/api/hydrogen/current/utilities/getshopanalytics) Return the `consent` object with its required values. If you're using Shopify's cookie banner, then make sure `withPrivacyBanner` is set to `true`. Update the root component to wrap your Hydrogen app with the Analytics provider component.
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](/docs/api/hydrogen/current/components/analytics/analytics-provider) 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.ProductView](/docs/api/hydrogen/current/components/analytics/analytics-productview) 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.CollectionView](/docs/api/hydrogen/current/components/analytics/analytics-collectionview) ...the search results route... - [Analytics.SearchView](/docs/api/hydrogen/current/components/analytics/analytics-searchview) ...and the cart route. - [Analytics.CartView](/docs/api/hydrogen/current/components/analytics/analytics-cartview) If your cart is a side panel, you can publish the `cart_viewed` event with `useAnalytics`. 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`.``` ## 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. Export your custom component so it can be used in your Hydrogen routes. 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](/docs/api/web-pixels-api/standard-events) Register the analytics integration with ``. This will prevent any analytics events from being sent until this integration is ready. 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. Notify `` that this analytics integration is ready to receive events. Once you've created your custom analytics component, import it into your root route. 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.
### Next steps #### Validate and troubleshoot Hydrogen analytics Test that your analytics are working and check for common errors