Optimize your store’s performance with search tracking
You can measure your storefront’s search performance and improve its relevance by collecting and leveraging customer interaction data.
This guide shows you how to set up search tracking on your custom storefront using the Storefront API and Shopify analytics functionality available in Hydrogen.
Search tracking is only supported in custom storefronts built with either Hydrogen or other React frameworks using the Hydrogen React library.
Anchor to What you’ll learnWhat you’ll learn
In this tutorial, you’ll learn how to do the following tasks:
- Set up Shopify cookies.
- Extract and append search tracking parameters.
- Send analytics to Shopify.
Anchor to RequirementsRequirements
- You've completed the Getting started with the Storefront API guide.
- You’ve learned how to set up Shopify analytics in Hydrogen.
- You’re using the Storefront API version
2023-07
or higher.
To enable search tracking, use Hydrogen’s useShopifyCookies
hook to create and refresh required Shopify cookies. It's up to you to decide how to obtain the value for hasUserConsent
. Without hasUserConsent
set to true
, Shopify cookies are removed.
File
app/root.jsx
import {useShopifyCookies} from '@shopify/hydrogen';
export default function App() {
useShopifyCookies({hasUserConsent: true});
import {useShopifyCookies} from '@shopify/hydrogen';
export default function App() {
useShopifyCookies({hasUserConsent: true});
Learn more about how to set up Shopify analytics.
Shopify cookies are automatically added to Storefront API queries when Hydrogen’s createStorefrontClient
utility function is used.
- For Hydrogen storefronts, follow the Hydrogen documentation.
- For storefronts using other React frameworks and the Hydrogen React library, follow the Hydrogen React documentation.
Anchor to Step 2: Extract and append search tracking parametersStep 2: Extract and append search tracking parameters
You can extract search tracking parameters from the search
and predictiveSearch
responses using the trackingParameters
field. The following examples show how to return the tracking parameters and append them to the search result URLs.
Anchor to Example of ,[object Object]Example of search
search
POST https://{shop}.myshopify.com/api/{api_version}/graphql.json
GraphQL query
JSON response
Anchor to Example of ,[object Object]Example of predictiveSearch
predictiveSearch
POST https://{shop}.myshopify.com/api/{api_version}/graphql.json
GraphQL query
JSON response
Anchor to Example of appending tracking parametersExample of appending tracking parameters
The following example shows how to construct search result URLs using an array of products.
The following example shows how to construct search result URLs using an array of queries.
Anchor to Step 3: Send analytics to ShopifyStep 3: Send analytics to Shopify
Follow the Hydrogen analytics documentation to learn how to send tracking events to Shopify.
Anchor to Next stepsNext steps
- Customize search, filters, and product recommendations using the Shopify Search & Discovery app.