tags:

views:

43

answers:

2

I want to insert tracking codes on all of the pages of a Magento site, and need to use a different syntax if the page is a CMS page, a category browsing page, or a product view page. I have a custom module set up with a block that inserts a generic tracking code on each page for now. From within the block, how can I distinguish between CMS pages, category pages, and product pages?

I started with:

Mage::app()->getRequest();

I can see that

Mage::app()->getRequest()->getParam('id');

returns the product or category ID on product and category pages, but doesn't distinguish between those page types.

Mage::app()->getRequest()->getRouteName();

return "cms" for CMS pages, but returns "catalog" for both category browsing and product view pages, so I can't use that to tell category and product pages apart.

Is there some indicator in the request I can use safely? Or is there a better way to accomplish my goal of different tracking codes for different page types?

+1  A: 

There may be an even better way to do this using routers, but one fast way is to check the registry to see if we have a single product that we are looking at:

<?php

$onCatalog = false;
if(Mage::registry('current_product')) {
    $onCatalog = true;
}

Hope that helps!

Thanks, Joe

Joseph Mastey
A: 

For any Category / Product page, you are getting the ID from:-

Mage::app()->getRequest()->getParam('id');

Now try the below code:-

<?php
$tempProduct = Mage::getModel('catalog/product')
               ->load(Mage::app()->getRequest()->getParam('id'));
if(!empty($tempProduct->getSku())) {
    echo 'Inside the Product page definitely';
}
?>  

Since Sku will only be available for Products, so you have a very good chance to identify whether the current page is a product page / not.

Knowledge Craving
Thanks for the suggestion. I am currently using the solution posted by Joseph Mastey which has been effective so far and doesn't require the extra database access to load the product. Your solution looks solid as well.
Del F