rack-google-analytics 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -0
- data/lib/rack/google-analytics.rb +41 -0
- data/lib/rack/templates/async.erb +20 -0
- data/lib/rack/templates/sync.erb +9 -0
- metadata +5 -2
data/README.md
CHANGED
@@ -24,6 +24,7 @@ This middleware *should* be thread safe. Although my experience in such areas is
|
|
24
24
|
|
25
25
|
## Change Log
|
26
26
|
|
27
|
+
* 0.9.2 Fixed a bug with lots of missing files from the Gem... how silly!
|
27
28
|
* 0.9.1 Updated readme to reflect 0.9.0 merge from achiu
|
28
29
|
* 0.9.0 Include name changed from 'rack-google-analytics' to 'rack/google-analytics' more inline with the norm
|
29
30
|
* 0.6.0 Class now named Rack::GoogleAnalytics, in 0.5 and earlier this was incorrectly documented as Rack::GoogleTracker
|
@@ -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>}, "</body>" + @template.result(binding))
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
<script type="text/javascript">
|
2
|
+
|
3
|
+
var _gaq = _gaq || [];
|
4
|
+
_gaq.push(['_setAccount', <%= @options[:tracker].inspect %>]);
|
5
|
+
<% if @options[:multiple] %>
|
6
|
+
_gaq.push(['_setDomainName', <%= @options[:domain].inspect %>]);
|
7
|
+
<% end %>
|
8
|
+
<% if @options[:top_level] %>
|
9
|
+
_gaq.push(['_setDomainName', 'none']);
|
10
|
+
_gaq.push(['_setAllowLinker', true]);
|
11
|
+
<% end %>
|
12
|
+
_gaq.push(['_trackPageview']);
|
13
|
+
|
14
|
+
(function() {
|
15
|
+
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
16
|
+
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
17
|
+
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
18
|
+
})();
|
19
|
+
|
20
|
+
</script>
|
@@ -0,0 +1,9 @@
|
|
1
|
+
<script type="text/javascript">
|
2
|
+
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
|
3
|
+
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
|
4
|
+
</script>
|
5
|
+
<script type="text/javascript">
|
6
|
+
try{
|
7
|
+
var pageTracker = _gat._getTracker(<%= @options[:tracker].inspect %>);
|
8
|
+
pageTracker._trackPageview();
|
9
|
+
} catch(err) {}</script>
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 9
|
8
|
-
-
|
9
|
-
version: 0.9.
|
8
|
+
- 2
|
9
|
+
version: 0.9.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Lee Hambley
|
@@ -31,6 +31,9 @@ files:
|
|
31
31
|
- LICENSE
|
32
32
|
- README.md
|
33
33
|
- Rakefile
|
34
|
+
- lib/rack/google-analytics.rb
|
35
|
+
- lib/rack/templates/async.erb
|
36
|
+
- lib/rack/templates/sync.erb
|
34
37
|
has_rdoc: true
|
35
38
|
homepage: http://github.com/leehambley/rack-google-analytics
|
36
39
|
licenses: []
|