Recently I worked on WordPress site and had to add Google Analytics tracking code to tabs. But tabs was generated via shortcode and tracking code was truncated. So I added it with jQuery.

The original code was:

<ul class="tabs-shortcode-list ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all" role="tablist">
  <li class="ui-state-default ui-corner-top ui-tabs-active ui-state-active" role="tab" tabindex="0" aria-controls="Phones-0" aria-labelledby="ui-id-1" aria-selected="true"><a href="#Phones-0" class="ui-tabs-anchor" role="presentation" tabindex="-1" id="ui-id-1">Phones</a></li>
  <li class="ui-state-default ui-corner-top" role="tab" tabindex="-1" aria-controls="Email-1" aria-labelledby="ui-id-2" aria-selected="false"><a href="#Email-1" class="ui-tabs-anchor" role="presentation" tabindex="-1" id="ui-id-2">Email</a></li>
  <li class="ui-state-default ui-corner-top" role="tab" tabindex="-1" aria-controls="Form-2" aria-labelledby="ui-id-3" aria-selected="false"><a href="#Form-2" class="ui-tabs-anchor" role="presentation" tabindex="-1" id="ui-id-3">Form</a></li>
  <li class="ui-state-default ui-corner-top" role="tab" tabindex="-1" aria-controls="OfficeHours-3" aria-labelledby="ui-id-4" aria-selected="false"><a href="#OfficeHours-3" class="ui-tabs-anchor" role="presentation" tabindex="-1" id="ui-id-4">Office Hours</a></li>
</ul>

So, i’ve used jQuery’s attr adding the code in functions.php

function gaTracker_inline_script() {
?>
<script type="text/javascript">
jQuery(document).ready(function($){
    $(".tabs-shortcode").find("a[href='#Phones-0']").attr("onclick" , "__gaTracker('send', 'event', 'contacttab', 'phone');");
	$(".tabs-shortcode").find("a[href='#Email-1']").attr("onclick" , "__gaTracker('send', 'event', 'contacttab', 'email');");
	$(".tabs-shortcode").find("a[href='#Form-2']").attr("onclick" , "__gaTracker('send', 'event', 'contacttab', 'form');");
	$(".tabs-shortcode").find("a[href='#OfficeHours-3']").attr("onclick" , "__gaTracker('send', 'event', 'contacttab', 'office');");
});
</script>
<?php
  }
add_action( 'wp_footer', 'gaTracker_inline_script' );