Skip to content
프로젝트

kangguru/rack-google-analytics

GitHub에서 보기

소개

Ruby-Rack 기반 프로젝트에 Google Analytics 추적을 구현하는 간단한 Rack 미들웨어입니다. 동기 및 비동기 삽입과 로드 옵션 설정을 지원합니다.

기타 프로젝트

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

PLEASE READ:

There is a new gem for various tracking and analytics services, which includes all the functionality of rack-google-analytics. So if you need more than just google, checkout the rack-tracker gem.


Rack google Analytics

Build Status

Simple Rack middleware to help injecting the Google Analytics tracking code in your website.

This middleware injects the Google Analytics tracking code into the correct place of any request only when the response's Content-Type header contains html (therefore text/html and similar).

Usage

Gemfile

gem 'rack-google-analytics'

Sinatra

## app.rb
use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x'

Padrino

## app/app.rb
use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x'

Rails 3.X and Rails 4.X

## application.rb:
config.middleware.use Rack::GoogleAnalytics, :tracker => 'UA-xxxxxx-x'

Options

If you are not sure what's best, go with the defaults, and read here if you should opt-out.

Event Tracking

In your application controller, you may track an event. For example:

ga_track_event("Users", "Login", "Standard")

See https://developers.google.com/analytics/devguides/collection/analyticsjs/events

Custom Push

In your application controller, you may push arbritrary data. For example:

ga_push("_addItem", "ID", "SKU")

Dynamic Tracking Code

You may instead define your tracking code as a lambda taking the Rack environment, so that you may set the tracking code dynamically based upon information in the Rack environment. For example:

config.middleware.use Rack::GoogleAnalytics, :tracker => lambda { |env|
        return env[:site_ga].tracker if env[:site_ga]
}

Special use case: Event tracking only

If you already set up your Google Analytics analytics.js tracker object with pageview tracking in your templates/frontend (inside the <head>), the only thing you might want to use the rack-google-analytics middleware for is to track server-side events which you can't properly track in the forntend. In that case simply use the middleware without specifying the :tracker option, then it will only render the event tracking code (ga('send', hitType: 'event', ..)) and nothing else.

config.middleware.use Rack::GoogleAnalytics

Thread Safety

This middleware should be thread safe. Although my experience in such areas is limited, having taken the advice of those with more experience; I defer the call to a shallow copy of the environment, if this is of consequence to you please review the implementation.

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright (c) 2009-2012 Lee Hambley. See LICENSE for details. With thanks to Ralph von der Heyden and Simon Schoeters - And the biggest hand to Arthur Chiu for the huge work that went into the massive 0.9 re-factor.

관련 프로젝트 살펴보기

불러오는 중…