Shoora
9/13/2013 - 12:34 PM

For Universal Google Analytics - Tracking Search Results with 0 hits. Put this in the 0 results page template.

For Universal Google Analytics - Tracking Search Results with 0 hits.

Put this in the 0 results page template.

//Define your search query parameter as varible sn
var sn = "q";
var sr = new RegExp(sn+"=[^\&]+");
p = document.location.pathname;
s = document.location.search;

if (!!s.match(sr)) {
  sm = s.match(sr).toString();
  srs = sm.split("=");
  // The next line is where we add the category and add
  // the phrase no-results to the search term.
  sre = sm.replace(sr,srs[0]+"=no-results:"+srs[1]+"&cat=no-results");
  sf = s.replace(sr,sre);
  // Send the data to Google as a Pageview
  //pageTracker._trackPageview(p+sf);
  
  ga('send', 'pageview', {'page': p+sf});
}