Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-06T09:11:55.177Z
Answers.LastActivityDate: 2015-06-06T15:37:59.803Z
Answers.LastEditDate: 2015-06-06T15:37:59.803Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-04T17:54:47.580Z
Answers.Text: Assuming I write a plugin then, how could i intercept the entries method, or can't I?
Answers.UserDisplayName: Russ Back
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-04T18:01:04.893Z
Answers.Text: Wait... what are you trying to log?  I assumed anything that went through the SearchService.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-04T21:13:16.340Z
Answers.Text: 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?
Answers.UserDisplayName: Russ Back
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-04T21:52:36.623Z
Answers.Text: Just pass it off to the SearchService like normal?
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-08T08:50:16.600Z
Answers.Text: FYI: I just added the [feature request](http://feedback.buildwithcraft.com/forums/285221-feature-requests/suggestions/8267127-add-events-for-onbeforesearch-and-onsearch) :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-04T17:41:18.757Z
Answers.LastActivityDate: 2015-06-04T17:41:18.757Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Tags: <plugin-development><search>
Title: Is there a hook or event we can use to intercept a search request?
ThreadText: # 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 username_2 says, you can [add a feature request](http://feedback.buildwithcraft.com/).
  
  > [...] 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) :)  
  
  ---
  
  