Number of Site Search Results in Google Analytics

WordPress site search sucks out of the box, but with a few improvements, it can end up rendering some fairly decent result sets.

The usual method most people use to track their WordPress site search is the WordPress Search Meter Plugin. This is an easy way to get some feedback on how your users are utilizing your site search, but don’t you know that Google Analytics can track site search for you?

As great as Search Meter is, Google Analytics is the ideal place to track your onsite search. Not only can you capture the search phrases used, but I will show you how to capture the number of results in the search result set. This will let you identify all the null search terms used on your site and also see the number of results for popular phrases, and which phrases may be returning too many results.

The first step to tracking site search in Google Analytics is to edit your website profile information and and select Do Track Site Search, and put in s as the Query Parameter.

configure site search

The Query Parameter input is the only thing necessary for Google Analytics to start tracking your WordPress search results, but notice we also added a Category Parameter called numResults and set that parameter to be stripped out from the URL.

Next we are going to add a javascript variable near the top of the page in the global header template.

<script type="text/javascript">
var pageType =  "default";

Then we are going to edit the template code where the search results are displayed. Some themes have a search results template, some display search results from a main template, but this code block should work inserted into either.

<?php if (is_search()) {
$numResults = $wp_query->found_posts; ?>
<script type="text/javascript">
pageType = "search results";
var numResults = <?php echo $numResults ?>;
var searchString = '<?php the_search_query() ?>';
</script> <?php } ?>

The outputted javascript should be 3 variables, pageType, numResults and searchString (We don’t use searchString in this example, but I wanted to show you all how to make it visible in JS).

Finally, you need to edit your Google Analytics code usually placed in your footer template. Make sure you don’t paste this in verbatim, as you need to maintain your unique GA id.

We are going to add a switch statement, and send a different request if the page is a search results page.

<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-99999-1");
switch (pageType) {
case 'search results':
pageTracker._trackPageview(document.location.href.toLowerCase() + "&numResults=" + numResults);
} catch(err) {}

This bit of javascript appends the numResults variable to the pageview in Google Analytics, which Google Analytics then interprets as a Search Category. It also forces the URL and search string to lowercase, which normalizes search phrases with different cases.

Once Google Analytics starts reporting on your search results, you can break down search terms by number of results or number of results by search terms.

site search categories

You can then click on the different number of results to see the search terms used. In a future post, I’ll show you how you can use this same GA switch statement for 404 page tracking.

Be Sociable, Share!

Speak Your Mind