Skip to content

Instantly share code, notes, and snippets.

Last active October 18, 2018 14:27
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save simondahla/6917361 to your computer and use it in GitHub Desktop.

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