rack-google_analytics 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -13,7 +13,12 @@ Rack middleware to embed Google Analytics tracking code.
13
13
 
14
14
  == Configuring for a Rails App
15
15
 
16
- config.gem "ambethia-rack-google_analytics", :lib => "rack/google_analytics", :source => "http://gems.github.com"
16
+ In your `Gemfile`:
17
+
18
+ gem 'rack-google_analytics'
19
+
20
+ In your `config/application.rb`
21
+
17
22
  config.middleware.use "Rack::GoogleAnalytics", :web_property_id => "UA-0000000-1"
18
23
 
19
24
  == TODO and Motivations
@@ -39,4 +44,4 @@ fixes for these deficiencies.
39
44
 
40
45
  == Copyright
41
46
 
42
- Copyright (c) 2009 Jason L Perry. See LICENSE for details.
47
+ Copyright (c) 2011 Jason L Perry. See LICENSE for details.
@@ -0,0 +1 @@
1
+ require 'rack/google_analytics'
@@ -1,7 +1,7 @@
1
1
  module Rack #:nodoc:
2
2
  class GoogleAnalytics < Struct.new :app, :options
3
3
 
4
- def call env
4
+ def call(env)
5
5
  status, headers, response = app.call(env)
6
6
 
7
7
  if headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/
@@ -18,23 +18,38 @@ module Rack #:nodoc:
18
18
  [status, headers, response]
19
19
  end
20
20
 
21
- private
21
+ protected
22
22
 
23
23
  # Returns JS to be embeded. This takes one argument, a Web Property ID
24
24
  # (aka UA number).
25
- def tracking_code web_property_id
26
- return <<-EOF
25
+ def tracking_code(web_property_id)
26
+ returning_value = <<-EOF
27
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"));
28
+ if (typeof gaJsHost == 'undefined') {
29
+ var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
30
+ document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
31
+ }
30
32
  </script>
31
33
  <script type="text/javascript">
32
34
  try {
33
- var pageTracker = _gat._getTracker("#{web_property_id}");
34
- pageTracker._trackPageview();
35
+ var #{options[:prefix]}pageTracker = _gat._getTracker("#{web_property_id}");
36
+ EOF
37
+ if options[:multiple_top_level_domains]
38
+ returning_value << <<-EOF
39
+ #{options[:prefix]}pageTracker._setDomainName("none");
40
+ #{options[:prefix]}pageTracker._setAllowLinker(true);
41
+ EOF
42
+ elsif options[:domain_name]
43
+ returning_value << <<-EOF
44
+ #{options[:prefix]}pageTracker._setDomainName("#{options[:domain_name]}");
45
+ EOF
46
+ end
47
+
48
+ returning_value << <<-EOF
49
+ #{options[:prefix]}pageTracker._trackPageview();
35
50
  } catch(err) {}</script>
36
51
  EOF
52
+ returning_value
37
53
  end
38
-
39
54
  end
40
55
  end
@@ -1,6 +1,8 @@
1
- require 'test_helper'
1
+ require 'test/unit'
2
2
  require 'rack/mock'
3
3
 
4
+ require 'rack/google_analytics'
5
+
4
6
  class Rack::GoogleAnalyticsTest < Test::Unit::TestCase
5
7
 
6
8
  def test_embed_tracking_code_at_the_end_of_html_body
@@ -19,11 +21,38 @@ class Rack::GoogleAnalyticsTest < Test::Unit::TestCase
19
21
  assert_nothing_raised { request(:body => ["<html></html>"]) }
20
22
  end
21
23
 
22
- def test_shoud_buff_content_length_by_the_size_of_tracker_code
23
- assert_equal HTML_DOC.length + 406, request.content_length
24
+ def test_should_buff_content_length_by_the_size_of_tracker_code
25
+ request do |app, req|
26
+ assert_equal HTML_DOC.length + app.send(:tracking_code, WEB_PROPERTY_ID).length, req.content_length
27
+ end
28
+ end
29
+
30
+ def test_should_include_pageTracker_definition
31
+ assert_match( /#{Regexp.escape('var pageTracker = _gat.')}/, request.body)
32
+ end
33
+
34
+ def test_should_append_prefix_to_pageTracker_definition
35
+ assert_match( /#{Regexp.escape('var conductor_pageTracker = _gat.')}/, request(:prefix => 'conductor_').body)
36
+ end
37
+
38
+ def test_should_allow_multiple_top_level_domains
39
+ assert_match( /#{Regexp.escape('pageTracker._setDomainName("none")')}/, request(:multiple_top_level_domains => true).body)
40
+ assert_match( /#{Regexp.escape('pageTracker._setAllowLinker(true)')}/, request(:multiple_top_level_domains => true).body)
41
+ end
42
+
43
+ def test_multiple_top_level_domains_should_supercede_domain_name
44
+ request(:multiple_top_level_domains => true, :domain_name => '.test.com') do |app, req|
45
+ assert_match( /#{Regexp.escape('pageTracker._setDomainName("none")')}/, req.body)
46
+ assert_no_match( /#{Regexp.escape('pageTracker._setDomainName(".test.com")')}/, req.body)
47
+ end
48
+ end
49
+
50
+ def test_should_allow_domain_name
51
+ assert_match( /#{Regexp.escape('pageTracker._setDomainName(".test.com")')}/, request(:domain_name => '.test.com').body)
24
52
  end
25
53
 
26
54
  private
55
+ WEB_PROPERTY_ID = "UA-0000000-1"
27
56
 
28
57
  TRACKER_EXPECT = /<script.*pageTracker.*<\/script>\s?<\/body>/m
29
58
 
@@ -47,15 +76,20 @@ class Rack::GoogleAnalyticsTest < Test::Unit::TestCase
47
76
  </poem>
48
77
  EOF
49
78
 
50
- def request opts = {}
51
- Rack::MockRequest.new(app(opts)).get("/")
79
+ def request(opts = {})
80
+ @application = app(opts)
81
+ @request = Rack::MockRequest.new(@application).get("/")
82
+ yield(@application, @request) if block_given?
83
+ @request
52
84
  end
53
85
 
54
- def app opts = {}
86
+ def app(opts = {})
87
+ opts = opts.clone
55
88
  opts[:content_type] ||= "text/html"
56
89
  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")
90
+ rack_app = lambda { |env| [200, { 'Content-Type' => opts.delete(:content_type) }, opts.delete(:body)] }
91
+ opts[:web_property_id] ||= WEB_PROPERTY_ID
92
+ Rack::GoogleAnalytics.new(rack_app, opts)
59
93
  end
60
94
 
61
95
  end
metadata CHANGED
@@ -1,7 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-google_analytics
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ prerelease:
5
+ version: 1.0.2
5
6
  platform: ruby
6
7
  authors:
7
8
  - Jason L Perry
@@ -9,11 +10,20 @@ autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
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
13
+ date: 2011-08-18 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ description: Embeds Google Analytics tracking code in the bottom of HTML documents
17
27
  email: jasper@ambethia.com
18
28
  executables: []
19
29
 
@@ -23,44 +33,37 @@ extra_rdoc_files:
23
33
  - LICENSE
24
34
  - README.rdoc
25
35
  files:
26
- - .document
27
- - .gitignore
28
- - LICENSE
29
36
  - README.rdoc
30
- - Rakefile
31
- - VERSION
37
+ - lib/rack-google_analytics.rb
32
38
  - lib/rack/google_analytics.rb
33
- - rack-google_analytics.gemspec
39
+ - LICENSE
34
40
  - test/rack/google_analytics_test.rb
35
- - test/test_helper.rb
36
- has_rdoc: true
37
41
  homepage: http://github.com/ambethia/rack-google_analytics
38
42
  licenses: []
39
43
 
40
44
  post_install_message:
41
- rdoc_options:
42
- - --charset=UTF-8
45
+ rdoc_options: []
46
+
43
47
  require_paths:
44
48
  - lib
45
49
  required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
46
51
  requirements:
47
52
  - - ">="
48
53
  - !ruby/object:Gem::Version
49
54
  version: "0"
50
- version:
51
55
  required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
52
57
  requirements:
53
58
  - - ">="
54
59
  - !ruby/object:Gem::Version
55
60
  version: "0"
56
- version:
57
61
  requirements: []
58
62
 
59
63
  rubyforge_project:
60
- rubygems_version: 1.3.5
64
+ rubygems_version: 1.8.5
61
65
  signing_key:
62
66
  specification_version: 3
63
67
  summary: Google Analytics for Rack applications
64
68
  test_files:
65
69
  - test/rack/google_analytics_test.rb
66
- - test/test_helper.rb
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE
data/.gitignore DELETED
@@ -1,5 +0,0 @@
1
- *.sw?
2
- .DS_Store
3
- coverage
4
- rdoc
5
- pkg
data/Rakefile DELETED
@@ -1,57 +0,0 @@
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 DELETED
@@ -1 +0,0 @@
1
- 1.0.1
@@ -1,50 +0,0 @@
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
data/test/test_helper.rb DELETED
@@ -1,7 +0,0 @@
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
-