Upcoming LunaMetrics Seminars
Washington DC, Dec 1-5 Los Angeles - Anaheim, Dec 8-12 Pittsburgh, Jan 12-16 Boston, Jan 12-16

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:

sitesearch-luna

But in GA there is no “path” option:

sitesearch-path

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.

http://www.lunametrics.com/blog/2010/08/19/site-search-without-query-parameters/

30 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?

mridul says:

Great post!
For all query if search url is: http://www.site.com/cms/web/publishing/search.action then how can we track it through google analytics.

Mahbub says:

hello,
i need help to create my Analytic Tracking Code Snippet to Analytic Traditional Tracking Code Snippet,so i humbly await for your help.

Helena says:

Hi John,
Thanks for sharing the great article. I’m a JS moron. Could you please let me know where the following code should be placed? var s = location.pathname;
var re = new RegExp(“/search/([^/]*)/?$”);
var m = s.match(re);
pageTracker._trackPageview(‘/search/?q=’ + m[1]);

Is below code right?

var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-XXXXXXX-1′]);
var s = location.pathname;
var re = new RegExp(“/search/([^/]*)/?$”);
var m = s.match(re);
pageTracker._trackPageview(‘/search/?q=’ + m[1]);

_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);
})();

Your help is highly appreciated:)

Blessings,
Helena

Helena -

Your code is mixing the traditional and asynchronous syntax for Google Analytics, which is a bad thing.

For reference, look at the the comment from Israel (6 comments before yours). That example uses the async syntax.

Helena says:

Thank you! Jim..I modified the code as below:

var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-27973835-1′]);
var s = location.pathname;
var re = new RegExp(“/search/k([^/]*)\.shtm$”);
var m = s.match(re);
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);
})();

Is it right this time?

Helena – as long as the following line:

var re = new RegExp(“/search/k([^/]*)\.shtm$”);

matches the URL structure for your site search page, then it looks like that will work. Of course, you’ll want to test it first ;)

Helena says:

Thank you SO much Jim! you’re the best!!

Hi

I have a slightly different situation here, for which I hope to find a GA Advanced Filter solution.

Here’s the format of the search results URL at present (thanks Drupal), where ‘cuckoo bar’ is the term:
http://www.wellingtonnz.com/search/site/cuckoo%20bar

And this is what I’d like to see in GA, once the Advanced Filter has had it’s way with the URI:
http://www.wellingtonnz.com/search/site/cuckoo?search-site-q=cuckoo%20bar

As you can see, I basically want to append the search string to the end of the URL so I can use Internal Search tracking in GA. Though this could be done with virtual Pageviews, I’d rather try and find a Filter solution if at all possible.

Any idea guys? I would be most grateful for any assistance in wrangling the forces of the GA Advanced Filter machine.

Cheers,
Dave

Robbin Steif Robbin Steif says:

Dave – Site search always happened BEFORE filters were applied. So I think you need a solution like this one: http://www.lunametrics.com/blog/2010/08/19/site-search-without-query-parameters/

Thanks very much Robbin – very handy to know.

Cheers,
Dave

imp source says:

I simply could not depart your website prior to suggesting that I extremely enjoyed the usual information an individual provide in your visitors? Is going to be again steadily to investigate cross-check new posts.

ian says:

I’m struggling with my site, it basically brings in feeds from twiiter and you can search twitter for keywords once the search has completed the URL looks like http://www.domain.com/#searchterm.

I’ve put # in the query parameter but nothing tracks any clues please.

Ian

Ian -

Everything after the # is ignored in GA. You won’t see it in content reports or be able to use it for site search settings.

You can try using a virtual pageview on your search results. So instead of having:

_gaq.push([‘_setAccount’, ‘UA-XXXXXXX-Y]);
_gaq.push([‘_trackPageview’]);

You would replace with:

_gaq.push([‘_setAccount’, ‘UA-XXXXXXX-Y]);
_gaq.push([‘_trackPageview’, ‘window.location.pathname+’?keyword=’+window.location.href.split(‘#’)[1]’]);

*** Be sure to test it out before you use it!

Lav says:

Hi,

my question is, in my website i have a search box and when i click on search button, i get the desired result but am not seeing any query parameter in the url.

I only see /mywebsitename/search.aspx

Please suggest me some solution that how can i track the search term.