Since Google Analytics is relying on query parameters in the URL to track Internal Site Search, Internal Site Search solutions that are using the HTTP POST method can't normally be tracked without some help from IT and server side implementation/programming. This because HTTP POST don't send any query parameters. And based on my experience, it can often take some time to get something like this implemented.
I needed a quicker solution for some friends that have slow opened their web shop Karma.no, and wrote a simple javascript/jQuery solution for them that also tracks Zero Result Searches.
Image may be NSFW.
Clik here to view.
Some example HTML code
Imagine that the search result page is found at the location http://www.yourdomain.com/search.aspx, and that the HTML-code below is generated on this page when a user has searched for something that resulted in Zero Results.
- <form action="/search.aspx" method="post" name="intSearch">
- <input name="keywordFieldName" id="keywordFieldID" value="some search phrase" type="text" />
- <input name="searchButton" type="submit" value="Search" />
- </form>
- <div id="divName">
- <
p>No posts found. Try a different search?</p>
- </div>
In the example HTML-code above, some part of the code have been highlighted with red text. We are going to use those values in the javascript/jQuery script.
The Javascript/jQuery solution for tracking Internal Site Search
To grab the search phrase, I'm using the jQuery .val() method. Replace values in red with your values.
- $(document).ready(function() {
- var strURL = document.location.href;
- if (strURL.indexOf("/search.aspx") != -1) { // Only run the script on the /search.aspx page
- var intSearchVal = $("#keywordFieldID").val(); // Keyword field ID. Replace with your text ID
- var noResults = document.getElementById('divName').innerHTML; // ID of div where the text for "No posts found" is shown.
- if (noResults.indexOf("No posts found.") != -1) { // Replace the text "No posts found" with (part) of the text you are using
- _gaq.push(['_trackPageview','/search/?cat=No results&q='+intSearchVal]);
- } else {
- _gaq.push(['_trackPageview','/search/?q='+intSearchVal]);
- }
- }
- });
Since we are using a virtual search result page name in the script above, we have to exclude the real search result page name (search.aspx) to avoid double counting of page views. The filter below will exclude search.aspx from the reports in Google Analytics.
Filter Name - Exclude Page: search.aspx
Filter Type | Custom filter |
Filter | Exclude |
Filter Field | Request URI |
Filter Pattern | \/search\.aspx |
Case Sensitive | No |
Site Search Setting
The next thing you have to do is set your Site Search Settings.
Image may be NSFW.
Clik here to view.
Done! Happy tracking internal search phrases and zero result searches in Google Analytics from internal search using POST request method.
If you think the piece of code could be improved, feel fra to comment (I'm not a programmer).
Clik here to view.
Clik here to view.
Clik here to view.
Clik here to view.
Clik here to view.