-
AuthorPosts
-
December 22, 2020 at 2:19 pm #1269317
How can I add ID to a menubutton? I need it to track the event of the link/button in Google Analytics.
I know how to add ID to a normal link like this:
<a href="https://your.site/link" id="your-id">Link text</a>
I also know how to add ID to a Enfold button. But I can’t seem to figure out how to add ID to a link in the head menu.Thank you
- This topic was modified 3 years, 11 months ago by wen91.
December 22, 2020 at 3:17 pm #1269336Hi Wendy,
Can you give us a screenshot and point out which one you’re referring?
Are you referring to the main navigation?Best regards,
NikkoDecember 22, 2020 at 5:37 pm #1269370Do you mean your main navigation menu? – the id’s are generated by the system itself and are added automatically to the list points.
On a normal wp-nav-menu it is possible to have customized menu-item ids. there are existing snippets for that.
F.e. using the filter: nav_menu_item_id – but i guess enfold has its own nomenklatura – and now we need the place where enfold determines those ids.
Maybe it is in that function: function avia_ajax_switch_menu_walker()December 22, 2020 at 6:52 pm #1269380but you can do something that will not disturb those menu-item-ids.
You can give an Id and Alt attribute to the anchor itself.But i had to find a way that we will not have duplicate ids then – you see the problem if you put this to your child-theme functions.php
the (hamburger menu items anchors will have the same ids – and if the menu-link goes in a one-pager to that ID – . . . . .… etc. pp.
maybe it is on SEO Purpose more important to have only the alt Attribute to that link!!!:
Why must it be an ID that was tracked by Google Analytics? Arn’t there other possibilities.function set_attributes_to_menu_links(){ ?> <script> (function($){ $(document).ready(function(){ $('#avia-menu .menu-item a').each(function(){ var menuName = $(this).find('>.avia-menu-text').text(); var customID = menuName.replace(/\s+/g, '_').toLowerCase(); $(this).attr('alt', menuName); $(this).attr('id', customID); }); }); })(jQuery); </script> <?php } add_action('wp_footer', 'set_attributes_to_menu_links');
December 23, 2020 at 12:17 pm #1269519Thank you very much. Yes I meant the main navigation. I will comunicate your remark to our SEO manager.
-
AuthorPosts
- You must be logged in to reply to this topic.