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