Take care of your Magento store – be aware of 404 errors

Every owner of the web store does all possible to provide good conditions for customers and takes care of their needs. Moreover, it is also good practice to use different services to track the behaviours of web store visitors, collect and analyze the data related to customers preferences to determine how to improve the conversion rate. As a one way of such improvements is to test if there are any issues on the web store. Maybe you’ve heard the words “to win the battle – at least, you should know your enemy”, as follows – you should find what problems the website has. In our article we are describing possible ways to notify the store owner or website admin about 404 pages. Therefore, if you know what URLs are sending the customers to 404 pages, you will be able to fix it by creating the corresponding redirects, rewriting rules etc.

Now let’s find out how to implement such feature on the website. We have few ways – collect the information about 404 pages in a log file, send it to any third-party API or send the data about such broken (404) URLs by email. We will be describing how to implement the notifications about 404 URLs using “Atwix_WeHave404” extension as an example. It has two options that you can work with in the admin panel:

1. Collect 404 URLs in the log file under “{magento_root}/var/log” folder.

2. Send notifications about 404 URLs to the corresponding email address every time when 404 error occurs.

We should create the following files and folders for this module:

– Module declaration file: app/etc/modules/Atwix_WeHave404.xml

atwix_wehave404_tree_2_w

– Module folder with the config files, controller, helper and source model: app/code/community/Atwix/WeHave404

atwix_wehave404_tree_1_w

3. Custom email template for sending the notifications: app/locale/en_US/template/email/atwix_wehave404_notification.html

atwix_wehave404_tree_3_w

The easiest way is just clone or download all the files described above from Github.

We should mention that the main idea of “Atwix_WeHave404” extension is to override Magento core controller which is responsible for 404 error to add few custom actions there. As you may also know, 404 pages are rendered by “noRouteAction” of the “Mage_Cms_IndexController”, so we will override it in our Atwix_WeHave404_Cms_IndexController:

require_once Mage::getModuleDir('controllers', 'Mage_Cms') . DS . 'IndexController.php';

class Atwix_WeHave404_Cms_IndexController extends Mage_Cms_IndexController
{
    /**
     * Render CMS 404 Not found page and send notification to configured email address
     *
     * @param string $coreRoute
     */
    public function noRouteAction($coreRoute = null)
    {
        /** @var Atwix_WeHave404_Helper_Data $atwixNotFoundHelper */
        $atwixNotFoundHelper = Mage::helper('atwix_wehave404');
        $atwixNotFoundHelper->notifyAbout404();

        $this->getResponse()->setHeader('HTTP/1.1','404 Not Found');
        $this->getResponse()->setHeader('Status','404 File not found');

        $pageId = Mage::getStoreConfig(Mage_Cms_Helper_Page::XML_PATH_NO_ROUTE_PAGE);
        if (!Mage::helper('cms/page')->renderPage($this, $pageId)) {
            $this->_forward('defaultNoRoute');
        }
    }

}

In addition to that, if you compare an original “noRouteAction” method with our custom one – you will see the difference only in the following three rows:

/** @var Atwix_WeHave404_Helper_Data $atwixNotFoundHelper */
    $atwixNotFoundHelper = Mage::helper('atwix_wehave404');
    $atwixNotFoundHelper->notifyAbout404();

The main logic of the notification functionality can be found in the “notifyAbout404” method of app/code/community/Atwix/WeHave404/Helper/Data.php class:

public function notifyAbout404()
    {
        $result = false;
        if ($this->isEnabled()) {
            $notificationType = (int) $this->getNotificationType();
            $requestedUrl = Mage::helper('core/url')->getCurrentUrl();

            if ($notificationType === self::NOTIFICATION_TYPE_LOG) {
                $fileName = $this->getLogfileName();
                Mage::log($requestedUrl, Zend_Log::INFO, $fileName, false);
                $result = true;
            } elseif ($notificationType === self::NOTIFICATION_TYPE_EMAIL) {
                $emailTemplateVariables = array('requested_url' => $requestedUrl);
                $storeId = Mage::app()->getStore()->getId();
                $emailTemplateId = $this->getEmailTemplateId();
                $recipientEmail = $this->getRecepientEmail();
                $recipientName = $this->getRecipientName();
                $sender = $this->getSender();
                
                /** @var Mage_Core_Model_Email_Template $emailTemplate */
                $emailTemplate = Mage::getModel('core/email_template');
                $emailTemplate->sendTransactional($emailTemplateId, $sender, $recipientEmail, $recipientName, $emailTemplateVariables, $storeId);
                $result = true;
            }
        }

        return $result;
    }


As you can see, there are few verifications – the first one is to check if the extension is enabled, and the second one is to select the type of the notification. All the settings are declared in app/code/community/Atwix/WeHave404/etc/system.xml and they can be found in Magento Admin -> System -> Configuration -> Atwix Extensions -> We Have 404. Take a look at the possible options:

1. Notification Type = “Log File”

atwix_wehave404_config_1

Pay attention that if this option is selected – you will be able to find all the 4o4 URLs in “{magento_root}/var/log/atwix_wehave404.log” file. Moreover, each URL will be added as a new line in the following format:

2015-09-03T22:53:19+00:00 INFO (6): http://mage1910.dev/qwewqe

2. Notification Type = “Email Notification”

atwix_wehave404_config_2

When this option is selected – every 404 URL will be sent to the configured email address using a custom email template app/locale/en_US/template/email/atwix_wehave404_notification.html. It will look in the following way for the default RWD theme:

atwix_wehave404_email_template

That’s all. We hope this article will help you control 404 URLs on your web store and increase number of orders. We will be glad to receive your feedback in the comments below.