Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Track Searches with 0 Results in Universal Analytics

The code would look something like this (you will have to modify this so it will work on your site):

var searchQuery = $('input[class="rsearch_sok2"]').val();
var p = document.location.pathname;
var s = "/?q=" +  searchQuery + "&cat=no-results";
ga('send', 'pageview', {'page':p + s});

The code will:

  1. Take the query parameter from a input field
  2. Get the location path name (e.g /search/ from the url http://www.example.com/search/)
  3. Put the data from 1 & 2 into a string matching the search settings in Google Analytics.
  4. Send it into Universal Google Analytics
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment