LoginSignup
8
8

More than 5 years have passed since last update.

GoogleAnalyticsのイベントトラッキングタグを簡単に埋め込む

Last updated at Posted at 2014-03-18

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 );
    });
  });
});
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8