Magento Analytics - Detect CategoryId & ProductId?

I have a custom tracking script which is recording things like Product and Category Page views. Because the script loads on every page, not all pages are category pages and would have a $categoryid OR a Product_View with a $productid.

What would be the best way to detect if $categoryid OR $productid exist in order to pass those values to the tracking script?

Solutions

In the block you use to call the script use some logic after calling:

$category = Mage::registry('current_category'); //if null then not a category
$category->getParentId(); //if null $catagory is a top level category
$product  = Mage::registry('current_product'); //if null then not a product
$product  = Mage::registry('product'); //if null then not a product

For more info check out: Magento: How to tell if you're on a category page, or product page in .phtml file(s)

Similar questions

how to get productId in sign up or sign in page when full page cache on in magento2?
I want to redirect page if customer come from product page ->sign up page i want redirect them after sign up on product page. But i am not getting product id on that page if full page cache is on in magento2. I have created loginpost observer for redirecting page back where before come. I am not getting product_id when full page cache is on $produc...
Why getParentsIdsByChild($productId); is returning empty for Child product?
I'm trying to get parent products id for child products but i'm getting empty result.
Get Simple Product Details Passing Configurable productId and option like color
Get Simple Product Details Passing Configurable productId and option like color Using Product Collection I want to fetch this.
Cannot detect redirect response in Ajax using Prototype.js in Magento
I'm using Prototype.js to send a post request to a Magento controller. The controller will response a Json data or a redirect e.g. $this->_redirect('*/*/billing'); The issue that it seems the Ajax cannot detect the redirect response. I need to "catch" it then handle the redirect action in JavaScript by using window.location = redirect_url; Pleas...
Magento doesn't detect child theme in different store view
I have a Magento website with one store view (ITALIAN) and a theme with the child theme and everything is working good. Today I created a second store view (ENGLISH) but this doesn't recognize the child theme, but only the default theme. Why?
How do I detect ajax complete event in Magento?
I have button on checkout page. When I clicked on that button, an ajax event it is called and returns me a HTML code, which is actually the html code from the next step(payments step). I want to add some CSS code after the ajax event. How can I do that? I tried, by using the ajaxComplete function, but its not firing. or or prototype: Neither of the...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.