0

I have been asked to insert a google tracking code to all of my main_menu Links

<a href="/something" onclick="_gaq.push(['_trackEvent', 'Menu', 'link-name', 'Click']);"\ >link-name</a>

I try to found a module but i couldn't found how to use it to accomplish this task. https://drupal.org/project/google_analytics_et

I thought i can use a hook_menu but i need some help..

Drupal 7

2

To do that I was using Menu attributes module with Onclick Event patch. Simply install it, apply patch, enable module and you will be able to add onclick code to menu elements via GUI.

|improve this answer|||||
  • It worked like a charm!!!!! One small thing because i have 50+ menu items is a way to make it more automatic because the only think it changes is the menu item name – Drupalizeme Oct 1 '13 at 10:38
  • 1
    @Drupalizeme post that as a comment in patch's issue? Should be easy enough. Or you can access link's name in javascript itself (if I get what you mean). I know seeding it is painful, but then you probably will not need to repeat it that many times. And of course I want to see alternative answers. Easier, more automated way should exist. – Mołot Oct 1 '13 at 10:45
0

You can also achieve this with hook_menu() and a preg_replace:

    /**
     * Implements theme_menu_tree__MENU_NAME.
     */
    function mytheme_menu_tree__menu_menuname($variables) {
      $variables['tree'] = preg_replace("@<a(.+)>(.+)</a>@", "<a $1 onclick=\"ga('send', 'event', 'Menu', 'click', '$2');\">$2</a>", $variables['tree']);
      return $variables['tree'];
    }
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.