Answers.Body: <p>Add a <code>init()</code> method to your plugin and as long as it's installed and enabled, it'll be called on every request.</p>
  
  <pre><code>public function init()
  {
      parent::init();
  
      /* Do your stuff */
  }
  </code></pre>
  
  <p>If you want it to only happen on front-end requests, you can use:</p>
  
  <pre><code>if (craft()-&gt;request-&gt;isSiteRequest()) { ... }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-20T18:07:10.177Z
Answers.Text: +1 Read my mind, great answer. Definitely heed the `isSiteRequest` advice... there are probably _a lot_ of Craft requests in which you wouldn't want to trigger your behavior.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-20T18:34:29.543Z
Answers.Text: Perfect, thank you! EDIT: Sorry, I was asking more about the `craft()->request` stuff, but I just confirmed that it can do what I need. Thanks again!
Answers.UserDisplayName: Gavin Anderegg
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-21T15:23:47.390Z
Answers.Text: I also check to see that I'm in a specific page in the control panel sometimes like this.
  
  
          $pattern = '/mycraftplugin\/subpage$/';
          $isCorrectSubpagePage = preg_match($pattern, craft()->request->requestUri );
  
          // if we are in the control panel on the banner upload page,
          if ( craft()->request->isCpRequest() && $isCorrectSubpagePage ) {
              ... do something
          }
Answers.UserDisplayName: John Morton
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-20T18:04:47.517Z
Answers.LastActivityDate: 2016-04-20T18:04:47.517Z
Answers.UserDisplayName: Brad Bell
Body: <p>For an upcoming site build, I'm going to need to capture information on every page load for a number of sections. These will be pages based on standard entries, so I'd prefer to use traditional routing (so not handled through a custom controller). There will probably be three different sections that will each have the same information captured when the page is loaded – basically just a list of categories so we can track the most popular type of content and display it as a dashboard widget.</p>
  
  <p>At first I assumed I could do this with an event, but it doesn't look like there are any events for a page being loaded. I'm currently thinking that I use JavaScript to fire an AJAX request to a plugin and send the data along. This will work, but generate extra page loads. I'm thinking it'd be more efficient if I could keep everything server side.</p>
  
  <p>Is there some way to do this that I'm not seeing?</p>
  
Tags: <plugin-development><controller><events>
Title: Have a custom plugin do something on every page load?
ThreadText: # Have a custom plugin do something on every page load?
  
  (Asked by: username_0 on 2016-04-20)
  
  For an upcoming site build, I'm going to need to capture information on every page load for a number of sections. These will be pages based on standard entries, so I'd prefer to use traditional routing (so not handled through a custom controller). There will probably be three different sections that will each have the same information captured when the page is loaded – basically just a list of categories so we can track the most popular type of content and display it as a dashboard widget.
  
  At first I assumed I could do this with an event, but it doesn't look like there are any events for a page being loaded. I'm currently thinking that I use JavaScript to fire an AJAX request to a plugin and send the data along. This will work, but generate extra page loads. I'm thinking it'd be more efficient if I could keep everything server side.
  
  Is there some way to do this that I'm not seeing?
  
  **Tags:** plugin-development, controller, events
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-04-20. Score: 8 | Accepted answer
  
  Add a `init()` method to your plugin and as long as it's installed and enabled, it'll be called on every request.
  
  ```
  public function init()
  {
      parent::init();
  
      /* Do your stuff */
  }
  
  ```
  
  If you want it to only happen on front-end requests, you can use:
  
  ```
  if (craft()->request->isSiteRequest()) { ... }
  
  ```
  
  > **Comments:**
  > **username_2**: +1 Read my mind, great answer. Definitely heed the `isSiteRequest` advice... there are probably _a lot_ of Craft requests in which you wouldn't want to trigger your behavior.  
  > **username_0**: Perfect, thank you! EDIT: Sorry, I was asking more about the `craft()->request` stuff, but I just confirmed that it can do what I need. Thanks again!  
  > **username_3**: I also check to see that I'm in a specific page in the control panel sometimes like this.           $pattern = '/mycraftplugin\/subpage$/';         $isCorrectSubpagePage = preg_match($pattern, craft()->request->requestUri );          // if we are in the control panel on the banner upload page,         if ( craft()->request->isCpRequest() && $isCorrectSubpagePage ) {             ... do something         }  
  
  ---
  
  