Skip to content
This repository has been archived by the owner on Nov 14, 2020. It is now read-only.

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
Track 404, forward DNT header if present
  • Loading branch information
datenimperator committed Jun 20, 2012
1 parent edb42d5 commit 1c5bab1
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions lib/rakwik/tracker.rb
@@ -1,12 +1,13 @@
require 'em-http'
require 'pp'

module Rakwik
class Tracker

include Rack::Response::Helpers

DEFAULT = {}
DEFAULT = {
:track_404 => true
}

def initialize(app, options = {})
@app, @options = app, DEFAULT.merge(options)
Expand All @@ -16,11 +17,15 @@ def initialize(app, options = {})

def call(env)
@status, @headers, @body = @app.call(env)
track Rack::Request.new(env) if ok?
track Rack::Request.new(env) if track?
[@status, @headers, @body]
end

private

def track?
ok? || (not_found? && @options[:track_404] === true)
end

def piwik_url
@options[:piwik_url]
Expand All @@ -33,23 +38,39 @@ def piwik_id
def token_auth
@options[:token_auth]
end

def track(request)
def extract(request)
header = {
'User-Agent' => request.user_agent,
'Accept-Language' => request.env["HTTP_ACCEPT_LANGUAGE"]
'User-Agent' => request.user_agent
}
header['Accept-Language'] = request.env["HTTP_ACCEPT_LANGUAGE"] unless request.env["HTTP_ACCEPT_LANGUAGE"].nil?
header['DNT'] = request.env["HTTP_DNT"] unless request.env["HTTP_DNT"].nil?
data = {
'idsite' => piwik_id,
'token_auth' => token_auth,
'rec' => 1,
'url' => request.url,
'cip' => request.ip,
'rand' => rand(1000000),
'apiv' => 1
}
data['urlref'] = request.referer unless request.referer.nil?

if not_found? && @options[:track_404] === true
data['action_name'] = "404/URL = #{data['url']}/FROM= #{data['urlref']}"
end

[header, data]
end

def track(request)
h, d = extract(request)
EventMachine.schedule do
http = connection(piwik_url).get :head => header, :query => data
http = connection(piwik_url).get :head => h, :query => d
http.errback {
time = Time.now.strftime("%Y-%m-%d %H:%M:%S")
request.env['rack.errors'].puts "[#{time}] ERROR Rakwik::Tracker: #{http.error}"
}
end
end

Expand Down

0 comments on commit 1c5bab1

Please sign in to comment.