You can use this in your observer:
public function yourMethod(Varien_Event_Observer $observer)
{
$categoryId = '';
if ($category = Mage::registry('current_category')) {
$categoryId = $category->getId();
}
}
Edit:
Okay first of all why the if statement is needed? Second I tried the code updated and previous and still nothing. When I keep the if statement it returns null and without the if statement it gives the error: Call to a member function getId() on null.
The if-statement is required to check if you view a product within a category. Products can also be viewed without category path ... if $categoryId
returns null
you are not in a category. Please check URL for the viewed product.