Using Google Analytics to Track ‘Zero Results Found’ for Site Searches
Are visitors using site search on your WordPress site? If so, are they finding what they are looking for? In this article I’ll show you how to configure Google Analytics to track and report on the search terms visitors are entering into your site search that return zero results.
The importance of understanding what content is being searched for on your site cannot be understated. Utilizing Google Analytics to ascertain zero result search terms allows you to tailor your content to fulfill visitor requirements. This article assumes you are already using Google Analytics to track your website activity.
Google Analytics contains a full suite of Site Search reports that are categorized under the Content menu. There are a variety of reports that will show the number of visits that result in a site search, which terms are searched for, the most popular destination pages after a search, and a variety of other metrics related to visitor activity and site search. Google Analytics reports all search terms without distinguishing whether or not any results were found.
Note that Site Search Tracking needs to be configured in your Google Analytics Profile before Google Analytics can report on site search data. However, to implement only zero result search reporting as described in this article, it is not required to have Site Search tracking turned on.
When the default WordPress site search finds no results for a searched term, a message will display ‘No posts found. Try a different search?’ The message is customizable so your website may return different wording. This message is generated dynamically within the WordPress search template which is usually located in the themes subdirectory in a file called search.php. You will need to find the source file that contains your message since it will be necessary to add some javascript code at the point where this message is generated. To find the message that your theme displays when no results are found for a search term simply enter a phrase in your site search box that is not included in any of your posts. Then, using a text search utility—possibly included as part of your development tool—find the file that contains this message string.
Example of a zero results found message in the search.php file:
<?php if (have_posts()) : ?>
<!– show posts here –>
:
:
<?php else : ?>
<!– no posts found –>
<h2 class=“center”>No posts found. Try a different search?</h2>
<?php include (TEMPLATEPATH . ‘/searchform.php’); ?>
<?php endif; ?>
To track only site searches that return zero results it is necessary to set-up an event using the Google Analytics Tracking Code (GATC). Using events, Google Analytics is able to record the details for any browser based event. An event is defined as any user activity that does not generate a Pageview , including things like ajax calls, clicking on a user control, as well as when a site search produces zero results. Please keep in mind that, assuming you have the GATC code installed to record Pageviews, the site search itself will be recorded by Google Analytics as a Pageview (or virtual page view depending on your configuration) whether results are found or not. (It is important not to use the GATC _trackPageview method call to record zero search results information as this will inaccurately inflate your Pageview count.)
There are no profile configuration changes required in Google Analytics to track events. It is only necessary to call the _trackEvent method of the GATC. The events will be reported in the Google Analytics Content Menu under Event Tracking.
To summarize Google’s Event Tracking Guide, the _trackEvent method is defined as such:
_trackEvent(category, action, opt_label, opt_value)
- category (required) The name you supply for the group of objects you want to track.
- action (required) A string that is uniquely paired with each category and commonly used to define the type of user interaction for the web object.
- label (optional) An optional string to provide additional dimensions to the event data.
- value (optional) An integer that you can use to provide numerical data about the user event.
To record zero result site searches, the _trackEvent method needs to be called within the same logical condition that displayed the ‘No Posts Found’ message in the search.php file.
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!– no posts found –> <h2 class=”center”>No posts found. Try a different search?</h2> <!– add this code right below the ‘No Post Found’ message –> <script type=“text/javascript”> function gup( name ) { name = name.replace(/[[]/,“\[“).replace(/[]]/,“\]“); var regexS = “[\?&]”+name+“=([^&#]*)”; var regex = new RegExp( regexS ); var results = regex.exec( window.location.href ); if( results == null ) return “”; else return results[1]; } var SearchTerm = gup(‘s’); _gaq.push([‘_trackEvent’, ‘Search’, ‘ZeroResults’, (SearchTerm ? SearchTerm : ‘blank’)]); </script> |
The code listed above does not initialize the GATC. The _trackPageview method should have already been called from the header section of the search page. As so, the _gaq object is still within scope. The _gaq.push method is what registers the event with Google Analytics. In the _gaq call, set the event category to ‘Search’, the action to ‘ZeroResults’, and the label to the search term.
The gup() function is used to parse the value of the ‘s’ parameter from the URL string. WordPress uses ‘s’ as the default search parameter. You can see this in the URL preceding the search term when you perform a site search (?s=silo). If the search parameter is different for your site it will be necessary to modify the call to gup() with the correct value. Credit for the gup() function belongs to the Netlobo.com site.
It is important to note here that Google recently changed the GATC to operate in asynchronous mode. This means that the browser can continue rendering the page while the GATC operates in the background. It is recommended to place this async call to _trackPageview in the header section of the web page as opposed to the older GATC versions where it is recommended the call be placed right before the closing </body> tag of the page. If you are using a pre-Asynchronous version of the GATC you will need to convert the _gaq.push line in the code listed below as detailed in the Google Analytics Asynchronous Migration Examples.
As you can see from this graphic there were 3 Search events registered.
Clicking on the Actions menu item will show the Total ZeroResults count. This represents the number of searches that returned the ‘No Posts Found’ message.
Drilling down on ZeroResults shows the search terms that yielded zero results.