Rails前提ですが、適宜変更してお使いください。
/**
Google Analytics Event Tracking Tag
google_analytics_tracker.js
@requred Google Analytics tag
@requred jquery
:class => 'js_ga_tracker', :"data-category" => '', :"data-action" => '', :"data-label" => '', :"data-num_value" => ''
:"data-label", :"data-num_value" is optional
ex)
link_to 'hoge', '/', :class => 'js_ga_tracker', :"data-category" => '', :"data-action" => '', :"data-label" => '', :"data-num_value" => ''
**/
$(function() {
$('a.js_ga_tracker').each(function(){
var target = $(this);
target.click(function(){
params = ['_trackEvent', target.data('category'), target.data('action')];
if ( target.data('label') ) {
params.push( target.data('label') );
if ( target.data('num_value') && $.isNumeric( target.data('num_value')) ) {
params.push( target.data('num_value') );
} else {
console.log('num_value invalid: ' + target.data('num_value'));
}
} else {
console.log('label invalid: ' + target.data('label'));
}
_gaq.push( params );
});
});
});