Sub topic 6
Site Search and Query Parameter Settings
Working model
Assume you already have a GA4 property, a web data stream, and the Google tag installed. You should also understand that GA4 records an event with parameters, where an event describes an interaction and a parameter adds detail about it.
Enhanced measurement can record a site search without custom JavaScript. When a page URL contains a recognized search query parameter, GA4 sends the event viewsearchresults. The event normally includes the search_term parameter.
For example, this URL contains the query parameter q:
https://example.com/search?q=ga4+enhanced+measurement
GA4 reads the value of q and records it as the search term. The parameter name does not have to be q. Common recognized names include:
- q
- s
- search
- query
- keyword
Your search page must place the search term in the URL. A GET form does this automatically:
<form action="/search" method="get">
<label for="search-box">Search this site</label>
<input id="search-box" name="q" type="search">
<button type="submit">Search</button>
</form>Submitting this form creates a URL such as /search?q=ga4.
Configure the query parameter
Open the relevant web data stream:
- Open Admin in GA4.
- Select Data streams under Data collection and modification.
- Select the web stream.
- Open Enhanced measurement.
- Make sure the main setting is on.
- Open the Site search setting.
- Add your site's search parameter if it is not one of the recognized names.
- Save the setting.
For example, a site might use /find?term=ga4 instead of /search?q=ga4. Add term as a search query parameter. GA4 can then use its value for search_term.
| URL pattern | Query parameter | Expected event |
|---|---|---|
| `/search?q=ga4` | q | view_search_results |
| `/find?term=ga4` | term, added in settings | view_search_results |
| `/search` with the term in the page body only | none | No enhanced search event |
Use one consistent parameter when possible. If a site uses several parameter names for the same search function, configure each required name and test them separately. Avoid adding unrelated parameters. A parameter such as campaign or sort may describe the page but does not identify a search term.
Understand page changes and single-page sites
On a traditional site, submitting the form loads a new search-results page. The query string is present during page loading, so GA4 can detect it.
Single-page applications need more testing. They may change the URL with the browser History API without loading a page. Enhanced measurement can observe some history changes, but the result depends on how the application and Google tag are configured. A URL change that does not produce the expected page or history event may not produce viewsearchresults.
The event flow should look like this:
Verify the event
Use Tag Assistant preview or GA4 DebugView while testing. Open the search page with a real query, such as /search?q=analytics. Confirm that:
- viewsearchresults appears.
- search_term contains analytics.
- The event is sent only once for that search.
- The query string does not contain private or sensitive information.
DebugView can show events quickly, but standard reports may take longer to process. Do not use real customer names, account numbers, email addresses, or other personally identifiable information as search terms.
Common errors
A missing search event usually has one of these causes:
- The search form uses POST, so the term is not in the URL.
- The site's parameter, such as term, was not added to the Site search settings.
- The application changes the page without a detectable history or page-view event.
- Another tag or custom script sends a second search event.
- The search term contains sensitive data that should never be sent to Google Analytics.
When enhanced measurement cannot represent the site's search behavior reliably, send a custom event named sitesearch with a carefully reviewed parameter. Use this option only after checking that it will not duplicate viewsearch_results.
Exercise
A site sends users to /results?term=cloud, but DebugView shows no viewsearchresults event. What should you check first?
Show answer
Check that term is added to the Site search query parameter settings, then test the URL in DebugView. Also confirm that the search term is present in the URL rather than sent through a POST request.