Log clicked search suggestions

If the search UI is using typeahead and the user selects a suggested option to go right to a page, then a search is never logged as a search query; it's like the search never happened. Add a fetch request to log the search query just before sending the user on their way to the page.
This commit is contained in:
Brian Huisman 2023-06-16 14:38:24 -04:00
parent 8ed10eac36
commit 54bbbb6a65
2 changed files with 35 additions and 5 deletions

View file

@ -21,8 +21,22 @@ $('input.os_typeahead').attr('autocomplete', 'off').typeahead({
source: os_bloodhound,
display: 'title'
}).bind('typeahead:selected', function (obj, datum) {
if (typeof os_odata.jw_depth == 'string')
datum.url = datum.url.replace(/^\//, os_odata.jw_depth);
window.location.href = datum.url;
// We are offline
if (typeof os_odata != 'undefined' && typeof os_odata.jw_depth == 'string') {
window.location.href = datum.url.replace(/^\//, os_odata.jw_depth);
// Else we are online
} else {
// On user click of a search suggestion, add this search to the
// query log
fetch(new Request(window.location.origin + window.location.pathname), {
method: 'POST',
headers: { 'Content-type': 'application/json' },
body: JSON.stringify({ q: datum.query, log: 'log' })
})
.then((response) => response.text())
.then((data) => { window.location.href = datum.url; });
}
});

View file

@ -21,6 +21,19 @@ $_SDATA = array(
);
/* ***** Handle POST Requests ************************************** */
unset($_REQUEST['log']);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// JSON POST request
// These are usually sent by javascript fetch()
if ($_SERVER['CONTENT_TYPE'] == 'application/json') {
$postBody = file_get_contents('php://input');
$_REQUEST = json_decode($postBody, true);
}
}
foreach ($_ODATA['s_weights'] as $key => $weight)
$_ODATA['s_weights'][$key] = (float)$weight;
@ -656,6 +669,8 @@ if ($_RDATA['s_searchable_pages']) {
$result['url'] = preg_replace($repStr, '', $result['url']);
// Highlight the terms in the title, url and matchtext
$_RESULT->query = $_REQUEST['q'];
$_RESULT->title = $result['title'];
$_RESULT->url = $result['url'];
$_RESULT->matchtext = $result['matchtext'];
@ -833,9 +848,10 @@ if ($_ODATA['sp_interval'] && !$_ODATA['sp_crawling'] &&
}
}
// Output JSON and exit if requested
if (isset($_REQUEST['json'])) {
header('Content-type: application/json; charset='.$_ODATA['s_charset']);
die(json_encode($_SDATA['json'], JSON_INVALID_UTF8_IGNORE));
} ?>
// Just log this query and do not post results
} else if (isset($_REQUEST['log'])) die(); ?>