2

In a custom module I'm inserting universal analytics so I have to change a few functions from Ga.php file

In Ga.php there is:

class Mage_GoogleAnalytics_Block_Ga extends Mage_Core_Block_Template
{
//google analytics functions
}

If I do like this:

class Custom_Module_Block_Universalanalytics extends Mage_GoogleAnalytics_Block_Ga{ 
//a couple of functions that I change from Ga.php
}

it doesn't work.

But if I do like this:

class Custom_Module_Block_Universalanalytics extends Mage_Core_Block_Template{  
//all functions from Ga.php plus the ones that I change
}

it does work.

So my question is why wouldn't it work if I extend the Mage_GoogleAnalytics_Block_Ga class which already has all the functions?

Update: Here is my config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_Module>
            <version>0.1.0</version>
        </Custom_Module>
    </modules>
    <global>
        <blocks>
            <universalanalytics>
                <class>Custom_Module_Block</class>
            </universalanalytics>
        </blocks>
        <helpers>
            <universalanalytics>
                <class>Custom_Module_Helper</class>
            </universalanalytics>
        </helpers>
        <models>
            <universalanalytics>
                <class>Custom_Module_Model</class>
            </universalanalytics>
        </models>
    </global>
    <frontend>
        <events>
            <checkout_onepage_controller_success_action>
                <observers>
                    <universalanalytics_order_success>
                        <class>universalanalytics/observer</class>
                        <method>setUniversalanalyticsOnOrderSuccessPageView</method>
                    </universalanalytics_order_success>
                </observers>
            </checkout_onepage_controller_success_action>
            <checkout_multishipping_controller_success_action>
                <observers>
                    <universalanalytics_order_success>
                        <class>universalanalytics/observer</class>
                        <method>setUniversalanalyticsOnOrderSuccessPageView</method>
                    </universalanalytics_order_success>
                </observers>
            </checkout_multishipping_controller_success_action>
        </events>
        <layout>
            <updates>
                <universalanalytics>
                    <file>universalanalytics.xml</file>
                </universalanalytics>
            </updates>
        </layout>
    </frontend>
</config>

Thanks!

Update: Folowing the comments, indeedthe issue is with this function:

protected function _toHtml(){
    if (!Mage::helper('universalanalytics')->isUniversalanalyticsAvailable()) {
        return '';
    }
    return parent::_toHtml();
}

Mage::helper('universalanalytics')->isUniversalanalyticsAvailable() is returning true, but, as I read here https://stackoverflow.com/questions/11179732/overwritten-block-appears-in-addition-to-module-overwrite return parent::_toHtml(); will return the google classic analytics object.

8
  • please show your Custom_Module module config.xml code?
    – Amit Bera
    Commented May 18, 2015 at 9:49
  • @AmitBera done that Commented May 18, 2015 at 9:51
  • Are you sure this Mage::helper('googleanalytics')->isGoogleAnalyticsAvailable()) is returning true? If not, the toHtml function will return an empty string. Commented May 18, 2015 at 10:06
  • how you have call Custom_Module_Block_Universalanalytics file in your code?
    – Amit Bera
    Commented May 18, 2015 at 10:09
  • @Mayers I've created Mage::helper('universalanalytics')->isUniversalanalyticsAvailable() which is returning true. I want the client to be able to control it from the admin. Commented May 19, 2015 at 7:55

1 Answer 1

2

In the class that you extend, there is a _toHtml function that will return an empty string is isGoogleAnalyticsAvailable() returns false. And since your class extends this one, it inherits this function (unless you rewrite it)

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.