<blockquote>
  <p>We are looking to log searches so we can report on them. Are there any
  hooks or events we can use to get this data?</p>
</blockquote>

<p>No, there aren't any hooks or events related to searching, currently (Craft 2.4). Like Brad says, you can <a href="http://feedback.buildwithcraft.com/" rel="nofollow">add a feature request</a>.</p>

<blockquote>
  <p>[...] how can a custom plugin log the initial search parameters, call the
  underlying core Craft search, and finally log the results?</p>
</blockquote>

<p>Something like this should work:</p>

<pre><code>class MyPluginVariable
{

    public function search($query = '', ElementType $elementType = null)
    {

        MyPluginPlugin::log($query);

        $criteria = craft()-&gt;elements-&gt;getCriteria($elementType ?: ElementType::Entry);
        $criteria-&gt;search = $query;

        MyPluginPlugin::log(count($criteria));

        return $criteria;

    }

}
</code></pre>

<p>...and in the template:</p>

<pre><code>{% set entries = craft.myPlugin.search('body:foo -body:bar') %}
</code></pre>

CC BY-SA 3.0
2015-06-06T09:11:55.177Z
2015-06-06T15:37:59.803Z
2015-06-06T15:37:59.803Z
Mats Mikkel Rummelhoff
<p>It sounds like what you'd need would be an <code>onBeforeSearch</code> or <code>onSearch</code> event.</p>

<p>There currently isn't anything like that in Craft, but would probably be worth adding as a <a href="http://feedback.buildwithcraft.com" rel="nofollow">feature request</a>!</p>

CC BY-SA 3.0
2015-06-04T17:54:47.580Z
Assuming I write a plugin then, how could i intercept the entries method, or can't I?
Russ Back
CC BY-SA 3.0
2015-06-04T18:01:04.893Z
Wait... what are you trying to log?  I assumed anything that went through the SearchService.
Brad Bell
CC BY-SA 3.0
2015-06-04T21:13:16.340Z
Yes that's right, but I was hoping we could latch onto craft.entries.search('body:foo -body:bar'), but I guess not? If that's the case and we instead have a plugin that in twig looks like craft.myplugin.search('body:foo -body:bar'), how can we (in the plugin) defer the processing onto the core search functionality once we've done our logging?
Russ Back
CC BY-SA 3.0
2015-06-04T21:52:36.623Z
Just pass it off to the SearchService like normal?
Brad Bell
CC BY-SA 3.0
2015-06-08T08:50:16.600Z
FYI: I just added the [feature request](http://feedback.buildwithcraft.com/forums/285221-feature-requests/suggestions/8267127-add-events-for-onbeforesearch-and-onsearch) :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-06-04T17:41:18.757Z
2015-06-04T17:41:18.757Z
Brad Bell
<p>We are looking to log searches so we can report on them. Are there any hooks or events we can use to get this data?</p>

<p>If not, then how can a custom plugin log the initial search parameters, call the underlying core Craft search, and finally log the results?</p>

<plugin-development><search>
Is there a hook or event we can use to intercept a search request?
# Is there a hook or event we can use to intercept a search request?

(Asked by: username_0 on 2015-06-04)

We are looking to log searches so we can report on them. Are there any hooks or events we can use to get this data?

If not, then how can a custom plugin log the initial search parameters, call the underlying core Craft search, and finally log the results?

**Tags:** plugin-development, search

---

## 2 answers

### Answer by username_1 on 2015-06-06. Score: 3 | Accepted answer

We are looking to log searches so we can report on them. Are there any
  hooks or events we can use to get this data?

No, there aren't any hooks or events related to searching, currently (Craft 2.4). Like Brad says, you can add a feature request.

[...] how can a custom plugin log the initial search parameters, call the
  underlying core Craft search, and finally log the results?

Something like this should work:
class MyPluginVariable
{

    public function search($query = '', ElementType $elementType = null)
    {

        MyPluginPlugin::log($query);

        $criteria = craft()->elements->getCriteria($elementType ?: ElementType::Entry);
        $criteria->search = $query;

        MyPluginPlugin::log(count($criteria));

        return $criteria;

    }

}

...and in the template:
{% set entries = craft.myPlugin.search('body:foo -body:bar') %}


---

### Answer by username_2 on 2015-06-04. Score: 1

It sounds like what you'd need would be an `onBeforeSearch` or `onSearch` event.

There currently isn't anything like that in Craft, but would probably be worth adding as a [feature request](http://feedback.buildwithcraft.com)!

> **Comments:**
> **username_0**: Assuming I write a plugin then, how could i intercept the entries method, or can't I?  
> **username_2**: Wait... what are you trying to log?  I assumed anything that went through the SearchService.  
> **username_0**: Yes that's right, but I was hoping we could latch onto craft.entries.search('body:foo -body:bar'), but I guess not? If that's the case and we instead have a plugin that in twig looks like craft.myplugin.search('body:foo -body:bar'), how can we (in the plugin) defer the processing onto the core search functionality once we've done our logging?  
> **username_2**: Just pass it off to the SearchService like normal?  
> **username_1**: FYI: I just added the [feature request](http://feedback.buildwithcraft.com/forums/285221-feature-requests/suggestions/8267127-add-events-for-onbeforesearch-and-onsearch) :)  

---

