How To Set Up Google Analytics Site Search Without Query Parameters

August 19, 2010

If your site’s search results page uses a query parameter to denote what the visitor searched for, setting up site search in GA is pretty straight forward:


But in GA there is no “path” option:


What to do?

By altering the GA JavaScript on the search page you can cause GA to “think” that the page has a query parameter for the keyword.

In the traditional GA syntax there is a line of JavaScript on the page that looks like this:

If we pass the _trackPageview() method a parameter, it will use that value as the URL for the page, INSTEAD of whatever happens to be in the browser’s address bar.

This might look like:

Where “keyword” is replaced (by YOU) with the actual keyword that each particular visitor searched for.

This replacement can be done in several ways.
1. CMS script – your CMS system may have a template scripting language that allows you to reference the keyword value.

2. JavaScript – If your results URL is in a standard format and includes the keyword, you may be able to extract the keyword with a regular expression.
This might look like:
var s = location.pathname;
var re = new RegExp(“/search/([^/]*)/?$”);
var m = s.match(re);
pageTracker._trackPageview(‘/search/?q=’ + m[1]);
*Assuming the results page is in the format
and further assuming my JavaScript and regex are even remotely accurate ?  — meaning don’t just use this example code and expect it to work, this is to demonstrate the *concept* only

3. Server side scripting language such as asp, php, cold fusion, etc.
This might look like:
pageTracker._trackPageview(‘/searchresults/?q=<?php echo $keyword ?>’);
*Assuming you have a php variable named “keyword” that contains the search terms used. Don’t just insert this example code into your site and expect it to work — this is to demonstrate the concept only

So the technical details of how you programatically get the keyword into the string of text that you pass to _trackPageview(. . .) will differ from case to case.  But the ideas above should get your web developer started in the right direction.