Google Tracking & Magento Drop down Navigation
-
12-12-2019 - |
Question
I'm trying to track click events in our 2 level drop down category navigation menu - located in the header.
All I'm trying to do is add some attributes to the <a href>
tags but this is proving to be harder than it sounds. I can't figure out where the $_menu
variable referenced in topmenu.phtml
is coming from. (any ideas?) I've found 2 solutions to the this problem that only seem OK and don't think either is best practice.
Option 1
Use an observer to intercept and modify the HTML.. This is roughly what the Anowave Tag Manager Module does.
Option 2
Use this example to create a fully custom top menu.. which exactly what's there already with some extra <a href>
attributes.
Thoughts
Option 2 requires create pretty redundant code because whatever is there is 99.9% good.. I'm just trying to include an attribute like data-value='category name'
etc.
ANSWER NOTES
Both @sander & @denisa posted answers that solved my problem. However, as @denisa mentioned, you need to know if you're using using the newer RWD template or the older template. I was using a newer install of Magento but and Non-RWD template. Once you know this, use either @denisa's answer or @sander's
Solution
Since you didn't extend from rwd theme, renderer.phtml won't be rendered if you will add it in your custom theme.
I would do something like that: extend Mage_Page_Block_Html_Topmenu
and modify protected function _getHtml
with my code. You will see there $html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>'
- that's the place where you have to add you own code.
OTHER TIPS
Adding extra data can be done in the template file app/design/frontend/rwd/default/template/page/html/topmenu/renderer.phtml
or whatever template you're using.
On line 54 you'll find the following line
$html .= '<a href="'. $child->getUrl() .'" class="'. $outermostClassCode .' '. $_hasChildren .'">'. $this->escapeHtml($this->__($child->getName())) .'</a>';
Change that to, for example
$html .= '<a data-value="'. $this->escapeHtml($this->__($child->getName())) .'" href="'. $child->getUrl() .'" class="'. $outermostClassCode .' '. $_hasChildren .'">'. $this->escapeHtml($this->__($child->getName())) .'</a>';