WordPress search route

Extended Google Analytics measuremet.


WordPress URL after search has a format like /?q=query+term. It is not possible to remove a query parameter from URL in Google Alanytics - all search URLs would be displayed as homepage then. That is the reason for it is advisable to change the URL and track a virtual pageview in format /search?q=query-term.

In addition, if a function which returns the number of search results is defined, searched term and result count will be automatically tracked as event. You can trace easily frequently searched terms for which site search does not find any results.


It is needed to create a route and add it to _gas object via addRoute method. Creating a route might look like this:

<script type="text/javascript" src="/analytics.js"></script>
<script type="text/javascript">
    var getResultCount = function() {
        return $('span#selector').text(); // RETURN NUMBER OF RESULTS
    var wpSearch = new _gas.route.WPSearch(getResultCount);


Route constructor has 2 optional parameters.

  • function that returns the number of search results - if defined, events with searched term and the number of results is sent to Google Analytics automaticly.
  • function - anonymous function, which is executed after internal site search on WP before _trackPageview is sent. Function is executed only once even if more trackers are created.