Possible to see a log of what people are searching for when they use the simple search on my site?

Is it possible to see a log of what people are searching for when they visit my site. We have simple search installed but could consider switching to advsearch.
For example, if they go to my site and search for kittens. Then the next person comes and searches for puppies. I’d live to be able to see a file that just would say
kittens
puppies

The easiest way to do this and create a file would be to create a snippet that takes the URL query parameters and adds them to a file something like:

// snippet name: savesearch

$query = $modx->getOption('query',$scriptProperties,'search');
$log   = $modx->getOption('log',$scriptProperties,'/assets/logs/search.txt');

if (isset($_GET[$query])) {
   $txt = filter_var($_GET[$query], FILTER_SANITIZE_STRING);
   file_put_contents($log, $txt.PHP_EOL , FILE_APPEND | LOCK_EX);
}

return;

Then call the snippet on the search results page:

[[!savesearch?
  &query=`simpleSearchQueryParamater`
  &log=`/path/to/your/log//file.txt`
]]

**Note: Above code not tested

  • Make sure the file exists and is writeable by the server
  • Call uncached

I’d suggest adding these terms to the DB, you could rewrite to add the data to a table that way you can capture additional data like IP Address (potential GDPR issue), timestamp, etc

1 Like

I’ll try it Tuesday when I get back to the office!

1 Like

This right here! This is the answer :smiley: Thanks @jako

Hey there!
Does it take some time for it to show up after someone does a search? I’m not seeing anything appear in the widget? Is something set up wrong?

Here’s the LogRequest Log widget setting:

And the LogRequest Rank

I did some searches and had some others search and it appears blank
blank

You have to fill the logrequest.trigger system setting with search (or whatever request key SimpleSearch is using).

Thanks for the quick reply. That worked!