rakwik 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +40 -1
- data/lib/rakwik/helpers.rb +28 -0
- data/lib/rakwik/tracker.rb +30 -8
- data/lib/rakwik/version.rb +1 -1
- metadata +4 -3
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
*Server-side* integration of web tracking methods does not require Javascript snippets
|
4
4
|
or tracking images to be includd in the actual frontend. Rakwik implements asynchronous
|
5
|
-
tracking, so it tries to keep the time low
|
5
|
+
tracking, so it tries to keep the time low that is needed to count a particular request.
|
6
6
|
|
7
7
|

|
8
8
|
|
@@ -49,6 +49,45 @@ The `:token_auth` is needed since Rakwik will tell Piwik to record hits from ano
|
|
49
49
|
than its own. The token_auth must be either the Super User token_auth, or a user with
|
50
50
|
"admin" permission for this website ID.
|
51
51
|
|
52
|
+
### Action names
|
53
|
+
|
54
|
+
Piwik allows to set a custom action name which will be used in reports instead of the original
|
55
|
+
URL. To use it from your Rails application, include it into your controller like such:
|
56
|
+
|
57
|
+
require 'rakwik/helpers'
|
58
|
+
|
59
|
+
class ApplicationController < ActionController::Base
|
60
|
+
# ...
|
61
|
+
include Rakwik::Helpers
|
62
|
+
end
|
63
|
+
|
64
|
+
In the particular controller activate it by using the `action_name` class method:
|
65
|
+
|
66
|
+
class BooksController < ApplicationController
|
67
|
+
action_name :page_title
|
68
|
+
|
69
|
+
# GET /books
|
70
|
+
# GET /books.xml
|
71
|
+
def index
|
72
|
+
@books = Book.all
|
73
|
+
@page_title = "Books"
|
74
|
+
|
75
|
+
respond_with @books
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
Currently, `action_name` points to a instance variable.
|
80
|
+
|
81
|
+
## TODO
|
82
|
+
|
83
|
+
* ~~Handle tracking cookies~~ no way to do this asynchronously
|
84
|
+
* Implement a helper to set the action title ~~from the controller~~ or view
|
85
|
+
* Implement a way to provide custom variables from the controller or view
|
86
|
+
* Implement a way to detect client capabilities without a separate request
|
87
|
+
* Detect [Warden](/hassox/warden) based credentials, eg. from [Devise](/plataformatec/devise)
|
88
|
+
* ~~Track 404 responses~~
|
89
|
+
* Implement meaningful specs
|
90
|
+
|
52
91
|
## Reference
|
53
92
|
|
54
93
|
* http://piwik.org/docs/tracking-api/reference/
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Rakwik
|
2
|
+
module Helpers
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
base.send(:include, InstanceMethods)
|
6
|
+
base.class_eval do
|
7
|
+
after_filter :set_action_name
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def rakwik
|
13
|
+
@rakwik ||= {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def action_name(var_name)
|
17
|
+
rakwik[:action_name] = var_name
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module InstanceMethods
|
22
|
+
def set_action_name
|
23
|
+
return if self.class.rakwik[:action_name].nil?
|
24
|
+
request.env['rakwik.action_name'] = instance_variable_get("@#{self.class.rakwik[:action_name]}")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/rakwik/tracker.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
require 'em-http'
|
2
|
-
require 'pp'
|
3
2
|
|
4
3
|
module Rakwik
|
5
4
|
class Tracker
|
6
5
|
|
7
6
|
include Rack::Response::Helpers
|
8
7
|
|
9
|
-
DEFAULT = {
|
8
|
+
DEFAULT = {
|
9
|
+
:track_404 => true
|
10
|
+
}
|
10
11
|
|
11
12
|
def initialize(app, options = {})
|
12
13
|
@app, @options = app, DEFAULT.merge(options)
|
@@ -16,11 +17,15 @@ module Rakwik
|
|
16
17
|
|
17
18
|
def call(env)
|
18
19
|
@status, @headers, @body = @app.call(env)
|
19
|
-
track Rack::Request.new(env) if
|
20
|
+
track Rack::Request.new(env) if track?
|
20
21
|
[@status, @headers, @body]
|
21
22
|
end
|
22
23
|
|
23
24
|
private
|
25
|
+
|
26
|
+
def track?
|
27
|
+
ok? || (not_found? && @options[:track_404] === true)
|
28
|
+
end
|
24
29
|
|
25
30
|
def piwik_url
|
26
31
|
@options[:piwik_url]
|
@@ -33,23 +38,40 @@ module Rakwik
|
|
33
38
|
def token_auth
|
34
39
|
@options[:token_auth]
|
35
40
|
end
|
36
|
-
|
37
|
-
def
|
41
|
+
|
42
|
+
def extract(request)
|
38
43
|
header = {
|
39
|
-
'User-Agent' => request.user_agent
|
40
|
-
'Accept-Language' => request.env["HTTP_ACCEPT_LANGUAGE"]
|
44
|
+
'User-Agent' => request.user_agent
|
41
45
|
}
|
46
|
+
header['Accept-Language'] = request.env['HTTP_ACCEPT_LANGUAGE'] unless request.env['HTTP_ACCEPT_LANGUAGE'].nil?
|
47
|
+
header['DNT'] = request.env['HTTP_DNT'] unless request.env['HTTP_DNT'].nil?
|
42
48
|
data = {
|
43
49
|
'idsite' => piwik_id,
|
44
50
|
'token_auth' => token_auth,
|
45
51
|
'rec' => 1,
|
46
52
|
'url' => request.url,
|
47
53
|
'cip' => request.ip,
|
54
|
+
'rand' => rand(1000000),
|
48
55
|
'apiv' => 1
|
49
56
|
}
|
57
|
+
data['action_name'] = request.env['rakwik.action_name'] unless request.env['rakwik.action_name'].nil?
|
50
58
|
data['urlref'] = request.referer unless request.referer.nil?
|
59
|
+
|
60
|
+
if not_found? && @options[:track_404] === true
|
61
|
+
data['action_name'] = "404/URL = #{data['url'].gsub(/\//, '%2f')}/From = #{data['urlref'].gsub(/\//, '%2f')}"
|
62
|
+
end
|
63
|
+
|
64
|
+
[header, data]
|
65
|
+
end
|
66
|
+
|
67
|
+
def track(request)
|
68
|
+
h, d = extract(request)
|
51
69
|
EventMachine.schedule do
|
52
|
-
http = connection(piwik_url).get :head =>
|
70
|
+
http = connection(piwik_url).get :head => h, :query => d
|
71
|
+
http.errback {
|
72
|
+
time = Time.now.strftime("%Y-%m-%d %H:%M:%S")
|
73
|
+
request.env['rack.errors'].puts "[#{time}] ERROR Rakwik::Tracker: #{http.error}"
|
74
|
+
}
|
53
75
|
end
|
54
76
|
end
|
55
77
|
|
data/lib/rakwik/version.rb
CHANGED
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.2
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Christian Aust
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-06-
|
18
|
+
date: 2012-06-20 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rack
|
@@ -78,6 +78,7 @@ files:
|
|
78
78
|
- README.md
|
79
79
|
- Rakefile
|
80
80
|
- lib/rakwik.rb
|
81
|
+
- lib/rakwik/helpers.rb
|
81
82
|
- lib/rakwik/tracker.rb
|
82
83
|
- lib/rakwik/version.rb
|
83
84
|
- rakwik.gemspec
|