rack-piwik 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.md +48 -0
- data/lib/rack/google-analytics.rb +40 -0
- data/lib/rack/piwik.rb +41 -0
- data/lib/rack/templates/async.erb +12 -0
- metadata +71 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Lee Hambley
|
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,48 @@
|
|
1
|
+
# Rack Piwik
|
2
|
+
Adapted from Rack::Piwik
|
3
|
+
|
4
|
+
Simple Rack middleware to help injecting the Piwik tracking code into the footer of your websites.
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
#### Gemfile:
|
9
|
+
gem 'rack-piwik', :require => 'rack/piwik'
|
10
|
+
|
11
|
+
#### Sinatra
|
12
|
+
## app.rb
|
13
|
+
use Rack::Piwik, :piwik_url => '<url of your piwik site here>', :piwik_id => '<your piwik id here>'
|
14
|
+
|
15
|
+
#### Padrino
|
16
|
+
|
17
|
+
## app/app.rb
|
18
|
+
use Rack::Piwik, :piwik_url => '<url of your piwik site here>', :piwik_id => '<your piwik id here>'
|
19
|
+
|
20
|
+
#### Rails
|
21
|
+
|
22
|
+
## environment.rb:
|
23
|
+
config.gem 'rack-piwik', :lib => 'rack/piwik
|
24
|
+
config.middleware.use Rack::Piwik, :piwik_url => '<url of your piwik site here>', :piwik_id => '<your piwik id here>'
|
25
|
+
|
26
|
+
## Thread Safety
|
27
|
+
|
28
|
+
This middleware *should* be thread safe. Although my experience in such areas is limited, having taken the advice of those with more experience; I defer the call to a shallow copy of the environment, if this is of consequence to you please review the implementation.
|
29
|
+
|
30
|
+
## Change Log
|
31
|
+
|
32
|
+
* 0.1.0 Initial Release
|
33
|
+
|
34
|
+
|
35
|
+
## Note on Patches/Pull Requests
|
36
|
+
|
37
|
+
* Fork the project.
|
38
|
+
* Make your feature addition or bug fix.
|
39
|
+
* Add tests for it. This is important so I don't break it in a
|
40
|
+
future version unintentionally.
|
41
|
+
* Commit, do not mess with rakefile, version, or history.
|
42
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
43
|
+
* Send me a pull request. Bonus points for topic branches.
|
44
|
+
|
45
|
+
## Copyright
|
46
|
+
|
47
|
+
Copyright (c) 2009-2011 Lee Hambley. See LICENSE for details.
|
48
|
+
With thanks to Ralph von der Heyden http://github.com/ralph/ and Simon `cimm` Schoeters http://github.com/cimm/ - And the biggest hand to Arthur `achiu` Chiu for the huge work that went into the massive 0.9 re-factor.
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'erb'
|
3
|
+
|
4
|
+
module Rack
|
5
|
+
|
6
|
+
class Piwik
|
7
|
+
|
8
|
+
|
9
|
+
def initialize(app, options = {})
|
10
|
+
raise ArgumentError, "piwik_url must be user" unless options[:piwik_url] and !options[:piwik_url].empty?
|
11
|
+
@app, @options = app, DEFAULT.merge(options)
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env); dup._call(env); end
|
15
|
+
|
16
|
+
def _call(env)
|
17
|
+
@status, @headers, @response = @app.call(env)
|
18
|
+
return [@status, @headers, @response] unless html?
|
19
|
+
response = Rack::Response.new([], @status, @headers)
|
20
|
+
@response.each { |fragment| response.write inject(fragment) }
|
21
|
+
response.finish
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def html?; @headers['Content-Type'] =~ /html/; end
|
27
|
+
|
28
|
+
def inject(response)
|
29
|
+
file = @options[:async] ? 'async' : 'sync'
|
30
|
+
@template ||= ::ERB.new ::File.read ::File.expand_path("../templates/#{file}.erb",__FILE__)
|
31
|
+
if @options[:async]
|
32
|
+
response.gsub(%r{</head>}, @template.result(binding) + "</head>")
|
33
|
+
else
|
34
|
+
response.gsub(%r{</body>}, @template.result(binding) + "</body>")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
data/lib/rack/piwik.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'erb'
|
3
|
+
|
4
|
+
module Rack
|
5
|
+
|
6
|
+
class GoogleAnalytics
|
7
|
+
|
8
|
+
DEFAULT = { :async => true }
|
9
|
+
|
10
|
+
def initialize(app, options = {})
|
11
|
+
raise ArgumentError, "Tracker must be set!" unless options[:tracker] and !options[:tracker].empty?
|
12
|
+
@app, @options = app, DEFAULT.merge(options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(env); dup._call(env); end
|
16
|
+
|
17
|
+
def _call(env)
|
18
|
+
@status, @headers, @response = @app.call(env)
|
19
|
+
return [@status, @headers, @response] unless html?
|
20
|
+
response = Rack::Response.new([], @status, @headers)
|
21
|
+
@response.each { |fragment| response.write inject(fragment) }
|
22
|
+
response.finish
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def html?; @headers['Content-Type'] =~ /html/; end
|
28
|
+
|
29
|
+
def inject(response)
|
30
|
+
file = @options[:async] ? 'async' : 'sync'
|
31
|
+
@template ||= ::ERB.new ::File.read ::File.expand_path("../templates/#{file}.erb",__FILE__)
|
32
|
+
if @options[:async]
|
33
|
+
response.gsub(%r{</head>}, @template.result(binding) + "</head>")
|
34
|
+
else
|
35
|
+
response.gsub(%r{</body>}, @template.result(binding) + "</body>")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<!-- Piwik -->
|
2
|
+
<script type="text/javascript">
|
3
|
+
var pkBaseURL = (("https:" == document.location.protocol) ? "https://<%= @options[:piwik_url]%>/" : "http://<%= @options[:piwik_url]%>/");
|
4
|
+
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
|
5
|
+
</script><script type="text/javascript">
|
6
|
+
try {
|
7
|
+
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", <%= @options[:piwik_id]%>);
|
8
|
+
piwikTracker.trackPageView();
|
9
|
+
piwikTracker.enableLinkTracking();
|
10
|
+
} catch( err ) {}
|
11
|
+
</script>
|
12
|
+
<!-- End Piwik Code -->
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-piwik
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Maxwell Salzberg
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-01-05 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Simple Rack middleware for implementing piwik Analytics racking in your Ruby-Rack based project.
|
22
|
+
email:
|
23
|
+
- maxwell@joindiaspora.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- lib/rack/google-analytics.rb
|
32
|
+
- lib/rack/piwik.rb
|
33
|
+
- lib/rack/templates/async.erb
|
34
|
+
- README.md
|
35
|
+
- LICENSE
|
36
|
+
homepage: https://github.com/leehambley/rack-google-analytics
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
hash: 3
|
50
|
+
segments:
|
51
|
+
- 0
|
52
|
+
version: "0"
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.12
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Rack middleware to inject the Piwik tracking code into outgoing responses. Adapted from rack-google-analytics
|
69
|
+
test_files: []
|
70
|
+
|
71
|
+
has_rdoc:
|