LoginSignup
6
6

More than 5 years have passed since last update.

Rails の Ajax リクエスト全てを Google Analytics のイベントとしてトラックする

Posted at
$(document).ajaxSend(function(event, xhr, settings){
  if (typeof _gaq !== "undefined" && _gaq !== null) {
    pathes = url('path', settings.url).split('/');
    _gaq.push(['_trackEvent', pathes[1], pathes[2]+":"+settings.type]);
  }
});

パスからControllerとActionを分離して、それぞれカテゴリ、アクションに分離している。
環境に合わせてイベントのパラメータを設定して下さい。

jQuery UJS を利用している事が前提だと思う。
https://github.com/rails/jquery-ujs
URLのパースはjs-urlを利用している。
https://github.com/websanova/js-url

6
6
1

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
6
6