Sha256: c0e2238c5b4c6346714bc91c8320b592f9bd45fb95ce9c836249108eb8535f35
Contents?: true
Size: 1.48 KB
Versions: 2
Compression:
Stored size: 1.48 KB
Contents
module Metry module Rack class Tracking COOKIE = "_metry" def initialize(app) @app = app @storage = Metry.current end def call(env) request = ::Rack::Request.new(env) visitor = find_or_create_visitor(request) env["metry.visitor"] = visitor env["metry.event"] = event = build_event(request) status, headers, body = @app.call(env) response = ::Rack::Response.new(body, status, headers) save_visitor(response, visitor) unless env["metry.ignore"] == "true" event["status"] = status.to_s event["visitor"] = visitor['_id'].to_s @storage.add_event(event) end response.to_a end def build_event(request) { "event" => "pageview", "path" => request.fullpath, "time" => Time.now.to_f, "ip" => request.ip, "host" => request.host, "method" => request.request_method, "referrer" => request.env["HTTP_REFERER"], "user_agent" => request.env["HTTP_USER_AGENT"] } end def find_or_create_visitor(request) @storage.visitor(request.cookies[COOKIE]) || @storage.new_visitor end def save_visitor(response, visitor) @storage.save_visitor(visitor) response.set_cookie(COOKIE, :value => visitor['_id'].to_s, :expires => (Time.now+(60*60*24*365*20)), :path => '/') end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
metry-2.0.1 | lib/metry/rack/tracking.rb |
metry-2.0.2 | lib/metry/rack/tracking.rb |