6

I have a request from client where he wants to put google analytics code on different pages in Magento under footer.

I see that footer is generic, so is there a way to place a different code under footers for pages / create different footers?

  • But what is point of that? By default magento puts the google analytics code in the beginning of the body which is loaded asynchronously. It is put there to start analytics as early as possible. If put in the footer, it is a lot more likely for a user to navigate away from the page before the analytics code is loaded. – Dan Apr 2 '13 at 9:53
11

This is a common problem: Adding something in the footer but only on some pages. Everything works perfectly as Alex suggested but only when the block cache is disabled. You can add a block to the footer on any page using layouts. For example adding something only on the home page can be done like this:

<cms_index_index>
    <reference name="footer">
        <block type="some/block_type" template="some/template.phtml" name="some_name" as="some_name" />
    </reference>
</cms_index_index>

The problem occurs when you activate the block cache. The footer cache is general and does not take into account the page it's in.

Here is my general solution for this kind of problem. I override the footer block (Mage_Page_Block_Html_Footer) and change the cache key info. Let's say that my new footer block is Easylife_Page_Block_Html_Footer, the method getCacheKeyInfo() would look like this:

public function getCacheKeyInfo()
{
    $info = parent::getCacheKeyInfo();
    $info[] = $this->getHasSomeBlock();
    /* this needs to be added for each block that 
       should appear only on specific pages, just 
       choose a different key for each block
    */
    return $info;
}

Now in the layout section where I added the block to the footer I add a new line:

<cms_index_index>
    <reference name="footer">
        <action method="setHasSomeBlock"><param>1</param></action> 
        <block type="some/block_type" template="some/template.phtml" name="some_name" as="some_name" />
    </reference>
</cms_index_index>

This solved all my issues for this kind of problem.

  • you suggested me this link but my code was different from your code please help me with reference to my code. – dhoni7 Nov 20 '14 at 12:01
  • public function getCacheKeyInfo() { return array( 'PAGE_FOOTER', Mage::app()->getStore()->getId(), (int)Mage::app()->getStore()->isCurrentlySecure(), Mage::getDesign()->getPackageName(), Mage::getDesign()->getTheme('template'), Mage::getSingleton('customer/session')->isLoggedIn() ); } – dhoni7 Nov 20 '14 at 12:02
  • @marius Would this work if you want a completely different footer alltogether? In some pages like checkout, I was required to add a different footer that has no links. So I created a new template file and added this <template>page/html/different_footer.phtml</template> to the checkout layout file. It works with cache disabled but when I enable it I get the regular default footer. Since I'm not adding a block to the existing footer but instead using a different template alltogether, can I use this cache key info? – Vic May 26 '15 at 17:57
  • @Vic . I haven't tried it but I don't see any reason not to work. – Marius May 26 '15 at 19:36
  • 2
    Thank you for this trick. My solution is create a new child block of root instead of altering the footer. So, I can completely control where to show this block regardless the cache. – Tran Dinh Khanh Jul 14 '15 at 6:23
1

You can add / removed the concerning block per page in your layout XML files just like any other block.

There are handles for the different page types, for different categories, for different products and so on.

1

@Marius' answer will work, but it is far too complex. Rewriting core Magento classes should be avoided at all costs, especially when cleaner solutions exist.

The only thing that needs to be done is the following: add a cacheKey to the layout XML reference that is having its template changed. For example, this is how to change the footer on the onepage checkout--and only the onepage checkout, without concern that the cache will use the original, site-wide footer:

    <checkout_onepage_index>
        <reference name="footer">
            <action method="setTemplate">
                 <template>linusmoneymaker/page/html/checkout-footer.phtml</template>
            </action>
            <action method="setCacheKey">
                <key>your-own-unique-cache-key-for-linus-moneymaker</key>
            </action>
        </reference>
    </checkout_onepage_index>

Notice that the only addition is calling the setCacheKey method. No unnecessary Magento rewrite.

I've explained why this works in greater detail in my original answer on StackOverflow.

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.