get Tracking Values
Retrieves user session tracking values for analytics and marketing from the browser environment. It reads from server-timing headers in Storefront API responses and falls back to deprecated cookies for backward compatibility.
Anchor to getTrackingValuesget Tracking Values()
Retrieves user session tracking values for analytics and marketing from the browser environment. Customer Privacy API token readers are authoritative when available, including when consent denies tracking. Deprecated / cookies are read only without an API token reader. Consent comes from CTA's cache or the legacy cookie.
TrackingValues
- consent
Represents the consent given by the user or the default region consent configured in Admin
string - uniqueToken
Identifier for the unique user. Equivalent to the deprecated _shopify_y cookie
string - visitToken
Identifier for the current visit. Equivalent to the deprecated _shopify_s cookie
string
Examples
Example code
Description
I am the default example
JavaScript
import {getTrackingValues} from '@shopify/hydrogen-react'; export function sendCustomAnalyticsEvent(eventName) { const {uniqueToken, visitToken} = getTrackingValues(); // Use tracking values in your custom analytics implementation fetch('/api/analytics', { method: 'POST', body: JSON.stringify({ event: eventName, uniqueToken, visitToken, }), }); }TypeScript
import {getTrackingValues} from '@shopify/hydrogen-react'; export function sendCustomAnalyticsEvent(eventName: string) { const {uniqueToken, visitToken} = getTrackingValues(); // Use tracking values in your custom analytics implementation fetch('/api/analytics', { method: 'POST', body: JSON.stringify({ event: eventName, uniqueToken, visitToken, }), }); }