2

We're using the core search module on a D9 site and use the "Top Search Phrases" report for search tracking but the results get stored in the watchdog table so they get cleared out when the logs do.

I see there are some D7 modules to improve the core search logs but nothing that I could find for D8.

The client needs to retain the search word logging so how could we do this on our D9 site?

7
  • Does a contributed module provide the "Top Search Phrases" report? If so, which one?
    – cilefen
    Dec 19, 2022 at 15:59
  • A possible option would be to use your analytics to track this. Then you'd be able to get breakdown by day, generate reports and things like that Dec 19, 2022 at 16:06
  • Analytics is an option but this is not my area and haven't been able to figure out how to track Drupal core searches in GA. If you have any info on how to do this that would be awesome. Dec 19, 2022 at 16:08
  • You can ship logs outside of Drupal with the syslog module but of course that will not help with a report that according to you depends on parsing the database log rows.
    – cilefen
    Dec 19, 2022 at 16:08
  • 1
    It does not come from the core search module. It is provided by the core database logging module.
    – cilefen
    Dec 19, 2022 at 16:09

1 Answer 1

1

I don't know if there exists a module (off-topic anyway on this site), but you could try to implement your own cron hook. First disable the built-in clean up process by setting "Database log messages to keep" to "All" in the UI.

Then implement your own hook_cron(), similar to the core hook

/core/modules/dblog/dblog.module

/**
 * Implements hook_cron().
 *
 * Controls the size of the log table, paring it to 'dblog_row_limit' messages.
 */
function dblog_cron() {
  // Cleanup the watchdog table.
  $row_limit = \Drupal::config('dblog.settings')->get('row_limit');

  // For row limit n, get the wid of the nth row in descending wid order.
  // Counting the most recent n rows avoids issues with wid number sequences,
  // e.g. auto_increment value > 1 or rows deleted directly from the table.
  if ($row_limit > 0) {
    $connection = \Drupal::database();
    $min_row = $connection->select('watchdog', 'w')
      ->fields('w', ['wid'])
      ->orderBy('wid', 'DESC')
      ->range($row_limit - 1, 1)
      ->execute()->fetchField();

    // Delete all table entries older than the nth row, if nth row was found.
    if ($min_row) {
      $connection->delete('watchdog')
        ->condition('wid', $min_row, '<')
        ->execute();
    }
  }
}

only that your hook keeps the search entries or moves them to a different table.

1
  • Thank you, this is an interesting solution @4uk4. We're currently seeing if using Google Analytics will provide accurate search logging but, if not, we'll try your suggestion. Dec 20, 2022 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.