piwik-tracker 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +0 -0
- data/LICENSE +20 -0
- data/README.md +15 -0
- data/lib/piwik_tracker/piwik.rb +205 -0
- data/lib/piwik_tracker/version.rb +3 -0
- data/lib/piwik_tracker.rb +2 -0
- metadata +62 -0
data/CHANGELOG.md
ADDED
File without changes
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Jens Krämer
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
Piwik-Tracker
|
2
|
+
=============
|
3
|
+
|
4
|
+
http://github.com/jkraemer/piwik-tracker
|
5
|
+
|
6
|
+
Ruby client library for the [Piwik](http://piwik.org) opensource web
|
7
|
+
analytics application.
|
8
|
+
|
9
|
+
With Piwik-Tracker you can record visits, page views and goal conversions
|
10
|
+
in a Piwik server from within any ruby application.
|
11
|
+
|
12
|
+
Think of it as a Ruby version of the piwik.js standard Tracking API.
|
13
|
+
For more information, see http://piwik.org/docs/tracking-api/
|
14
|
+
|
15
|
+
Inspired by the PiwikTracker.php script that comes with Piwik.
|
@@ -0,0 +1,205 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'json'
|
3
|
+
require 'patron'
|
4
|
+
|
5
|
+
module PiwikTracker
|
6
|
+
|
7
|
+
# Piwik HTTP client
|
8
|
+
# piwik = Piwik.new('http://yoursite.com/piwik', 6)
|
9
|
+
# piwik.request(auth_token).
|
10
|
+
# url('http://yoursite.com/some-page.html').
|
11
|
+
# custom_variable(:foobar, 'value').
|
12
|
+
# track_page_view('Page Title')
|
13
|
+
class Piwik
|
14
|
+
|
15
|
+
MAX_INT = 2 ** ((['foo'].pack('p').size * 8) - 1) - 1
|
16
|
+
|
17
|
+
HTTP_TIMEOUT = 20
|
18
|
+
USER_AGENT = "piwik-tracker/#{PiwikTracker::VERSION}"
|
19
|
+
|
20
|
+
attr_accessor :debug
|
21
|
+
|
22
|
+
# base_uri - the location of your Piwik installation, i.e. 'http://yoursite.com/piwik'.
|
23
|
+
# site_id - Id of the site to be tracked
|
24
|
+
def initialize(base_uri, site_id)
|
25
|
+
@base_uri = base_uri
|
26
|
+
@site_id = site_id
|
27
|
+
end
|
28
|
+
|
29
|
+
# sends a tracking request
|
30
|
+
def track(params)
|
31
|
+
send_request params.merge( 'idsite' => @site_id,
|
32
|
+
'rec' => 1,
|
33
|
+
'rand' => rand(MAX_INT) )
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
|
38
|
+
def send_request(params)
|
39
|
+
headers = { 'Accept-Language' => params.delete(:browser_language) }
|
40
|
+
headers['User-Agent'] = params.delete(:user_agent) if params.key?(:user_agent)
|
41
|
+
url = "piwik.php?#{hash_to_querystring(params)}"
|
42
|
+
puts "#{url} / #{headers.inspect}" if @debug
|
43
|
+
http.get url, headers
|
44
|
+
end
|
45
|
+
|
46
|
+
def http
|
47
|
+
@http ||= connect
|
48
|
+
end
|
49
|
+
|
50
|
+
def hash_to_querystring(hash)
|
51
|
+
(keys = hash.keys).inject('') do |query_string, key|
|
52
|
+
query_string << '&' unless key == keys.first
|
53
|
+
query_string << "#{URI.encode(key.to_s)}=#{URI.encode(hash[key].to_s)}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def connect
|
58
|
+
Patron::Session.new.tap do |session|
|
59
|
+
session.timeout = HTTP_TIMEOUT
|
60
|
+
session.base_url = @base_uri
|
61
|
+
session.headers['User-Agent'] = USER_AGENT
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
public
|
67
|
+
|
68
|
+
class Request
|
69
|
+
|
70
|
+
# Piwik API version
|
71
|
+
VERSION = 1
|
72
|
+
|
73
|
+
# length of Piwik visitor ids
|
74
|
+
VISITOR_ID_LENGTH = 16
|
75
|
+
|
76
|
+
def initialize(piwik, auth_token = nil)
|
77
|
+
@piwik = piwik
|
78
|
+
@custom_variables = []
|
79
|
+
@data = {}
|
80
|
+
@data[:token_auth] = auth_token if auth_token
|
81
|
+
end
|
82
|
+
|
83
|
+
# Chainable attribute setters
|
84
|
+
###########################
|
85
|
+
|
86
|
+
# Sets the current URL being tracked
|
87
|
+
def url(url)
|
88
|
+
@data[:url] = url
|
89
|
+
self
|
90
|
+
end
|
91
|
+
|
92
|
+
# Sets the URL referrer used to track Referrers details for new visits.
|
93
|
+
def referrer(url)
|
94
|
+
@data[:urlref] = url
|
95
|
+
self
|
96
|
+
end
|
97
|
+
|
98
|
+
# Sets the attribution information to the visit, so that subsequent Goal conversions are
|
99
|
+
# properly attributed to the right Referrer URL, timestamp, Campaign Name & Keyword.
|
100
|
+
# info - 4 element array of [campaign name, campaign keyword, Timestamp at which the referrer was set, Referrer URL]
|
101
|
+
def attribution_info(info)
|
102
|
+
info = JSON.parse(info) if String === info
|
103
|
+
@data[:_rcn], @data[:_rck], @data[:_refts], @data[:_ref] = info
|
104
|
+
self
|
105
|
+
end
|
106
|
+
|
107
|
+
# Sets Visit Custom Variable.
|
108
|
+
# See http://piwik.org/docs/custom-variables/
|
109
|
+
# slot_id - Custom variable slot ID from 1-5
|
110
|
+
# name - Custom variable name
|
111
|
+
# value - Custom variable value
|
112
|
+
def custom_variable(slot_id, name, value)
|
113
|
+
raise "invalid slot id, has to be between 1 and 5" unless (1..5).include?(slot_id)
|
114
|
+
@custom_variables[slot_id - 1] = [name, value]
|
115
|
+
self
|
116
|
+
end
|
117
|
+
|
118
|
+
def browser_language(lang)
|
119
|
+
@data[:browser_language] = lang
|
120
|
+
self
|
121
|
+
end
|
122
|
+
|
123
|
+
def user_agent(name)
|
124
|
+
@data[:user_agent] = name
|
125
|
+
self
|
126
|
+
end
|
127
|
+
|
128
|
+
# Overrides server date and time for the tracking requests.
|
129
|
+
# By default Piwik will track requests for the "current datetime" but this function allows you
|
130
|
+
# to track visits in the past.
|
131
|
+
# time - ruby Time instance
|
132
|
+
def forced_date_time(time)
|
133
|
+
@data[:cdt] = time.utc.to_i
|
134
|
+
self
|
135
|
+
end
|
136
|
+
|
137
|
+
# Overrides IP address
|
138
|
+
# client_ip - IP address string
|
139
|
+
def ip(client_ip)
|
140
|
+
@data[:cip] = client_ip
|
141
|
+
self
|
142
|
+
end
|
143
|
+
|
144
|
+
# Forces the requests to be recorded for the specified Visitor ID
|
145
|
+
# rather than using the heuristics based on IP and other attributes.
|
146
|
+
# id - 16 hexadecimal characters visitor ID, eg. "33c31e01394bdc63"
|
147
|
+
def visitor_id(id)
|
148
|
+
raise "visitor_id must be exactly #{VISITOR_ID_LENGTH} characters long" unless id.to_s.length == VISITOR_ID_LENGTH
|
149
|
+
@data[:cid] = id
|
150
|
+
self
|
151
|
+
end
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
# Tracking functions
|
156
|
+
###########################
|
157
|
+
|
158
|
+
# Tracks a page view
|
159
|
+
# document_title is the page title as it will appear in the Actions > Page titles report
|
160
|
+
def track_pageview(document_title)
|
161
|
+
@piwik.track request_params.merge(:action_name => document_title)
|
162
|
+
end
|
163
|
+
|
164
|
+
# Records a Goal conversion
|
165
|
+
# goal_id - Id of the goal to record
|
166
|
+
# revenue - revenue for this conversion
|
167
|
+
def track_goal(goal_id, revenue = nil)
|
168
|
+
params = request_params.merge :idgoal => goal_id
|
169
|
+
params[:revenue] = revenue if revenue
|
170
|
+
@piwik.track params
|
171
|
+
end
|
172
|
+
|
173
|
+
# Tracks a download or outlink
|
174
|
+
# action_url - URL of the download or outlink
|
175
|
+
# action_type Type of the action: :download or :link
|
176
|
+
def track_action(action_url, action_type)
|
177
|
+
@piwik.track request_params.merge(action_type => action_url, :redirect => '0')
|
178
|
+
end
|
179
|
+
|
180
|
+
protected
|
181
|
+
|
182
|
+
def request_params
|
183
|
+
@data.dup.tap do |params|
|
184
|
+
params[:_cvar] = @custom_variables.to_json if @custom_variables.any?
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
end
|
189
|
+
|
190
|
+
|
191
|
+
# generates a new request object.
|
192
|
+
#
|
193
|
+
# Some Tracking API functionnality requires express authentication, using either the
|
194
|
+
# Super User token_auth, or a user with 'admin' access to the website.
|
195
|
+
#
|
196
|
+
# The following features require access:
|
197
|
+
# - force the visitor IP
|
198
|
+
# - force the date & time of the tracking requests rather than track for the current datetime
|
199
|
+
# - force Piwik to track the requests to a specific VisitorId rather than use the standard visitor matching heuristic
|
200
|
+
def request(auth_token = nil)
|
201
|
+
Request.new self, auth_token
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: piwik-tracker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- "Jens Kr\xC3\xA4mer"
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-06-13 00:00:00 +02:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: ""
|
18
|
+
email:
|
19
|
+
- jk@jkraemer.net
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- lib/piwik_tracker/piwik.rb
|
28
|
+
- lib/piwik_tracker/version.rb
|
29
|
+
- lib/piwik_tracker.rb
|
30
|
+
- LICENSE
|
31
|
+
- CHANGELOG.md
|
32
|
+
- README.md
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/jkraemer/piwik-tracker
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.3.6
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project: piwik-tracker
|
57
|
+
rubygems_version: 1.6.2
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Record visits, page views, Goals, in a Piwik server
|
61
|
+
test_files: []
|
62
|
+
|