Skip to content

Instantly share code, notes, and snippets.

@simondahla
Last active May 2, 2019 18:50
Show Gist options
  • Save simondahla/6550088 to your computer and use it in GitHub Desktop.
Save simondahla/6550088 to your computer and use it in GitHub Desktop.
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});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment