I need to execute some custom JavaScript right after the sales_order_place_after event is fired for collecting analytic data.

I know how to make an observer and do something in the relevant method. But how do I load and execute a custom JavaScript on the frontend/admin depending on how the order was placed?

Right now, I have created the module, added the config.xml, and have created an observer with following code.

class Namespace_module_Model_observer
    public function doSomeThing($observer)
        $order = $observer->getEvent()->getOrder();

        //do something here to load the custom javascript and execute it
        //whether the order was placed from backend or frontend

The observer method is getting executed when the order is placed. What should be the correct approach to achieve my custom functionality?

Note: I have to keep in mind that my observer code doesn't interfere with the checkout steps as the customer might/might not be redirected to payment gateway for further processing depending on the payment method chosen.

  • 1
    You cannot execute js code in an observer. The observer runs server side. And this event in particular is dispatched for an action that does not provide output. When an order is placed you get redirected. Either to the payment gateway, either to the success page.
    – Marius
    Oct 21, 2014 at 13:59
  • What's the alternative then? I'm not actually trying to execute javascript per se, if I can load it in a page, it will get executed.
    – Anil Kumar
    Oct 21, 2014 at 14:01
  • You'll need to add the JS on the page you get redirected to. Oct 21, 2014 at 14:08
  • Add this code on checkout/success page
    – Amit Bera
    Oct 21, 2014 at 14:13
  • Currently, I'm accomplishing this task on success page itself but it turned out after a month of implementing this that not all redirections to payment gateway come back to the server, like network error, payment gateway not available, thing like that.
    – Anil Kumar
    Oct 21, 2014 at 14:32

2 Answers 2


So in no way I'm saying this is best practice but you could get the layout in the observer and add a Javascript tag to the head block for example.

Still, there must be a better way to do whatever you wanna do. Maybe update your question with whatever you want to accomplish

$layout = Mage::app()->getLayout();
reference name="head">
    <block type="core/text" name="myjs">
        <action method="setText"><text><![CDATA[<script type="text/javascript">
// put your JS here

To accomplish what your trying to do will required allow of work especially for the admin section.

Take a look at the default GA


  1. Duplicate the Ga Analytics Module and update the filenames, class name and xml accordingly
  2. Change the logic to your version Mage_GoogleAnalytics_Block_Ga accordingly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.