Last active
May 2, 2019 18:50
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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