1

I'm implementing Facebook Conversion Tracking code. I've been instructed to insert the Facebook surveillance JavaScript onto the newsletter subscription confirmation page. But on the website, there is no "confirmation page", just a confirmation message that Magento displays in the header of the home page upon successful confirmation (which I've found is displayed using addSuccess() in code/core/Mage/Newsletter/controllers/SubscriberController.php).

To insert the same code on the purchase confirmation page, I've been able to able Anaraky's GPL-licensed GDRT extension, which creates a Block and uses $this->getData('pageType') on a class that extends Mage_Core_Block_Abstract to only insert the block on the desired page.

e.g. in a custom module, app/code/local/Alleyne/FCT/Block/Script.php

class Alleyne_FCT_Block_Script extends Mage_Core_Block_Abstract {
  // [snip]
  protected function _toHtml() {
    $type = $this->getData('pageType');
    if ($type == 'purchase') {
        // return JavaScript...
    }
  // [snip]
}

But for the newsletter confirmation, I can't just filter by page type -- I only want to trigger it when the newsletter subscription action is taking place (which happens to be on the home page).

Do I need to override Mage_Newsletter_SubscriberController::confirmAction() to output the desire JavaScript? If so, how? If not, what's a more reasonable approach?

1

The only solution without overridding the controller method would be to add the script to the translation of Your subscription has been confirmed.. You can do that in the en_US/translate.csv of your theme like this:

"Your subscription has been confirmed.","Your subscription has been confirmed.<script type=""text/javascript"">
//Here goes the script
//multiple lines are possible
//but quotation marks must be escaped like this: ""double quotation mark""
</script>"

Additionally you must allow unescaped HTML in the messages block with the following layout update for the home page:

<reference name="messages">
    <action method="setEscapeMessageFlag"><value>0</value></action>
</reference>

BUT: This is a potential security risk. And besides that, abusing the translation system to add scripts feels so wrong that I would actually prefer overriding the controller as you suggested.

Conclusion:

Do I need to override Mage_Newsletter_SubscriberController::confirmAction() to output the desire JavaScript? If so, how? If not, what's a more reasonable approach?

There is another approach but it is not more reasonable.

Override the controller, set a flag in the session and use a custom block for the script which checks if this flag is set. If yes, show the script and unset the flag. If no, do nothing.

  • Thanks so much for this answer. Any guidance on where I should begin though for figuring out how to override the controller, or how to set a flag in the session? – balleyne Jul 24 '15 at 4:47
  • Overriding Magento Controllers: inchoo.net/magento/… – Fabian Schmengler Jul 25 '15 at 10:16
  • Set flag in the session: Mage::getSingleton('core/session')->setData('this_is_your_flag', true), get flag: Mage::getSingleton('core/session')->getData('this_is_your_flag') clear flag: Mage::getSingleton('core/session')->unsetData('this_is_your_flag') – Fabian Schmengler Jul 25 '15 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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