LoginSignup
3
3

More than 5 years have passed since last update.

analytics関連で役立つgem「rack-tracker」についてのメモ

Last updated at Posted at 2015-11-10

GAやVWOなどのanalytics関連のタグを埋め込むときに役立ちそうなGem「rack-tracker」についてメモを残す。

rack-tracker

詳しいことはGithubのページに書いてあるので簡単な使い方をメモする。ほぼ自分のためのメモ。

設定

config/initilizersrack-tracker.rbのようなファイルを作成し以下のように設定を記述する。

rack-tracker.rb
config = MyApplication::Application.config
config.middleware.use(Rack::Tracker) do
  handler :google_analytics, { tracker: 'U-XXXXX-Y' }
end

イベント取得

イベントを送信する時はtrackerメソッドを使い、以下のようなコードをcontrollerなどに書く。

tracker do |t|
  t.google_analytics :send, { type: 'event', category: 'カテゴリ名', action: 'アクション名', label: 'ラベル名', value: '値' }
    end

おまけ

ちなみにGAのみのでよいなら「rack-google-analytics」というのもある。みたところGAだけならこっちの方がシンプルかも

rack-google-analytics

VWOやGoogleTagMangerなど複数のanalytics関連のタグを入れるのであればいちいち手で入れるよりも簡単に配置できる。

YahooTagManagerなど想定外のタグマネージャなんかはここの実装をまねて実装し、設定handlerで同じように登録すればできないことはない(イレギュラーなのでちゃんと動くかは不安)。

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