bigbro 0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,62 @@
1
+ BigBro: A Google Analytics plugin for Rails
2
+ ===========================================
3
+
4
+ Installation
5
+ ------------
6
+
7
+ gem install bigbro
8
+
9
+ Usage
10
+ -----
11
+
12
+ After the plugin has been loaded, you'll have an `analytics` helper to use
13
+ in your views. It generates an optimized version of the Google Analytics
14
+ code, and a `<noscript>` tag containing the direct path to the `__utm.gif`
15
+ image, to track JS-disabled browsers as well.
16
+
17
+ The `analytics` helper tracks the current page load by default, you can
18
+ disable this behaviour by passing the `:track => false` option.
19
+
20
+ Configuration
21
+ -------------
22
+
23
+ You must set your analytics account via the `BigBro.set()` method
24
+ in your `config/environment.rb`:
25
+
26
+ BigBro.set(:account => 'UA-12345-67')
27
+
28
+ In production mode, the `.set()` method will raise an `ArgumentError` if
29
+ no account is provided, unless the `:disabled` option is set to `true`.
30
+
31
+ We use these switches to allow the developer to run the application in
32
+ production mode on `localhost` while not sending requests to Analytics.
33
+
34
+ We know that the ga.js is empty if the `Referer` is `localhost`, but
35
+ there are situations in which the referer is reset, thus a complete disable
36
+ is necessary.
37
+
38
+ In development mode the plugin is always disabled - unless you set an
39
+ account via the `set` method.
40
+
41
+ Testing
42
+ -------
43
+
44
+ A simple `assert_analytics()` helper is included in to aid verifying
45
+ that the layouts include the `analytics` helper. Its usage is super
46
+ simple:
47
+
48
+ class FooControllerTest < ActionController::TestCase
49
+ context "An user" do
50
+ should "be tracked by analytics" do
51
+ get :index
52
+ assert_analytics
53
+ end
54
+ end
55
+ end
56
+
57
+
58
+ Compatibility
59
+ -------------
60
+
61
+ Tested with Rails 2.3.8 with the `rails_xss` plugin installed,
62
+ running under Ruby 1.9.1-p378.
@@ -0,0 +1,49 @@
1
+ require 'rake'
2
+ require 'rake/rdoctask'
3
+
4
+ require 'lib/bigbro'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gemspec|
9
+ gemspec.name = 'bigbro'
10
+
11
+ gemspec.summary = 'A Google Analytics plugin for Rails'
12
+ gemspec.description = 'BigBro provides view helpers to generate Analytics code ' \
13
+ '(with the noscript counterpart), test helpers to assert your ' \
14
+ 'page contains the tracking code and a configuration method to '\
15
+ 'set your GA account from environment.rb'
16
+
17
+ gemspec.authors = ['Marcello Barnaba']
18
+ gemspec.email = 'vjt@openssl.it'
19
+ gemspec.homepage = 'http://github.com/vjt/bigbro'
20
+
21
+ gemspec.files = %w( README.md Rakefile rails/init.rb ) + Dir['lib/**/*']
22
+ gemspec.extra_rdoc_files = %w( README.md )
23
+ gemspec.has_rdoc = true
24
+
25
+ gemspec.version = BigBro::Version
26
+ gemspec.date = '2010-11-23'
27
+
28
+ gemspec.require_path = 'lib'
29
+
30
+ gemspec.add_dependency('rails', '~> 3.0')
31
+ end
32
+ rescue LoadError
33
+ puts 'Jeweler not available. Install it with: gem install jeweler'
34
+ end
35
+
36
+ desc 'Generate the rdoc'
37
+ Rake::RDocTask.new do |rdoc|
38
+ rdoc.rdoc_files.add %w( README.md lib/**/*.rb )
39
+
40
+ rdoc.main = 'README.md'
41
+ rdoc.title = 'BigBro: A Google Analytics plugin for Rails'
42
+ end
43
+
44
+ desc 'Will someone help write tests?'
45
+ task :default do
46
+ puts
47
+ puts 'Can you help in writing tests? Please do :-)'
48
+ puts
49
+ end
@@ -0,0 +1,158 @@
1
+ require 'bigbro/railtie' if defined? Rails
2
+
3
+ module BigBro
4
+ Version = '0.9'
5
+
6
+ module Helpers
7
+ # Embeds the optimized Analytics code and the noscript tag with
8
+ # the direct path to the __utm.gif image into the current page.
9
+ #
10
+ # If the `:track` option is set to false, the current page load
11
+ # is *not* automatically tracked: you should track it later via
12
+ # the JS API.
13
+ #
14
+ def analytics(options = {})
15
+ return if BigBro.disabled?
16
+
17
+ track = options.has_key?(:track) ? options[:track] : true
18
+
19
+ ga_host = BigBro.host_for(request)
20
+ ga_cmds = [['_setAccount', BigBro.account]]
21
+ ga_cmds.push ['_setDomainName', BigBro.domain] if BigBro.domain
22
+ ga_cmds.concat(options[:commands]) if options[:commands]
23
+ ga_cmds.push ['_trackPageview'] if track
24
+
25
+ code = ''
26
+ code.concat javascript_tag(%(
27
+ var _gaq = #{ga_cmds.to_json};
28
+ $(document).ready (function () { // Because of Opera and FF <= 3.5
29
+ try {
30
+ (function(d, t, a) {
31
+ var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
32
+ g[a]=a;g.src='#{ga_host}/ga.js';s.parentNode.insertBefore(g,s);
33
+ }) (document, 'script', 'async');
34
+ } catch (err) {}
35
+ });
36
+ ))
37
+
38
+ code.concat(content_tag(:noscript,
39
+ image_tag(BigBro.noscript_image_path_for(request, ga_host),
40
+ :border => '0', :alt => '').html_safe
41
+ ))
42
+
43
+ return code.html_safe
44
+ end
45
+ end
46
+
47
+ class << self
48
+ attr_accessor :account, :domain, :disabled
49
+
50
+ # Sets the Analytics account and *enforces* it to be set
51
+ # in production mode.
52
+ #
53
+ # If you're developing and want to run your local copy
54
+ # in production mode, you can either pass an invalid
55
+ # account (e.g. to check how the JS code is generated)
56
+ # or pass the :disabled option set to true.
57
+ #
58
+ # In test mode, the account is always set to the dummy
59
+ # "UA-420-THEBRAIN" string.
60
+ #
61
+ # Sets the 'UA-12345-67' account:
62
+ #
63
+ # BigBro.set(:account => 'UA-12345-67')
64
+ #
65
+ # Sets the 'UA-12345-67' account and the 'foo.com' domain:
66
+ #
67
+ # BigBro.set(:account => 'UA-12345-67', :domain => 'foo.com')
68
+ #
69
+ # Disables analytics code generation:
70
+ #
71
+ # BigBro.set(:disabled => true)
72
+ #
73
+ def set(options = {})
74
+ self.account, self.disabled, self.domain =
75
+ options.values_at(:account, :disabled, :domain)
76
+
77
+ if Rails.env.production?
78
+ if self.account.blank? && !self.disabled
79
+ raise ArgumentError, 'BigBro: analytics configuration missing'
80
+ end
81
+ elsif Rails.env.test?
82
+ self.account = 'UA-420-THEBRAIN'
83
+ end
84
+ end
85
+
86
+ # In development mode the Analytics code is always disabled,
87
+ # or it can be disabled manually via the configuration.
88
+ #
89
+ # If no account is set, the code disables itself. Maybe the
90
+ # check in the set() method should be moved here, we'll see.
91
+ #
92
+ def disabled?
93
+ self.disabled || self.account.blank? || Rails.env.development?
94
+ end
95
+
96
+ # Returns the analytics host for the given request (SSL or not)
97
+ #
98
+ def host_for(request)
99
+ (request.ssl? ? 'https://ssl' : 'http://www') + '.google-analytics.com'
100
+ end
101
+
102
+ # Returns the noscript image path for the given request and GA Host
103
+ #
104
+ def noscript_image_path_for(request, ga_host)
105
+ cookie = rand( 89_999_999) + 10_000_000
106
+ req_no = rand(8_999_999_999) + 1_000_000_000
107
+ random = rand(1_147_483_647) + 1_000_000_000
108
+ now = Time.now.to_i
109
+
110
+ referer = request.referer.blank? ? '-' : CGI.escape(request.referer)
111
+ path = request.path.blank? ? '/' : CGI.escape(request.path)
112
+
113
+ utmcc =
114
+ "__utma%3D#{cookie}.#{random}.#{now}.#{now}.#{now}.2%3B%2B" \
115
+ "__utmz%3D#{cookie}.#{now}.2.2." \
116
+ "utmccn%3D(direct)%7C" \
117
+ "utmcsr%3D(direct)%7C" \
118
+ "utmcmd%3D(none)%3B%2B" \
119
+ "__utmv%3D#{cookie}.noscript%3B"
120
+
121
+ "#{ga_host}/__utm.gif?" \
122
+ "utmn=#{req_no}&" \
123
+ "utmac=#{account}&" \
124
+ "utmhn=#{request.host}&" \
125
+ "utmr=#{referer}&" \
126
+ "utmp=#{path}&" \
127
+ "utmcc=#{utmcc}&" \
128
+ 'utmwv=1&' \
129
+ 'utmje=0&' \
130
+ 'utmsr=-&' \
131
+ 'utmsc=-&' \
132
+ 'utmul=-&' \
133
+ 'utmfl=-&' \
134
+ 'utmdt=-'
135
+ end
136
+ end
137
+
138
+ module TestHelpers
139
+ # Asserts the GA <script> tag, with not much effort;
140
+ # Asserts the noscript tag with the descendant img.
141
+ #
142
+ def assert_analytics
143
+ host = BigBro.host_for(@request)
144
+
145
+ assert_tag :tag => 'script', :content => /#{host}\/ga.js/
146
+
147
+ assert_tag :tag => 'noscript',
148
+ :descendant => {
149
+ :tag => 'img',
150
+ :attributes => {
151
+ :src => /#{host}\/__utm.gif\?/,
152
+ :border => '0',
153
+ :alt => ''
154
+ }
155
+ }
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,22 @@
1
+ require 'bigbro'
2
+
3
+ module BigBro
4
+
5
+ if defined? Rails::Railtie
6
+ class Railtie < Rails::Railtie
7
+ initializer 'bigbro.insert_into_action_view' do
8
+ ActiveSupport.on_load :action_view do
9
+ BigBro::Railtie.insert
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ class Railtie
16
+ def self.insert
17
+ ActionView::Base.instance_eval { include BigBro::Helpers }
18
+ ActiveSupport::TestCase.instance_eval { include BigBro::TestHelpers } if Rails.env.test?
19
+ end
20
+ end
21
+
22
+ end
@@ -0,0 +1,2 @@
1
+ require 'bigbro/railtie'
2
+ BigBro::Railtie.insert
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bigbro
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 9
8
+ version: "0.9"
9
+ platform: ruby
10
+ authors:
11
+ - Marcello Barnaba
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-11-23 00:00:00 +01:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: rails
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 3
29
+ - 0
30
+ version: "3.0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: BigBro provides view helpers to generate Analytics code (with the noscript counterpart), test helpers to assert your page contains the tracking code and a configuration method to set your GA account from environment.rb
34
+ email: vjt@openssl.it
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - README.md
41
+ files:
42
+ - README.md
43
+ - Rakefile
44
+ - lib/bigbro.rb
45
+ - lib/bigbro/railtie.rb
46
+ - rails/init.rb
47
+ has_rdoc: true
48
+ homepage: http://github.com/vjt/bigbro
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.3.7
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: A Google Analytics plugin for Rails
79
+ test_files: []
80
+