Skip to main content

Analytics events

The AnalyticsEvent object holds the names of the analytics events that Hydrogen supports. Use these constants to publish page, product, collection, cart, and search view events, and to subscribe an analytics destination to specific events.

The cart analytics functions publish the cart events for you.

Import AnalyticsEvent from @shopify/hydrogen.

Render ShopifyScripts before you publish events. The component creates the analytics bus at window.Shopify.analytics.


The names of the analytics events that Hydrogen supports. Pass a value as the event name when you publish an event or subscribe a destination. TypeScript narrows the payload to match the event name.

Anchor to CART_UPDATED
CART_UPDATED
'cart_updated'
required

Cart tracking publishes this event when the cart changes.

Anchor to CART_VIEWED
CART_VIEWED
'cart_viewed'
required

Publish when a customer views the cart. The payload carries the current cart.

Anchor to COLLECTION_VIEWED
COLLECTION_VIEWED
'collection_viewed'
required

Publish when a customer views a collection. The payload identifies the collection by ID and handle.

Anchor to PAGE_VIEWED
PAGE_VIEWED
'page_viewed'
required

Publish when a customer views a page. Hydrogen fills in the current URL when the payload has none.

Anchor to PRODUCT_ADD_TO_CART
PRODUCT_ADD_TO_CART
'product_added_to_cart'
required

Cart tracking publishes this event when the customer adds a line or raises a line's quantity.

Anchor to PRODUCT_REMOVED_FROM_CART
PRODUCT_REMOVED_FROM_CART
'product_removed_from_cart'
required

Cart tracking publishes this event when the customer removes a line or lowers a line's quantity.

Anchor to PRODUCT_VIEWED
PRODUCT_VIEWED
'product_viewed'
required

Publish when a customer views a product. The payload lists the viewed products.

Anchor to SEARCH_VIEWED
SEARCH_VIEWED
'search_viewed'
required

Publish when a customer views search results. The payload carries the search term.

Examples

TypeScript

import { AnalyticsEvent } from "@shopify/hydrogen";

export function publishPageView() {
window.Shopify?.analytics?.publish(AnalyticsEvent.PAGE_VIEWED);
}

export function publishCollectionView(collection: { id: string; handle: string }) {
window.Shopify?.analytics?.publish(AnalyticsEvent.COLLECTION_VIEWED, {
collection: { id: collection.id, handle: collection.handle },
});
}

A destination callback receives the payload for the event that the destination subscribes to. Every view event payload accepts an optional page URL, shop, and custom data. When a payload leaves out the URL or shop, the analytics bus adds the current page URL and the shop that you pass to ShopifyScripts.

Every payload includes BasePayload, and every view event payload also includes UrlPayload. The table lists the payload type for each event and the fields that the payload adds.

EventPayload
PAGE_VIEWEDPageViewPayload adds no fields.
PRODUCT_VIEWEDProductViewPayload adds ProductsPayload, the products that the customer views.
COLLECTION_VIEWEDCollectionViewPayload adds CollectionPayload, the ID and handle of the collection that the customer views.
CART_VIEWEDCartViewPayload adds CartPayload, the current cart.
SEARCH_VIEWEDSearchViewPayload adds SearchPayload, the customer's search term and optional search results.
CART_UPDATEDCartUpdatePayload adds CartChangePayload, the current cart and the cart before the change.
PRODUCT_ADD_TO_CART, PRODUCT_REMOVED_FROM_CARTCartLineUpdatePayload adds CartChangePayload and CartLinePayload, the line before and after the change.

For a new line, the payload has only currentLine. For a removed line, the payload has only prevLine. For a quantity change, the payload has prevLine and currentLine.

Cart update and cart line payloads also carry OtherData, which holds extra keys. Cart tracking adds eventTimestamp, the publish time in milliseconds.

The shop and custom data that every analytics event payload carries.

Anchor to customData
customData
Record<string, unknown>

Custom values for destinations. Cart tracking copies the bus configuration's custom data here.

| null

The shop and sales channel. The bus fills in the configured shop when the payload omits the key, and normalizes the shop ID to a Shop GID.

The page URL that every view event payload carries.

string

The page URL. For view events, the bus fills in the current URL when the payload omits it.

The products in a product_viewed event.

Anchor to products
products
Array< & >
required

The products that the customer views.

The collection in a collection_viewed event.

Anchor to collection
collection
{ id: string; handle: string; }
required

The ID and handle of the collection that the customer views.

The current cart in a cart event.

| null
required

The current cart.

The search term and results in a search_viewed event.

Anchor to searchTerm
searchTerm
string
required

The customer's search query.

Anchor to searchResults
searchResults
unknown

The search results, in any shape that your destinations read.

The current cart and the cart before the change, in a cart update or cart line event.

| null
required

The current cart.

Anchor to prevCart
prevCart
| null
required

The cart before the change, or null when cart tracking has no earlier snapshot.

The cart line before and after the change, in a product_added_to_cart or product_removed_from_cart event.

Anchor to currentLine
currentLine

The line after the change.

Anchor to prevLine
prevLine

The line before the change.

Examples

TypeScript

import { AnalyticsEvent } from "@shopify/hydrogen";

export function addConsoleDestination() {
return window.Shopify?.analytics?.addDestination({
name: "console-logger",
setup({ subscribe }) {
return subscribe(AnalyticsEvent.PRODUCT_ADD_TO_CART, (payload) => {
console.log("[analytics] product_added_to_cart", payload.currentLine);
});
},
});
}

Was this page helpful?