Skip to content

Instantly share code, notes, and snippets.

@dalethedeveloper
Created September 27, 2011 21:48
Show Gist options
  • Save dalethedeveloper/1246351 to your computer and use it in GitHub Desktop.
Save dalethedeveloper/1246351 to your computer and use it in GitHub Desktop.
Wordpress functions.php snippet to add Google Analytics tracking to wp_nav_menu with Yoast's plugin
/*
For use in functions.php in combination with:
http://wordpress.org/extend/plugins/google-analytics-for-wordpress/
*/
add_filter('wp_nav_menu', 'menu_ga_tracking', 99);
function menu_ga_tracking($menu) {
if( class_exists('GA_Filter') and yoast_ga_do_tracking() ) {
$menu = preg_replace_callback(
'/<a (.*?)href=[\'\"](.*?)\/\/([^\'\"]+?)[\'\"](.*?)>(.*?)<\/a>/i',
'ga_parse_menu_links',
$menu
);
}
return $menu;
}
function ga_parse_menu_links($matches){
return GA_Filter::ga_parse_link('outbound-menu',$matches);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment