Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › No result track › Reply To: No result track
Hi,
For that there is indeed no setting as of yet – but it should be easily doable via a small custom code snippet via the javascript API.
For that, I constructed this small snippet, which hooks into the container when the results are displayed and to sends a no_results event with the search phrase in the “phrase” property, when detects 0 results:
add_action(
'wp_footer',
function () {
?>
<script>
document.querySelectorAll(".asp_main_container").forEach((el) => {
el.addEventListener("asp_results_show", (event) => {
// event.detail carries the arguments
const [id, instance, phrase] = event.detail;
const zeroResults = document.querySelectorAll('.asp_r_' + id + '_' + instance + ' .item').length === 0;
const noresContainerVisible = document.querySelectorAll('.asp_r_' + id + '_' + instance + ' .asp_nores').length > 0;
if ( zeroResults || noresContainerVisible ) {
// This is the GTAG event that is sent
gtag('event', 'no_results', {
'phrase': phrase,
});
}
});
});
</script>
<?php
}, 9999
);
Try adding this code via the Code Snippets plugin or to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.
If you need any help with this, just le me know!