Record Search Results Queries to a List using a Hit Counter

This is how I am using a Search Results page to record individual Users Search Queries in a list.  Each Search Query is displayed as how the user typed them in.

What I’ll explain is how we can grab the URL from the Search Results and strip out the unnecessary code to display only the User’s Search Query.

For Example

This Search Result URL:


Will be converted to display only what the user has typed in the Search Box, which in this example, is: “pdf“.

Each time a user types in a Search, their query is recorded with their name, date and keywords typed.

Firstly, you need to install and configure the “Hit-Counter and Star-rating for SharePoint” from Alexander’s SharePoint JavaScripts site.

Once you have this configured it and you can see the users data added to the  HitCounter List , we can now modify the HitCounterAndStarRatingForSharePoint.js file to include the Users keywords entered.

1: First create a new column within the HitCounter list call it UserKeywords.  Add this column to your default view.

2: Edit the HitCounterAndStarRatingForSharePoint.js  and scroll down to about line 218:  //Add item.  Within that block of code you will see the column variables, make a new line under ReferringURL:refURL, and type in the new column name which is attached to the variable as such:


3:  Then under the following code at about Line: 152

// Get url and referrer
refURL = document.referrer;

Add the following lines of code:

searchURL =;                               //returns string ?k=Users%20Search%20String
  modURL = searchURL.substring(3);                                   //removes characters ?k= from string
  decodedURL = decodeURIComponent(modURL);       //decode URL to translate encoded characters from string
  searchQuery = decodedURL.split('&')[0];                         //removes characters all after &

Each line I’ve added a comment that describes what each line does.  I don’t know how to write code in Javascript, so I had to Google Search each line to work out how to accomplish each Javascript function.

4:  Now you can test the Search Results page by entering in a Search Query and then checking the HitCounter List.

The UserKeywords column will now display the Search Keywords exactly how they are typed by the user.

Any questions, please comment below, thanks for reading.


Post a Comment

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s