rack-google_analytics 1.0.1

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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jason L Perry
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.rdoc ADDED
@@ -0,0 +1,42 @@
1
+ = Rack::GoogleAnalytics
2
+
3
+ Rack middleware to embed Google Analytics tracking code.
4
+
5
+ == Usage
6
+
7
+ require "rack/google_analytics"
8
+
9
+ use Rack::GoogleAnalytics, :web_property_id => "UA-000000-1"
10
+
11
+ app = lambda { |env| [200, { 'Content-Type' => 'text/html' }, '<html><body></body></html>'] }
12
+ run app
13
+
14
+ == Configuring for a Rails App
15
+
16
+ config.gem "ambethia-rack-google_analytics", :lib => "rack/google_analytics", :source => "http://gems.github.com"
17
+ config.middleware.use "Rack::GoogleAnalytics", :web_property_id => "UA-0000000-1"
18
+
19
+ == TODO and Motivations
20
+
21
+ This isn't very "feature rich", because I've mostly written it as an
22
+ excuse to learn more about rack. Hell, it's possible someone has already done
23
+ it (and better); I didn't bother to look. See the notes below on contributing
24
+ fixes for these deficiencies.
25
+
26
+ * Support for the various tracking options (http://code.google.com/apis/analytics/docs/gaJS/gaJSApi.html)
27
+ * Legacy tracking code
28
+
29
+ == Note on Patches/Pull Requests
30
+
31
+ * Fork the project.
32
+ * Make your feature addition or bug fix.
33
+ * Add tests for it. This is important so I don't break it in a
34
+ future version unintentionally.
35
+ * Commit, do not mess with rakefile, version, or history.
36
+ (if you want to have your own version, that is fine but
37
+ bump version in a commit by itself I can ignore when I pull)
38
+ * Send me a pull request. Bonus points for topic branches.
39
+
40
+ == Copyright
41
+
42
+ Copyright (c) 2009 Jason L Perry. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rack-google_analytics"
8
+ gem.summary = %Q{Google Analytics for Rack applications}
9
+ gem.description = %Q{Embeds GA tracking code in the bottom of HTML documents}
10
+ gem.email = "jasper@ambethia.com"
11
+ gem.homepage = "http://github.com/ambethia/rack-google_analytics"
12
+ gem.authors = ["Jason L Perry"]
13
+ end
14
+ Jeweler::GemcutterTasks.new
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ test.rcov_opts << "--exclude 'var/*,gems/*'"
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install relevance-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION')
47
+ version = File.read('VERSION')
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "rack-google_analytics #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ rdoc .options << "--all"
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.1
@@ -0,0 +1,40 @@
1
+ module Rack #:nodoc:
2
+ class GoogleAnalytics < Struct.new :app, :options
3
+
4
+ def call env
5
+ status, headers, response = app.call(env)
6
+
7
+ if headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/
8
+ body = ""
9
+ response.each { |part| body << part }
10
+ index = body.rindex("</body>")
11
+ if index
12
+ body.insert(index, tracking_code(options[:web_property_id]))
13
+ headers["Content-Length"] = body.length.to_s
14
+ response = [body]
15
+ end
16
+ end
17
+
18
+ [status, headers, response]
19
+ end
20
+
21
+ private
22
+
23
+ # Returns JS to be embeded. This takes one argument, a Web Property ID
24
+ # (aka UA number).
25
+ def tracking_code web_property_id
26
+ return <<-EOF
27
+ <script type="text/javascript">
28
+ var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
29
+ document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
30
+ </script>
31
+ <script type="text/javascript">
32
+ try {
33
+ var pageTracker = _gat._getTracker("#{web_property_id}");
34
+ pageTracker._trackPageview();
35
+ } catch(err) {}</script>
36
+ EOF
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,50 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rack-google_analytics}
8
+ s.version = "1.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jason L Perry"]
12
+ s.date = %q{2009-10-14}
13
+ s.description = %q{Embeds GA tracking code in the bottom of HTML documents}
14
+ s.email = %q{jasper@ambethia.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/rack/google_analytics.rb",
27
+ "rack-google_analytics.gemspec",
28
+ "test/rack/google_analytics_test.rb",
29
+ "test/test_helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/ambethia/rack-google_analytics}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{Google Analytics for Rack applications}
36
+ s.test_files = [
37
+ "test/rack/google_analytics_test.rb",
38
+ "test/test_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ else
47
+ end
48
+ else
49
+ end
50
+ end
@@ -0,0 +1,61 @@
1
+ require 'test_helper'
2
+ require 'rack/mock'
3
+
4
+ class Rack::GoogleAnalyticsTest < Test::Unit::TestCase
5
+
6
+ def test_embed_tracking_code_at_the_end_of_html_body
7
+ assert_match TRACKER_EXPECT, request.body
8
+ end
9
+
10
+ def test_embed_tracking_code_in_xhtml_documents
11
+ assert_match TRACKER_EXPECT, request(:content_type => "application/xhtml+xml").body
12
+ end
13
+
14
+ def test_dont_embed_code_in_non_html_documents
15
+ assert_no_match TRACKER_EXPECT, request(:content_type => "text/xml", :body => [XML_DOC]).body
16
+ end
17
+
18
+ def test_should_not_raise_exception_if_theres_no_html_body_tag
19
+ assert_nothing_raised { request(:body => ["<html></html>"]) }
20
+ end
21
+
22
+ def test_shoud_buff_content_length_by_the_size_of_tracker_code
23
+ assert_equal HTML_DOC.length + 406, request.content_length
24
+ end
25
+
26
+ private
27
+
28
+ TRACKER_EXPECT = /<script.*pageTracker.*<\/script>\s?<\/body>/m
29
+
30
+ HTML_DOC = <<-EOF
31
+ <html>
32
+ <head>
33
+ <title>Rack::GoogleAnalytics</title>
34
+ </head>
35
+ <body>
36
+ <h1>Rack::GoogleAnalytics</h1>
37
+ </body>
38
+ </html>
39
+ EOF
40
+
41
+ XML_DOC = <<-EOF
42
+ <?xml version="1.0" encoding="ISO-8859-1"?>
43
+ <poem>
44
+ <title>Old Pond</title>
45
+ <author>Matsuo Basho</author>
46
+ <body>an ancient pond / a frog jumps in / the splash of water</body>
47
+ </poem>
48
+ EOF
49
+
50
+ def request opts = {}
51
+ Rack::MockRequest.new(app(opts)).get("/")
52
+ end
53
+
54
+ def app opts = {}
55
+ opts[:content_type] ||= "text/html"
56
+ opts[:body] ||= [HTML_DOC]
57
+ rack_app = lambda { |env| [200, { 'Content-Type' => opts[:content_type] }, opts[:body]] }
58
+ Rack::GoogleAnalytics.new(rack_app, :web_property_id => "UA-0000000-1")
59
+ end
60
+
61
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ require 'rack/google_analytics'
7
+
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-google_analytics
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jason L Perry
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-14 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Embeds GA tracking code in the bottom of HTML documents
17
+ email: jasper@ambethia.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - lib/rack/google_analytics.rb
33
+ - rack-google_analytics.gemspec
34
+ - test/rack/google_analytics_test.rb
35
+ - test/test_helper.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/ambethia/rack-google_analytics
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --charset=UTF-8
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.5
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Google Analytics for Rack applications
64
+ test_files:
65
+ - test/rack/google_analytics_test.rb
66
+ - test/test_helper.rb