Sends analytics to Shopify.
import {
sendShopifyAnalytics,
getClientBrowserParameters,
AnalyticsEventName,
useShopifyCookies,
} from '@shopify/hydrogen-react';
import {useRouter} from 'next/router';
import {useEffect} from 'react';
function sendPageView(analyticsPageData) {
const payload = {
...getClientBrowserParameters(),
...analyticsPageData,
};
sendShopifyAnalytics({
eventName: AnalyticsEventName.PAGE_VIEW,
payload,
});
}
// Hook into your router's page change events to fire this analytics event:
// for example, in NextJS:
const analyticsShopData = {
shopId: 'gid://shopify/Shop/{your-shop-id}',
currency: 'USD',
acceptedLanguage: 'en',
};
export default function App({Component, pageProps}) {
const router = useRouter();
// eslint-disable-next-line no-undef
const hasUserConsent = yourFunctionToDetermineIfUserHasConsent();
// eslint-disable-next-line react-hooks/exhaustive-deps
const analytics = {
hasUserConsent,
...analyticsShopData,
...pageProps.analytics,
};
const pagePropsWithAppAnalytics = {
...pageProps,
analytics,
};
useEffect(() => {
const handleRouteChange = () => {
sendPageView(analytics);
};
router.events.on('routeChangeComplete', handleRouteChange);
return () => {
router.events.off('routeChangeComplete', handleRouteChange);
};
}, [analytics, router.events]);
useShopifyCookies();
return <Component {...pagePropsWithAppAnalytics} />;
}
import * as React from 'react';
import {useEffect} from 'react';
import {
sendShopifyAnalytics,
getClientBrowserParameters,
AnalyticsEventName,
useShopifyCookies,
} from '@shopify/hydrogen-react';
import {useRouter} from 'next/router';
function sendPageView(analyticsPageData) {
const payload = {
...getClientBrowserParameters(),
...analyticsPageData,
};
sendShopifyAnalytics({
eventName: AnalyticsEventName.PAGE_VIEW,
payload,
});
}
// Hook into your router's page change events to fire this analytics event:
// for example, in NextJS:
const analyticsShopData = {
shopId: 'gid://shopify/Shop/{your-shop-id}',
currency: 'USD',
acceptedLanguage: 'en',
};
export default function App({Component, pageProps}) {
const router = useRouter();
// @ts-expect-error - this is an example, you should implement this function
const hasUserConsent = yourFunctionToDetermineIfUserHasConsent();
// eslint-disable-next-line react-hooks/exhaustive-deps
const analytics = {
hasUserConsent,
...analyticsShopData,
...pageProps.analytics,
};
const pagePropsWithAppAnalytics = {
...pageProps,
analytics,
};
useEffect(() => {
const handleRouteChange = () => {
sendPageView(analytics);
};
router.events.on('routeChangeComplete', handleRouteChange);
return () => {
router.events.off('routeChangeComplete', handleRouteChange);
};
}, [analytics, router.events]);
useShopifyCookies();
return <Component {...pagePropsWithAppAnalytics} />;
}
If `event.payload.hasUserConsent` is false, no analytics event will happen.
Set user and session cookies and refresh the expiry time
event: ShopifyAnalytics
shopDomain: string
export function sendShopifyAnalytics( event: ShopifyAnalytics, shopDomain?: string, ): Promise<void> { const {eventName, payload} = event; if (!payload.hasUserConsent) return Promise.resolve(); let events: ShopifyMonorailEvent[] = []; const pageViewPayload = payload as ShopifyPageViewPayload; if (eventName === AnalyticsEventName.PAGE_VIEW) { events = events.concat( trekkiePageView(pageViewPayload), customerPageView(pageViewPayload), ); } else if (eventName === AnalyticsEventName.ADD_TO_CART) { events = events.concat( customerAddToCart(payload as ShopifyAddToCartPayload), ); } else if (eventName === AnalyticsEventName.PAGE_VIEW_2) { events = events.concat( trekkiePageView(pageViewPayload), customerPageView2(pageViewPayload), ); } else if (eventName === AnalyticsEventName.COLLECTION_VIEW) { events = events.concat(customerCollectionView(pageViewPayload)); } else if (eventName === AnalyticsEventName.PRODUCT_VIEW) { events = events.concat(customerProductView(pageViewPayload)); } else if (eventName === AnalyticsEventName.SEARCH_VIEW) { events = events.concat(customerSearchView(pageViewPayload)); } if (events.length) { return sendToShopify(events, shopDomain); } else { return Promise.resolve(); } }
ShopifyPageView | ShopifyAddToCart
Use `AnalyticsEventName.PAGE_VIEW` constant.
Language displayed to buyer.
Result of `customerPrivacyApi.analyticsProcessingAllowed()`
NPM package version of either hydrogen or hydrogen-react. Defaults to hydrogen-react package version.
Canonical url.
Result of `!customerPrivacyApi.saleOfDataAllowed()`
Shopify collection handle.
Shopify collection id.
Currency code.
Shopify customer id in the form of `gid://shopify/Customer/<id>`.
Result of `!(customerPrivacyApi.marketingAllowed() && customerPrivacy.analyticsProcessingAllowed())`
If we have consent from buyer for data collection
Alternative name for Shopify storefront id generated by Hydrogen sales channel. The value of env.PUBLIC_STOREFRONT_ID.
Result of `customerPrivacyApi.marketingAllowed()`
Navigation api: `'PerformanceNavigationTiming' | 'performance.navigation'`. Use `getClientBrowserParameters()` to collect this value.
Navigation type: `'navigate' | 'reload' | 'back_forward' | 'prerender' | 'unknown'`. Use `getClientBrowserParameters()` to collect this value.
Shopify page type.
Value of `window.location.pathname`. Use `getClientBrowserParameters()` to collect this value.
Product list.
Value of `window.document.referrer`. Use `getClientBrowserParameters()` to collect this value.
Shopify resource id in the form of `gid://shopify/<type>/<id>`.
Result of `customerPrivacyApi.saleOfDataAllowed()`
Value of `window.location.search`. Use `getClientBrowserParameters()` to collect this value.
Search term used on a search results page.
Shopify shop id in the form of `gid://shopify/Shop/<id>`.
Shopify sales channel.
Shopify storefront id generated by Hydrogen sales channel. The value of env.PUBLIC_STOREFRONT_ID.
Value of `document.title`. Use `getClientBrowserParameters()` to collect this value.
Total value of products.
Shopify unique user token: Value of `_shopify_y` cookie. Use `getClientBrowserParameters()` to collect this value.
Value of `window.location.href`. Use `getClientBrowserParameters()` to collect this value.
Value of `navigator.userAgent`. Use `getClientBrowserParameters()` to collect this value.
Shopify session token: Value of `_shopify_s` cookie. Use `getClientBrowserParameters()` to collect this value.
Product brand or vendor.
Product category or type.
Product name.
Product price.
Product id in the form of `gid://shopify/Product/<id>`.
Quantity of the product in this event.
Product sku.
Variant id in the form of `gid://shopify/ProductVariant/<id>`.
Variant name.
keyof typeof ShopifySalesChannel
Shopify Headless sales channel
Shopify Hydrogen sales channel
Use `AnalyticsEventName.ADD_TO_CART` constant.
Language displayed to buyer.
Result of `customerPrivacyApi.analyticsProcessingAllowed()`
NPM package version of either hydrogen or hydrogen-react. Defaults to hydrogen-react package version.
Shopify cart id in the form of `gid://shopify/Cart/<id>`.
Result of `!customerPrivacyApi.saleOfDataAllowed()`
Currency code.
Shopify customer id in the form of `gid://shopify/Customer/<id>`.
Result of `!(customerPrivacyApi.marketingAllowed() && customerPrivacy.analyticsProcessingAllowed())`
If we have consent from buyer for data collection
Alternative name for Shopify storefront id generated by Hydrogen sales channel. The value of env.PUBLIC_STOREFRONT_ID.
Result of `customerPrivacyApi.marketingAllowed()`
Navigation api: `'PerformanceNavigationTiming' | 'performance.navigation'`. Use `getClientBrowserParameters()` to collect this value.
Navigation type: `'navigate' | 'reload' | 'back_forward' | 'prerender' | 'unknown'`. Use `getClientBrowserParameters()` to collect this value.
Value of `window.location.pathname`. Use `getClientBrowserParameters()` to collect this value.
Product list.
Value of `window.document.referrer`. Use `getClientBrowserParameters()` to collect this value.
Result of `customerPrivacyApi.saleOfDataAllowed()`
Value of `window.location.search`. Use `getClientBrowserParameters()` to collect this value.
Shopify shop id in the form of `gid://shopify/Shop/<id>`.
Shopify sales channel.
Shopify storefront id generated by Hydrogen sales channel. The value of env.PUBLIC_STOREFRONT_ID.
Value of `document.title`. Use `getClientBrowserParameters()` to collect this value.
Total value of products.
Shopify unique user token: Value of `_shopify_y` cookie. Use `getClientBrowserParameters()` to collect this value.
Value of `window.location.href`. Use `getClientBrowserParameters()` to collect this value.
Value of `navigator.userAgent`. Use `getClientBrowserParameters()` to collect this value.
Shopify session token: Value of `_shopify_s` cookie. Use `getClientBrowserParameters()` to collect this value.
Analytics event names accepted by Shopify analytics.
These duplicated interface declaration is so that we can generate proper documentation for these public facing constants
Add to cart
Page view
Analytics page type values accepted by Shopify analytics.
Analytics sales channel values accepted by Shopify analytics.
Shopify Headless sales channel
Shopify Hydrogen sales channel