Search tracking in GA4 title image

Site search tracking in Google Analytics 4 (GA4)

It is very useful to know what your visitors are searching for on your website, especially if you have an e-commerce store. Let’s take a look at how to set up site search tracking in GA4 and where you can find reports with the most popular keywords.

Default site search tracking

The odds are, if you are using Google Analytics 4 with a default setup, you already might be tracking site search and the keywords used automatically. Here is how you can check this.

Open Reports > Engagement > Events section.

If those events are collected, you should see “search” and “view_search_results” in this report:

Site search events in GA4

You can also double-check this using Real-time reports. Open your website and make a search to see the search results page:

Then in Google Analytics click on “view_search_results” event from the table above. And here under “Events in Last 30 minutes” section you should see number greater than 0.

If you click on the “Parameter name” dropdown and select “search_term”, you should be able to see your searched keyword.

Search term parameter in GA4

If you are seeing this data, your site search should be up and running with default configuration so you can skip to the section where we take a look at how to create keyword report based on those searches.

Missing data? Let’s take a look at default configuration

First of all, default site search will work only if you have your search query parameter in URL.
If you have non-standart search, check this section below.

For example, in my scenario for search results I have this URL – https://ezsegment.com/?s=Data+Studio where ?s= contains my searched query and Google Analytics detects it as a search results page because it’s provided as one of the default parameters.

You can check you search tracking configuration in GA4 by going to the admin section at the bottom of left menu.

Select “Data Streams”

Select your WEB data stream.

Here you need to make sure that Enhanced measurement is enabled, which collects additional events automatically. And then click on the cogwheel icon to check details for additional events.

Here you should make sure that Site Search events are enabled.

If you click on “Show advanced settings” link you will see a list of default parameters that are used to identify when a search was made and to extract used keywords.

You also have an option to provide additional query parameters, that will be also automatically collected in your reports (more on that in the next section).

The main thing to check here is if your search query parameter matches one of the default onesq,s,search,query or keyword. If it’s not in the list just edit this based on your URL.
For example, if my site search had this URL:

https://ezsegment.com/?results=Google+Tag+Manager

Then in Search Term Query parameters, I would provide “results”.

If you did everything right, now from the LIVE tests you should see your search results appearing in the reports.

If your site search is more complex and you have additional parameters available in the search results URL, then GA4 makes it really easy to collect that information as a default parameter.

For example, if you have a search that can be used either for products or for blog articles you might have the additional parameter “category” in the URL.

https://ezsegment.com/?s=Google+Tag+Manager&category=blog

To have this data as an event parameter (dimension) in GA4, you just need to add it under the “Advanced Query Parameters” section.

If then make another search and preview your real-time event data, you should see an additional parameter named “q_category” that will contain dynamic data passed there.

Where to find site search keywords?

As we are limited in capabilities in default reports we will create a custom Explorations report that you can later use for search query monitoring.

Select the Exploration section in the left menu and create a new exploration.

Add 2 dimensions for this report.

  • Search term
  • Event name

Click “Import”

Now let’s add “Event count” as a metric, in the same way as we did for dimensions.

Now in Tab Settings we just need to pick “Search term” for the rows, and “Event count” for the values.

You will see the first site search keywords in the report on the right side.

Currently, it shows us all events, and because only site search-related events contain search terms, we see the majority of those without a value. Let’s fix that by adding a filter.

Scroll down in Tab Settings and select “Event name” from the Filters section. And match it exactly to the “search” event name.

If you click apply, you should now see a proper search term report.

And that is basically how you can view keywords from collected site search tracking data in GA4.
Your created report, as usual, will be always available in the future in the Explorations section, so you can give it a more intuitive name at the top of the Variables section.

Tracking search if you don’t have search query in URL

In some scenarios, you might have a URL structure that doesn’t have any query parameter or your searched keywords are inside the page path (e.g. /search/GoogleTagManager ). If this is the case for you then the default configuration won’t work and you will have to use developer help to get this done.

Adding a dataLayer to track searches

If you are using Google Tag Manager you can implement a small dataLayer code that will mark search events and pass searched keywords.

dataLayer.push({
  'event': 'siteSearch',
  'searchTerm': 'Searched keywords' // replace with actual search query
});

If you aren’t familiar with dataLayer, then here we basically send an event named ‘siteSearch’ that we can hook into Google Tag Manager and pass dynamic value from the ‘searchTerm’ attribute.
I have a separate article about what dataLayer is and how to work with it in GTM.

Setup in Google Tag Manager

When you have that in place, set up in GTM would be very simple – First, we create a GA4 event tag and our event name will be ‘search’ (or you can use any other if you like, just remember to adjust custom report that I’ve shown above).

Under Event parameters, we need o to add ‘searchTerm’ to our event. By default searched key phrases are collected under the search_term parameter (as per Google’s documentation) so we will use that as our parameter name.

For the value click on the “plus” icon and create a new variable in the top right corner (again plus sign icon).

From variable type pick “Data Layer Variable”.

And the variable name will be “searchTerm” since we have used it in our dataLayer code.

Now your event parameters section should look very similar to this:

Now, we just need to add a trigger to enable site search tracking in GA4, so click on “Triggering” and create a new trigger.

From trigger type select “Custom Event”.

And event name will be ‘siteSearch’ because we have used that in the dataLayer code.

Save your trigger and you should have the following tag setup:

You can customize your dataLayer to pass additional information like returned search result count and other additional parameters that you might be interested in, but this basic setup should work for most cases. Now you can publish your changes and check if everything works as expected.

Conclusions

It is relatively easy to track site searches if you have a search parameter in your URL, and in most cases, site search tracking in GA4 is enabled by default. It would be more complicated to set it up when you don’t have a standard URL structure and search queries are not seen in the URL, but hopefully, the approach with dataLayer will guide you in the right direction.

There are some additional data points that you might want to track on your search results pages in the future, for example, the number of results returned, searches without results, clicks on search results, and so on.

Hope you found it useful!

Leave a Reply

Your email address will not be published. Required fields are marked *