Tracking Searches in

Tracking Searches in Google Analytics

Jon Volks
Written By | @webproninja

I like to know what website visitors are searching for on my websites. It’s great insight to see if there is something missing in the navigation (among other things). I like to put the results into Google Analytics since that is the dashboard I’m already checking for various statistics. Lets start tracking searches in Google Analytics.

Before we get started, a few requirements:

  1. Google Analytics tracking code should already added to the site.
  2. You’ll need access to the form code. If forms a generated from the CMS, the following instructions will not work.
  3. HTML knowledge is recommended.

Code for Tracking Searches in Google Analytics

First, find the form code within your website. The form element could have a few different attributes on it, most likely an action and a method with a few others.
<form action="..." method="...">
Find the input field within the form element, something like this:
<input type="text" name="..." />
Check the input field to see if it has an ID attribute. If it does not then you’ll need to add one, otherwise you can leave it alone. Example:
<input type="text" name="..." id="searchField" />
After the input text field, we’ll go back to the <form> element and we’ll need to add a little bit of Javascript code:
<form action="..." method="..." onsubmit='ga("send","event","404 Search Form","submit", document.getElementById("searchField").value);' />
Whats going on here is when the form is submitted, the onsubmit action listener is triggered which calls the function ga(). This function is part of Google Analytics.

  • Parameter One – “send” – leave this as is.
  • Parameter Two – “event” – leave this as is.
  • Parameter Three – “404 Search Form” –  is the event category, it’s required  and typically the object that was interacted with.
  • Parameter Four – “submit” – is the type of action and is required.
  • Parameter Five – document.getElementById(“searchField”).value is the value of the input text field based on the ID field.

That’s pretty much it. Once added to the code on the website, it will be tracking right away. I know when I’m testing it, I don’t see results show up for at least five minutes (give or take). Adding tracking searches in Google Analytics is pretty easy. For more information about Google Analyitcs event tracking, check out this website. Any questions, add them in the comments below.

About The Author

Jonathan Volks is currently a Web Programmer at Skidmore College in Upstate New York. He enjoys everything about the Internet; from Search Engine Optimization to Web Programming. His hobbies include landscape photography and hiking the Adirondacks.

Leave a Reply

Your email address will not be published. Required fields are marked *