Roland Oldengarm - Independent IT Contractor

Living in the coolest little capital Wellington, New Zealand!

How to correctly log search results clicks by calling RecordPageClick in custom HTML & using SharePoint Search REST API

Scenario: Office 365, we have implemented an autocomplete search experience where search results pop up automatically when the user starts typing. We use jQuery autocomplete, and fire Search REST API queries every time a user types something. For different blocks we fire multiple queries, i.e. one for search suggestions, one for people search, and one for files / content.

This is all working like a rocket, but then we ran into the issue that search result clicks won’t be logged. That means that i.e. a search query does not get promoted to a search suggestion when a certain query has received 6 result clicks.

I recalled from SharePoint 2010 that SharePoint attaches a javascript onclick handler to every search result in an OOTB search result page. After Googling I found out that it’s still the case, and that the method is now called “RecordPageClick”. There is no documentation how to use this in an add-in, the only useful documentation was this MSDN article.

So, I started debugging the OOTB search result page, and debugged what variables went in “Srch.ClickRecorder.recordPageClick” (search.clientcontrols.js). And I found out that you need an object in this format:

var recordPageClickObject = {
piSearchResult: {
Title: item.fields.Title,
Path: item.fields.Path,
Url: item.fields.Path,
piSearchResultId: item.fields.piSearchResultId
},
piUserQuery: $(searchBoxSelector).val(),
SourceId: searchResultProperties.SourceId,
piPageImpressionBlockType: 2,
piClickType: “Result”,
piPageImpression: searchResultProperties.piPageImpression

};

Now, how do you extract these properties from the REST API result?

  • item.fields : The properties of each result in data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results
  • searchResultProperties: The query result properties:  data.d.query.Properties.results
  • piUserQuery: The query the user entered
  • piPageImpressionBlockType: Not sure why this has to be 2. This value is passed by SharePoint as well when you click on a search result in Everything search, so used that one. It does not come back in a normal REST API call.

It’s a bit hard to verify it’s working. On Prem you could go to the Search Log database, but that does not work in O365 of course. A good way is to click 6 times on a result for one query, to see that it’s added as a suggestion after a while.

Update: It turns out that it doesn’t work properly. Most likely this is caused by the fact that Search queries via the REST API are not logged in search reports. Thus, any call to RecordPageClick will just be ignored. To be continued….

2 Comments

  1. Hi – I am trying to accomplish the same thing. Did you ever find a way to have the REST search queries get logged through RecordPageClick? Thanks.

Leave a Reply

Your email address will not be published.

*