[ Skip to main content ]
← Back to blog

Integrating Raygun into Magento

 |  Infrastructure + stack Web + software development

Integrating Raygun.io into Magento is about as straightforward as any other PHP project (using Raygun4PHP, but there are a few gotcha’s to be aware of.

First of all, as the Raygun4PHP page states, make sure the curl binary is installed on the server (the php-curl extension is not enough, though it is also mentioned).

Next you’ll want to clone the raygun4php repository somewhere, once this is done copy the contents of the src directory (that is the Raygun4php folder) into app/code/community/.

Now we will insert the first part of our error handling.

Inside index.php, just before the last line (Mage::run($mageRunCode, $mageRunType);), insert the following code:

<?php // ignore this line

 * Error tracking via Raygun
require (MAGENTO_ROOT . '/app/code/community/Raygun4php/RaygunClient.php');

$client = new \Raygun4php\RaygunClient('replace me with your api key', true); // Use true for async

function error_handler($errno, $errstr, $errfile, $errline ) {
    global $client;
    $client->SendError($errno, $errstr, $errfile, $errline);

function exception_handler($exception)
    global $client;

function shutdown_handler()
   global $client;

   $err = error_get_last();

   if (isset($err['type']) && $err['type'] !== E_WARNING) {
      $client->SendError($err['type'], $err['message'], $err['file'], $err['line']);


This is very similar to the example code, with the added addition of a shutdown handler. With a shutdown handler you can also catch parse errors and other fatal errors. Here we have disabled E_WARNING due to a caveat that the PHP error_get_last() function returns info about suppressed errors, and during testing we found that Magento was suppressing a warning that came up in every request.

So this will cover a few errors, but Magento runs the bulk of its code within a try/catch statement inside app/Mage.php. We’ll modify that file next to also submit to Raygun.

Inside app/Mage.php find the run method and look for the line if (self::isInstalled() || self::$_isDownloader) {, just before this line place the following:

<?php // ignore this line

 * Error tracking via Raygun
$client = new \Raygun4php\RaygunClient('replace me with your api key', true);

To test the error handler, open one of the template files under app/design and try adding <?php : ?> somewhere (to test the fatal handler), or <?php throw new Exception('test'); ?>.

With this in place you are ready to commit your code and start sending errors to Raygun!

Share this post:

We'd love to hear from you

Technology enables us to do some awesome things. We love how a simple greeting or question can turn into something amazing.

Contact us
Magento - Wellington, Auckland


If your business is serious about ecommerce success, we can help. Magento offers flexible, feature-rich solutions that address a wide range of business sizes, needs and budgets.

PHP - Wellington, Auckland


PHP is a widely-used, general-purpose scripting language, that is especially suited for web development and can be embedded into HTML.

Silverstripe - Wellington, Auckland


Silverstripe CMS is a web content management system used by governments, businesses and non-profit organisations around the world. It is the Common Web Platform for New Zealand’s Public Sector.

Symfony - Wellington, Auckland


Symfony is a PHP framework for web projects. Speed up the creation and maintenance of your PHP web applications. Replace the repetitive coding tasks by power, control and pleasure.