Google Analytics Site Search: Setup Options

If you want to see your user’s site search activity correctly in your Google Analytics reports, you need to make sure that the Site Search feature has been set up properly. There are a number of ways to set up Google Analytics Site Search. How you do it will depend on the way your website functions and the URLs your search function creates.

site-search-loves-data-blog-v3-994x729.jpg

Site Search Option 1: Query Parameter

Perform a search for, say, ‘analytics’ on your website and look at the URL that results. Does it look like one of the following?

  • http://www.example.com/search?q=analytics
  • http://www.example.com/index.php?id=search&term=analytics
  • http://www.example.com/search.asp?search_term=analytics&id=ac9024

(If your search URLs do not follow this pattern, you’ll need to take different approach – check out option two below.)

Step 1

Firstly, you need to identify your query parameter.

In the example search for ‘analytics’, the resulting URL is ‘http://www.example.com/search?q=analytics’ and the query parameter is ‘q’. Please note that your website’s internal search may return a much longer URL. For example, in ‘http://www.example.com/search.asp?site=example&area=products&term=analytics’, the query parameter is ‘term’.

Step 2

Log into your Google Analytics account and visit the ‘Admin’ tab.

Then find the view you want to configure and click on the ‘View Settings’ option.

Step 3

Scroll down to the ‘Site Search Settings’ area and set ‘Site search Tracking’ to ‘ON’, and enter your query parameter. As some sites have more than one query parameter, Google Analytics allows you to enter up to five of them separated with commas. For example ‘q,query’.

Selecting ‘Strip query parameters out of URL’ will prevent your search from showing up in your content reports. If you leave the option unselected, you’ll see rows in your content reports for each distinct search term used, for example, ‘/search?q=analytics’.

Site Search also allows you to set up categories. This can be used if your internal search has the ability to segment results across different areas of your website. To set up categories, you’ll need to know the parameter that returns results for the different areas of your website. If your search does not have categories, leave this option set to ‘OFF’.

Site Search Option 2: Filter

You will need to take a different approach if your search URLs look like one of the following:

  • http://www.example.com/index/search/analytics
  • http://www.example.com/search.php/keyword/analytics/id946
  • http://www.example.com/searchterm/analytics

These examples include the term that you searched for (‘analytics’), but since there is no query parameter you'll need to setup a filter in order to pass the search term into the Site Search reports.

Possible filter:

If your URL looks like ‘http://www.example.com/index/search/analytics’, you’ll need to set up the following filter:

  • Field A -> Extract A: Request URI: ‘^/index/search/(.*)’
  • Output To -> Constructor: Search Term: ‘$A1’
  • Select ‘Field A required’ and ‘Override Output Field’

If your URL looks like ‘http://www.example.com/search.php/keyword/analytics/id946’ you’ll need to set up the following filter:

  • Field A -> Extract A: Request URI: ‘^/search.php/keyword/(.*)/id’
  • Output To -> Constructor: Search Term: ‘$A1’
  • Select ‘Field A required’ and ‘Override Output Field’

If your URL looks like ‘http://www.example.com/searchterm/analytics’ you will need to set up the following filter:

  • Field A -> Extract A: Request URI: ‘^/searchterm/(.*)’
  • Output To -> Constructor: Search Term: ‘$A1’
  • Select ‘Field A required’ and ‘Override Output Field’

You will start to see the search terms within the Site Search reports after you create the appropriate filter. The same technique can be used to capture search categories.

Site Search Option 3: Implementation

If the resulting URL does not contain the keyword you searched for, then you’ll either need to modify your search form, modify your Google Analytics tracking code, or use Google Tag Manager.

For example, you search for ‘analytics’ and the URL of your search results page looks like:

  • http://www.example.com/search
  • http://www.example.com/index.php?id=search
  • http://www.example.com/search-results.asp

View the source of a page on your site that contains the search input, if your form looks something like this:

<form action="search.php" method="post" name="gs">

Then try changing the method to ‘get’. Your source code should look something like this:

<form action="search.php" method="get" name="gs">

If you now perform a search you should see a URL along the lines of those in ‘Option One’. If you’re unable to modify the formmethod, or the change does not provide the search parameter in the URL, then you can modify the Google Analytics tracking code to capture the search terms people use or you can use Google Tag Manager (see below).

Modifying the Google Analytics tracking code

you will need to modify the Google Analytics tracking code to dynamically create a URL that is correctly structured for Site Search. For example, if you are using the latest ‘Universal Analytics’ version of Google Analytics, your tracking code will look like this:

<script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXX-X', 'auto'); ga('send', 'pageview'); </script>

You just need to modify the ga('send', 'pageview'); code to something like:

analytics.js: ga('send', 'pageview', '/search?q=analytics');

If you are using the slightly older ‘asynchronous’ version of the Google Analytics tracking code, try finding the following line:

_gaq.push(['_trackPageview']);

and changing it slightly to something like this:

_gaq.push(['_trackPageview', '/search?q=analytics']);

The keyword (‘analytics’ in the examples above) will need to be dynamically passed to the tracking code for each term that is searched. Talk to your web developer if you need help achieving this. After making this change, you can then look at ‘Option One′ because you’re creating a virtual pageview for http://www.your-site.com/search?q=analytics

Using Google Tag Manager

If you are using Google Tag Manager, then head over to Simo Ahava's blog where he outlines the options for capturing search terms (even if the search term doesn't appear in the URL).

This is an update to an article first published in 2012.