<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>

CC BY-SA 3.0
2016-04-20T18:07:10.177Z
+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.
Lindsey D
CC BY-SA 3.0
2016-04-20T18:34:29.543Z
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!
Gavin Anderegg
CC BY-SA 3.0
2016-04-21T15:23:47.390Z
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
        }
John Morton
CC BY-SA 3.0
2016-04-20T18:04:47.517Z
2016-04-20T18:04:47.517Z
Brad Bell
<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>

<plugin-development><controller><events>
Have a custom plugin do something on every page load?
# 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         }  

---

