Drupal search route

Extended Google Analytics measuremet.


Tracking of internal Drupal search using Google Analytics is problematic. Drupal URL after search has a form like /search/node/query+term. Google Analytics cannot parse this URL and obtain a query term. That That is the reason for it is advisable to change the URL and track a virtual pageview in format /search?q=query-term - this format is acceptable for Google Analytics

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#result-count').text();
    var drupalSearch = new _gas.route.DrupalSearch(getResultCount);


Route constructor has two 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 automatically.
  • 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.