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()
get Tracking Values()
Retrieves user session tracking values for analytics and marketing from the browser environment.
TrackingValuesTrackingValues
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
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,
}),
});
}
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, }), }); }
Was this page helpful?