piwik-tracker 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,32 @@
1
+ module PiwikTracker
2
+ # rack middleware to extract piwik site id and visitor id from request
3
+ # cookies
4
+ class Middleware
5
+
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ site_id, visitor_id = parse_cookie env
12
+ env['piwik'] = {
13
+ :site_id => site_id,
14
+ :visitor_id => visitor_id
15
+ }
16
+ @app.call(env)
17
+ end
18
+
19
+ private
20
+
21
+ def parse_cookie(env)
22
+ request = Rack::Request.new env
23
+ request.cookies.each_pair do |name, value|
24
+ if name =~ /^_pk_id\.(\d+)\.\w+$/
25
+ return [$1.to_i, value.split('.').first]
26
+ end
27
+ end
28
+ [nil, nil]
29
+ end
30
+
31
+ end
32
+ end
@@ -18,31 +18,37 @@ module PiwikTracker
18
18
  USER_AGENT = "piwik-tracker/#{PiwikTracker::VERSION}"
19
19
 
20
20
  attr_accessor :debug
21
-
21
+ attr_writer :logger
22
+
22
23
  # base_uri - the location of your Piwik installation, i.e. 'http://yoursite.com/piwik'.
23
24
  # site_id - Id of the site to be tracked
24
25
  def initialize(base_uri, site_id)
25
26
  @base_uri = base_uri
26
27
  @site_id = site_id
27
28
  end
28
-
29
+
29
30
  # sends a tracking request
30
31
  def track(params)
31
32
  send_request params.merge( 'idsite' => @site_id,
32
33
  'rec' => 1,
33
34
  'rand' => rand(MAX_INT) )
34
35
  end
35
-
36
+
37
+
36
38
  protected
37
-
39
+
40
+ def logger
41
+ @logger ||= (require 'logger'; Logger.new($stdout))
42
+ end
43
+
38
44
  def send_request(params)
39
45
  headers = { 'Accept-Language' => params.delete(:browser_language) }
40
46
  headers['User-Agent'] = params.delete(:user_agent) if params.key?(:user_agent)
41
47
  url = "piwik.php?#{hash_to_querystring(params)}"
42
- puts "#{url} / #{headers.inspect}" if @debug
48
+ logger.debug "Piwik request:\n#{url}\n#{headers.inspect}"
43
49
  http.get url, headers
44
50
  end
45
-
51
+
46
52
  def http
47
53
  @http ||= connect
48
54
  end
@@ -53,7 +59,7 @@ module PiwikTracker
53
59
  query_string << "#{URI.encode(key.to_s)}=#{URI.encode(hash[key].to_s)}"
54
60
  end
55
61
  end
56
-
62
+
57
63
  def connect
58
64
  Patron::Session.new.tap do |session|
59
65
  session.timeout = HTTP_TIMEOUT
@@ -64,15 +70,15 @@ module PiwikTracker
64
70
 
65
71
 
66
72
  public
67
-
73
+
68
74
  class Request
69
-
75
+
70
76
  # Piwik API version
71
77
  VERSION = 1
72
-
78
+
73
79
  # length of Piwik visitor ids
74
80
  VISITOR_ID_LENGTH = 16
75
-
81
+
76
82
  def initialize(piwik, auth_token = nil)
77
83
  @piwik = piwik
78
84
  @custom_variables = []
@@ -202,4 +208,4 @@ module PiwikTracker
202
208
  end
203
209
  end
204
210
 
205
- end
211
+ end
@@ -1,3 +1,3 @@
1
1
  module PiwikTracker
2
- VERSION = "0.1.0"
3
- end
2
+ VERSION = "0.1.1"
3
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: piwik-tracker
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.1.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Jens Kr\xC3\xA4mer"
@@ -10,10 +10,20 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-13 00:00:00 +02:00
13
+ date: 2011-06-14 00:00:00 +02:00
14
14
  default_executable:
15
- dependencies: []
16
-
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: patron
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
17
27
  description: ""
18
28
  email:
19
29
  - jk@jkraemer.net
@@ -24,6 +34,7 @@ extensions: []
24
34
  extra_rdoc_files: []
25
35
 
26
36
  files:
37
+ - lib/piwik_tracker/middleware.rb
27
38
  - lib/piwik_tracker/piwik.rb
28
39
  - lib/piwik_tracker/version.rb
29
40
  - lib/piwik_tracker.rb