How to Set Up Google Analytics Site Search without Query Parameters

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:

If your site does not use query parameters to denote the search term, however, you might see a results page that looks like this:

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:
pageTracker._trackPageview();

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:
pageTracker._trackPageview(‘/searchresults/?q=[keyword]’);

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 http://www.mysite.com/search/keyword
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.

Related Posts Plugin for WordPress, Blogger...

16 Responses to “How to Set Up Google Analytics Site Search without Query Parameters”

Steve says:

John – Great post. I could have used this 6 months ago!

We just moved to a new CMS that forces search friendly URLs and I couldn’t figure out the best way to do this.

We just changed the searches back to query parameters.

Keep up the great posts!

Tom says:

Or you could rewrite the data using GA profile filters (using pretty much the same regex as in your JS example and then writing out a fake parameter)..

John says:

Unfortunately, Site Search data is processed before the URLs reach the Profile Filters in GA, so Profile Filters will not have any impact on Site Search reports.

Hopefully this will get changed at some point in the future and make it easier to manipulate Site Search Reports.

Colin says:

Can I assume, “searchresults” can be defined as any value as we wish?

ie

pageTracker._trackPageview(‘/results/?q=’);

John says:

Yes, you can use whatever you want.

Vlad says:

We’re using Google’s custom site search and I’ve enabled GA to report the terms visitors search for within our site. When I go to Overview » Site Search Terms » I get everything displayed in this format:

cache:zE3fV7mdNhAJ:www.domain.com/directory/index.html racquetball
cache:1YOOeArIYY8J:www.domain.com/directory/services.html hours of operation

Has anyone seen this problem? What else should be configured to strip everything in front of the keywords?

Shotsy247 says:

Where do I put the pageTracker code?

I have a quick search available on every page of my site. Do I add this to the GA code somehow or should I somehow go on the submit of the search form?

Thanks.

Shotsy247 says:

Sorry, should have included this too:

The closet I have in my GA code is:

_gaq.push(['_trackPageview']);

John says:

Shotsy,

_gaq.push(['_trackPageview','/searchresults/?q=KEYWORD_GOES_HERE']);

Shotsy247 says:

Hi John,

Thanks for the reply. I have implemented the code as such where happy was term searched:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxx']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview','/searchresults/?searchfield=Happy']);
(function() {
var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl‘ : ‘http://www‘) + ‘.google-analytics.com/ga.js’;
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
})();

And I am not seeing any jump up in my results.

Previously there was no code in place that would capture the search terms, unless Google now looks at POST data as well as GET.

Does anyone see an issue with the above code and think that Google is not capturing accurate data or that Google now also views POST and the previous data along with data I’m seeing with above code in place is accurate?

Thank you in advance for your help.

John says:

Shotsy,

I’d just check to make sure your profile is setup with Site Search turned on and with the Query Parameter for site search set to ‘searchfield’.

Other than that I don’t know. I don’t see anything obviously wrong with the code you posted.

Shotsy247 says:

Hi John,

Thanks again. Yes the account does have Site Search on and the query parameter set to ‘searchfield’.

I guess as a test, I’ll change the search form POST to a GET and see if I get a jump then. If not it would seem to indicate that Google can grab the search terms from POST data. I’ll reply back once I know the results.

Israel says:

John:

I have same problem that Shotsy.

I setup “q” parameter and I have no results.

I’m using asynchronous way:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-XX']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAllowLinker', true]);
var s = location.pathname;
var re1 = new RegExp(‘/search/([^/]*)/?$’);
var re2 = new RegExp(‘/search/([^/]*)/page([^/]*)/?$’);
var m = s.match(re1) || s.match(re2);
if(m!=null){
_gaq.push(['_trackPageview','search/?q=' + m[1]]);
}else{
_gaq.push(['_trackPageview']);
}
(function() {
var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl‘ : ‘http://www‘) + ‘.google-analytics.com/ga.js’;
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
})();

John says:

I’d first verify that there are pages in your Top Content report that indeed follow the format of “/search/?q=TERM”

You might have to turn off any “Strip Query Parameters” from the site search set up.

Jeff says:

I am not getting the q= within the search result URL.

Our ColdFusion Verity search engine is configured to open search result links within the context of the site’s frameset. Thus, the search result link for a page that is located at http://www.brevardcc.edu/about/content/ will actually point to

http://www.brevardcc.edu/index.cfm?mainframe=/about/content/index.html&subnavframe=/home/index_log-in.html,

which opens the page within its normal context of the frameset.

Will this interfere with the ?q= parameter?

The search result page itself, /search/search_action.cfm, opens in a new tab or window.

So, I am not getting any site search data for the Asynchronous code that does not use query parameters (code is located in the search result page):

var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”); document.write(unescape(“%3Cscript
src=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));

var pageTracker = _gat._getTracker(“UA-xxxxxx-x”);
pageTracker._initData();
pageTracker._trackPageview(‘/search/search_action.cfm?q=criteria’);

Google Site Search is configured as:
Do Track Site Search
Query parameter: q
No, do not strip query parameters out of URL
Using categories? No

Hopefully I’m close, but obviously not there yet. How to remedy?

Jeff, when I run a search on your site, I actually see a pageview with the q parameter in it. Are you sure you’re not accidentally excluding that pageview somehow?

Leave a Reply