Logging user/customer actions in Magento

Logging user/customer actions in Magento

Recently I wrote a Magento extension called ActionLogger, inspired (thematically) by the “Logging of Administrator Actions” feature available in Magento Enterprise. ActionLogger is pretty simple extension, working on pretty simple but powerful concept of Magento controllers predispatch action.

All you need to do is to create event observer/observers for “controller_action_predispatch” event. This event is fired on every controller action call.

For example, if your store is located on url http://magento-1501.net url, and you go to url like http://magento-1501.net/electronics/cell-phones this will call action “view” from controller “category” from module “Mage_Catalog“. Simple logic/tracing would point this to be app\code\core\Mage\Catalog\controllers\CategoryController.php file and its viewAction().

Adding an event observer like the one shown below will trigger your code upon calling such url.

<frontend>
<events>
<controller_action_predispatch>
<observers>
<activecodeline_actionlogger_controller_action_predispatch>
<class>activecodeline_actionlogger/observer</class>
<method>hookToFrontendControllerActionPredispatch</method>
</activecodeline_actionlogger_controller_action_predispatch>
</observers>
</controller_action_predispatch>
</events>
</frontend>

Same goes for the admin section, for example if you open a link like http://magento-1501.net/index.php/admin/sales_order/index/key/9c13e78eecdc3d5805f6426be9f56254/. This one would call action “index” from controller “sales_order” from module “Mage_Adminhtml“. Simple logic/tracing would point this to be app\code\core\Mage\Adminhtml\controllers\Sales\OrderController.php file and its indexAction().

Adding an event observer like the one shown below will trigger your code upon calling such url.

<adminhtml>
<events>
<controller_action_predispatch>
<observers>
<activecodeline_actionlogger_controller_action_predispatch>
<class>activecodeline_actionlogger/observer</class>
<method>hookToAdminhtmlControllerActionPredispatch</method>
</activecodeline_actionlogger_controller_action_predispatch>
</observers>
</controller_action_predispatch>
</events>
</adminhtml>

All this is fine & etc., but what’s the point? We already know this, you might say.

The point is, the concept is very simple but enables you to add powerful logging mechanism on top of it. All you need to do is to add few lines of code that would record given action, controller, current user/customer, possibly entire parameters passed to controller, etc.

Then you can store it either in database or in some file.

For example, in ActionLogger extension I created a special models, called Admin and Frontend that handle writing logged values to database from observer.

public function hookToFrontendControllerActionPredispatch($observer = null)
{
if (!Mage::helper('activecodeline_actionlogger')->_canLogFrontendActions()) {
return;
}
 
$log = Mage::getModel('activecodeline_actionlogger/frontend');
 
$log->setActionName(Mage::app()->getRequest()->getActionName());
$log->setControllerName(Mage::app()->getRequest()->getControllerName());
 
if (Mage::helper('activecodeline_actionlogger')->_canLogRequestParams()) {
if($params = Mage::app()->getRequest()->getParams()) {
$log->setParams(Mage::helper('core')->encrypt(serialize($params)));
}
}
 
$log->setClientIp(Mage::app()->getRequest()->getClientIp());
$log->setControllerModule(Mage::app()->getRequest()->getControllerModule());
 
if ($customer = Mage::getSingleton('customer/session')->getCustomer()) {
$log->setCustomerId($customer->getId());
} else {
$log->setCustomerId(0);
}
 
try {
$log->save();
} catch (Exception $e) {
Mage::log('file: '.__FILE__.', line: '.__LINE__, 'msg: '.$e->getMessage());
}
}

On top of that you can create a nice grid in Magento for listing these. Combine this with built in Magento roles, and you can have your grid shown only to certain role.

Concept like this can be used for development/debugging purposes and possibly for some user/customer behaviour profiling. Although “profiling” can mostly be done via Google Analytics these days, there are still admin part of sites that are not suppose to get analyzed by Google.

Thinking further down the road, one can build truly dynamic user specific menus based on the logged data statistics, etc.

Hope I got you thinking.

P.S. Unlike my usual way of packing up the code into the zip archive, this time I decided to actually submit extension to Magento connect. This was done yesterday and is waiting to be approved :). Once approved, I’ll post a link to extension for those who are interested. Extension is now live at Magento Connect here.

Cheers.

You made it all the way down here so you must have enjoyed this post! You may also like:

How To Connect Google Analytics 4 To Magento 2 Bojan Mareljic
Bojan Mareljic, | 36

How To Connect Google Analytics 4 To Magento 2

3 best open-source eCommerce platforms in 2021 Zrinka Antolovic
Zrinka Antolovic, | 8

3 best open-source eCommerce platforms in 2021

eCommerce Returns Management – a simple solution from the Fashion industry Zrinka Antolovic
Zrinka Antolovic, | 12

eCommerce Returns Management – a simple solution from the Fashion industry

41 comments

  1. when we search through Action logger frontend grid then flash the Admin logger grid and move on loading….?
    how to resolve it?

  2. Hi,
    the problem with 404 relies in helper/Data.php
    if you var_dump $store you will see that its returning Inchoo_Logger, instead of store id. If you put 1 in getStoreConfig for second param, 404 will disappear. Btw. its not working on latest 1.9.3.1

  3. Anybody has resolved the 404 issue after installing this extension? Cleared all cache, logged out and login all possibility checked but could not solve the 404 error. Please help me

  4. Hi,
    I installed this module in magento 1.9, but it does not working. When I logged in into Administration, it displayed “404 error: Page not found.”. Can you tell me the know how to slove this issue.
    Thanks

  5. Hi, I installed this module from magento 1.9, but it does not working. When I logged in into Administration, it displayed “404 error: Page not found.”. Can you tell me the reason and solution? Thanks

  6. Hi,

    I installed master version from Github on a fresh MAgento 1.9 install and it broke my dashboard. Everytime I tried to log in I was receiving a 404 error. I tried cleaning var/session and var/cache folders several times and trying to log in again with no luck.

    I checked all permissions and everything is looking fine, but for some reason this module breaks the site. There isn’t any information on the Magento log, or either on the apache log on what’s causing the issue.

    Any idea on what’s happening?

    Thank you,
    Jordi

  7. Hey, Any update on its (https://github.com/ajzele/Inchoo_Logger) compatibility with v1.8.1? When enabled, if we login to admin at backend, instead of loading the backend dashboard, it is showing a page with 404 error.
    Do we need to update any file/url in the extension’s file?
    Help/Suggestions are welcome.
    @Branko Ajzele: Please guide!

  8. I uploaded the files from GITHUB to Magento ver. 1.7.0.2. But cannot see the new admin menu to access Logger. tried re-login and clearing cache. But no luck 🙁

  9. Lance, Thanks for the reply however this is not the case. This is not the usual 404 when hitting the module page. I have logged out and back in, cleared the cache, etc etc. Perhaps it’s another module interfering.

  10. Tried installing on 1.8.0.1 but after clearing cache I receive 404 error when hitting any page in the admin. No recorded logs to point me in the right direction. Custom admin URL.

  11. By looking at your code it looks like everything is now in the one grid yeah?

    One other question… I have the following user roles set up in the back end of Mangento: Administrators and Data Admin but neither seem to be recording anything in the Logger – Log entries of everything that passed through Mage::log() area – I am only getting frontend logs?

    Thanks again

  12. Hi Branko

    First up thanks for this post, extension and all the other great work you release into the Magento community!

    I just wanted to check that the new version (Inchoo_Logger) you have placed on Github is now called Logger in the System > Configuration > Developer area?

    If so I ave not set log Request Params to Yes but can’t seem to see the separate Recorded Frontend actions and Recorded Admin actions within the System > Tools menu now only one entry called Logger?

  13. Can you please upload your plugin to github with the rest of our plugins? I would really like to take a look =)

  14. Hi Branko, thank you for this excelent post. Your module is not available under magento connect. Could you post a link to download it? Cheers 😉

  15. Hi,

    Thank you for this great extension. I need it desperately for ver 1.4.0.1. Can I have a zip which I can install myself?

  16. After installing the module to version 1.6.1 (1.7.1-beta as well) I received after clicking “Inchoo Logger” message: “Inchoo_Logger seems to be disabled under System > Configuration > Advanced > Developer > Inchoo Logger > Enabled [Yes | No]. Please enable it in order to see the logger grid.”
    But it is allowed.

  17. I installed this extension. This is really good.

    But, Under Developer section there is option for front end and admin options. Also, under System->Logger there are no sub-menus.

    Am I missing anything?

    Thanks.

  18. As Philip and Jens mentioned, the files are gone on Magento Connect, could you please reupload the extension?

    Thanks!

  19. Download in Magento Connect is dead. Could you reupload the extension?

    Regards,
    Jens

  20. Hi,

    I tried to download this but Magento Connect is reporting that the file is no longer there. Is there a way to download a zip file of this extension and just install it manually? Also, I might modify it a bit to fit our shop.

    Regards,
    Philip

  21. I have been thinking about this for a while, like your work. It’s worth saying, this adds a few ‘script calls’ to a page so is likely to affect performance if left on on a busy site.

    I was also looking to add a dump of $_SESSION and $_COOKIE to help with debugging customers issues. Though Mage makes good use of sessions so the DB could get large quickly. I am going to play with the source and ‘upgrade’ into a more debugging module than a logging one.

    The first steps are easy just add,

    $log->setSessionData(serialize($_SESSION));
    $log->setCookieData(serialize($_COOKIE));

    to ActiveCodeline/ActionLogger/Model/Observer.php -> hookToFrontendControllerActionPredispatch

    and then add the two fields to the sql install ActiveCodeline/ActionLogger/sql/activecodeline_actionlogger_setup

    `session_data` TEXT NULL,
    `cookie_data` TEXT NULL,

    NOTE: this should be done before the module is installed.

    I’ll post more work when I’ve had a play.
    Alan

  22. Hi & thank you for this extension.
    I have a problem in admin when i want to see the frontend or admin grid i go to 404 page not found.
    I have a custom admin url.
    Any idea ? Thank in advance

  23. Hello, I see white coloured code on gray background. Very uncomfortable to read.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.