Sub topic 6
Site Search and Query Parameter Settings
This chapter covers the site search option in enhanced measurement. You will learn what it records, how GA4 finds the search term, and how to configure and check it. It also covers the cases where it fails.
It assumes you already have a GA4 web data stream with the Google tag installed, and that you know how events and parameters work. You should also be able to find the data stream settings in Admin, and read basic HTML and JavaScript.
What the site search event records
When site search is on, GA4 watches page loads on your site. If the URL of a loaded page contains a query parameter that you named in the settings, GA4 treats that page as a search results page. It then sends an event called view_search_results.
The event carries these parameters:
| Parameter | Meaning |
|---|---|
| `search_term` | The value of the query parameter that matched, for example `blue shoes`. |
| `unique_search_term` | A value of 1 the first time a given term is seen in a session. Later events with the same term do not carry it. |
| Standard page parameters | `page_location`, `page_referrer`, `page_title`, and so on. |
The search_term value is what the Search term dimension and related reports are built on. In Explorations you can also use the Search term dimension.
Site search is the only enhanced measurement event that depends on a setting you must supply. Scroll, outbound clicks and downloads work from fixed rules. Site search needs to know which parameter name your site uses, and it cannot guess it.
How the detection works
The logic is simple, and knowing it explains most problems.
- A page loads, or the history changes (if page changes based on browser history events is on).
- GA4 reads the URL query string.
- It looks for any parameter whose name matches your list.
- If one matches and has a value, it sends
view_search_resultswith that value assearch_term.
The event is tied to the URL. GA4 does not look at your search box, your results list, or what the user typed. If the URL does not change, nothing happens.
Configuring site search
You set this up in the data stream.
- In Admin, open Data collection and modification, then Data streams, and select your web stream.
- Under Events, find Enhanced measurement and select the gear icon.
- Make sure Site search is on. Open its advanced settings by selecting Show advanced settings or the gear next to the toggle.
- In Query parameter, enter the parameter names, separated by commas. Do not include the
?or=. - Optionally, turn on Use these query parameters for site search options such as removing the parameter from the page URL, if your interface shows them. Options can change over time, so check what your property offers.
- Save.
The default list is q,s,search,query,keyword. If your site uses one of these names, search tracking may already work without any change. If not, add yours.
Finding your parameter name
Run a search on your own site and look at the address bar.
| Search results URL | Parameter to enter |
|---|---|
| `https://example.com/search?q=boots` | `q` |
| `https://example.com/?s=boots` | `s` |
| `https://example.com/catalog?keyword=boots&sort=price` | `keyword` |
| `https://example.com/search/boots` | None. The term is in the path, not a query string. |
| `https://example.com/results#term=boots` | None. The term is in the fragment, which is never sent as a query string. |
A comma-separated list is fine when you have more than one search form. For example, enter q,keyword if the blog uses q and the shop uses keyword.
Parameter names are case sensitive in practice
Treat names as exact. If your site produces Query=boots but you entered query, do not assume it matches. Check in DebugView, and enter the exact spelling that appears in the URL.
A worked example
Suppose a shop uses this results URL:
https://shop.example.com/find?term=trail+shoes&page=1The parameter name is term, so you would add term to the list. After you save, a page load of that URL sends an event like this:
| Field | Value |
|---|---|
| Event name | `view_search_results` |
| `search_term` | `trail shoes` |
| `page_location` | `https://shop.example.com/find?term=trail+shoes&page=1` |
Note that the + becomes a space in the reported value, because GA4 decodes the URL.
The page=1 parameter is ignored for site search. It will still appear in the page location, though, so the same search on page 2 produces a second view_search_results event for the same term. This matters for counts, and the pitfalls section below returns to it.
Making search visible when the URL does not change
Many modern sites run the search through JavaScript and show results without changing the URL. Others use a POST form, so the term never appears in the address. Enhanced site search sees neither.
You have two options.
- Change the site so the term appears in the URL as a query parameter. Often this is a small change, and it also helps users share and bookmark results.
- Send your own event with the search term.
Here is a custom version. It uses the recommended event name search and the parameter search_term, which is the one GA4 already treats as the search term.
const form = document.querySelector('#site-search');
form.addEventListener('submit', () => {
const input = form.querySelector('input[name="q"]');
const term = input.value.trim().toLowerCase();
if (!term) return;
gtag('event', 'search', {
search_term: term
});
});If you add this on top of a working enhanced setup, you may send two search events for the same action. Pick one source. Chapter pitfalls later in this tutorial cover how to find and remove duplicates.
Verifying the setup
Use DebugView or Tag Assistant to check, as for other enhanced events. For site search specifically:
- Make sure debug mode is on. One way is the Google tag configuration below. Remove it when you are done.
- Open Admin, then DebugView.
- On your site, run a search for a known word.
- Look for
view_search_resultsin the stream of events. - Select it, and check that
search_termhas the value you typed.
gtag('config', 'G-XXXXXXXXXX', { debug_mode: true });Replace G-XXXXXXXXXX with your measurement ID. With debug_mode set, the events from your browser appear in DebugView in near real time.
Things to check:
- The event appears once per results page load, not several times.
search_termis not empty and is not URL-encoded (no%20in the value).- Searching the same word twice in a session sends two events, but only the first has
unique_search_term.
Using the data in reports
By default, you can see view_search_results in the Events report, and you can see the term in the Explore tool. In the standard reports, the Search term dimension is available in the Pages and screens or Events reports when you add it as a secondary dimension, depending on how your property is set up.
For anything more detailed, register the parameter as a custom dimension:
- In Admin, open Custom definitions.
- Select Create custom dimension.
- Set the scope to Event.
- Give it a name such as
Search term. - Set the event parameter to
search_term. - Save.
Data is collected only from the moment you register the dimension. Earlier events will not be backfilled. Custom dimensions also count against the property's quota, so check how many you have used.
A site search event can also be marked as a key event if a search is meaningful to your business. For most sites this is not recommended, because a search is an activity and not a result. A key event for a search leading to a purchase is better set up on the purchase.
Limitations and pitfalls
- Search terms in paths and fragments are invisible. Only query string parameters are read.
- Pagination and filter changes can duplicate events. If page 2 of the results includes the same search parameter, it sends another
view_search_results. If a user toggles a filter and the page reloads, you get another one. Theunique_search_termparameter helps you count distinct searches. Counting events withunique_search_termequal to 1 is a closer approximation of the number of distinct searches in a session. - Other parameters collide. The default list includes short names like
sandq. If any other feature on your site usessfor something unrelated, such as a sort order or session ID, you will get false search events. Remove names you do not use. - Personal data can leak. Users sometimes type email addresses, order numbers or names into a search box. These end up in
search_termand inpage_location. Google's policies prohibit sending personally identifiable information to analytics. If your search box may receive this, mask it before it reaches the URL, or do not collect it. - Single-page applications. If the site uses the History API to change the URL without a reload, make sure page changes based on browser history events is enabled. Otherwise GA4 will not see the new URL. If it is on, check for double events, because some frameworks update the URL twice.
- Case differences split reports.
Bootsandbootscan appear as separate rows. Lowercase terms before they reach the URL if you can, or handle this in Looker Studio or BigQuery. - Empty searches. A search with no term usually gives
?q=. GA4 does not send an event when the value is empty, so you cannot measure empty searches this way.
When to use something custom
Choose a custom search event when your site does not put the term in the URL, when you need extra parameters such as the number of results returned, or when you must clean the term before sending it. Stay with enhanced measurement when your URLs already carry the query, and you want the least maintenance.
A common approach is to keep enhanced site search for the term, and add a separate custom event for zero-result searches, with its own name, such as search_no_results. That avoids duplication and gives you information that enhanced measurement cannot see.
Exercise
A site's results page URL is https://example.com/search/red-shoes?sort=price. Enhanced site search is on with the default parameter list, yet no view_search_results events appear in DebugView. Why, and what are two ways to fix it?
Show answer
The search term is in the URL path (/search/red-shoes), and the only query parameter is sort, which is not in the list. GA4 reads only query string parameters. Fix 1: change the site so the term is sent as a query parameter such as ?q=red-shoes. Fix 2: send a custom search event with search_term from the page's JavaScript, and make sure you do not also have enhanced site search active for the same action.
Exercise
Users report that your search count in GA4 is much higher than the count in your site's own search logs. Name two likely causes related to enhanced site search.
Show answer
Likely causes include: results pagination or filter changes that reload the page with the same search parameter, so each load sends view_search_results; a custom search event running alongside enhanced measurement; a configured parameter such as s used by an unrelated feature; and single-page frameworks that update the URL twice. Counting events where unique_search_term is 1 gives a closer number of distinct searches per session.