rubaidh-google_analytics 1.0.20081111
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +122 -0
- data/Rakefile +22 -0
- data/lib/rubaidh/google_analytics.rb +233 -0
- data/lib/rubaidh/view_helpers.rb +67 -0
- data/rails/init.rb +5 -0
- data/test/google_analytics_test.rb +119 -0
- data/test/test_helper.rb +10 -0
- data/test/view_helpers_test.rb +56 -0
- metadata +69 -0
data/README.rdoc
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
= GoogleAnalytics
|
2
|
+
|
3
|
+
This plugin enables Google Analytics support in your application. By default
|
4
|
+
it will output the analytics code for every single page automatically, if it
|
5
|
+
is configured correctly. This is done by adding:
|
6
|
+
|
7
|
+
Rubaidh::GoogleAnalytics.tracker_id = 'UA-12345-67'
|
8
|
+
|
9
|
+
to your <tt>config/environment.rb</tt>, inserting your own tracker id. This
|
10
|
+
can be discovered by looking at the value assigned to +_uacct+ in the
|
11
|
+
Javascript code.
|
12
|
+
|
13
|
+
If you want to disable the code insertion for particular pages, add the
|
14
|
+
following to controllers that don't want it:
|
15
|
+
|
16
|
+
skip_after_filter :add_google_analytics_code
|
17
|
+
|
18
|
+
If you are running rails 2.1 or above add install this by adding:
|
19
|
+
|
20
|
+
config.gem 'rubaidh-google_analytics', :lib => 'rubaidh/google_analytics', :source => 'http://gems.github.com'
|
21
|
+
|
22
|
+
and run:
|
23
|
+
|
24
|
+
rake gems:install
|
25
|
+
|
26
|
+
Simple. :-)
|
27
|
+
|
28
|
+
|
29
|
+
== Note
|
30
|
+
|
31
|
+
This version of the plugin uses the new Google Analytics code (ga.js) by
|
32
|
+
default. To use the legacy tracking code add the following line to your
|
33
|
+
<tt>config/environment.rb</tt>:
|
34
|
+
|
35
|
+
Rubaidh::GoogleAnalytics.legacy_mode = true
|
36
|
+
|
37
|
+
== Tracking outbound Links
|
38
|
+
|
39
|
+
Google Analytics only tracks intra-site links by default. To create an
|
40
|
+
outbound link that is tracked use the link_to_tracked helper:
|
41
|
+
|
42
|
+
link_to_tracked(name, track_path = "/", options = {}, html_options = {})
|
43
|
+
|
44
|
+
You can use the track_path parameter to group your outbound links into logical
|
45
|
+
folders inside of Google Analytics.
|
46
|
+
|
47
|
+
The other forms of link_to are also supported:
|
48
|
+
|
49
|
+
link_to_tracked_if(condition, name, track_path = "/", options = {}, html_options = {}, &block)
|
50
|
+
link_to_tracked_unless(condition, name, track_path = "/", options = {}, html_options = {}, &block)
|
51
|
+
link_to_tracked_unless_current(name, track_path = "/", options = {}, html_options = {}, &block)
|
52
|
+
|
53
|
+
To track outbound links, you should set
|
54
|
+
|
55
|
+
Rubaidh::GoogleAnalytics.defer_load = false
|
56
|
+
|
57
|
+
This will move the tracking javascript to the top of your page.
|
58
|
+
(see http://www.google.com/support/googleanalytics/bin/answer.py?answer=55527&topic=11006)
|
59
|
+
|
60
|
+
Tracked links respect the legacy_mode flag.
|
61
|
+
|
62
|
+
Note: Link-tracking works by inserting onclick() code in the HTML. Because of
|
63
|
+
this, it will overwrite any onclick that you insert in the html_options hash.
|
64
|
+
|
65
|
+
== Using local copies of the Analytics Javascript files
|
66
|
+
|
67
|
+
Under certain circumstances you might find it valuable to serve a copy of the
|
68
|
+
Analytics JavaScript directly from your server to your visitors, and not
|
69
|
+
directly from Google. If your visitors are geograhically very far from Google,
|
70
|
+
or if they have low quality international bandwidth, the loading time for the
|
71
|
+
Analytics JS might kill the user experience and force you to remove the
|
72
|
+
valuable tracking code from your site.
|
73
|
+
|
74
|
+
This plugin now supports local copies of the legacy and new Analytics
|
75
|
+
JavaScript files, updated via a rake task and served courtesy of the Rails
|
76
|
+
AssetTagHelper methods. So even if you use asset hosts, the JS will be served
|
77
|
+
from the correct source and under the correct protocol (HTTP/HTTPS).
|
78
|
+
|
79
|
+
To enable cached copies and the following to your initialization code:
|
80
|
+
|
81
|
+
Rubaidh::GoogleAnalytics.local_javascript = true
|
82
|
+
|
83
|
+
Use the following rake task to update the local copy of the JavaScript file:
|
84
|
+
|
85
|
+
rake google_analytics:updates
|
86
|
+
|
87
|
+
To keep the file updated you can add the following to your Capistrano
|
88
|
+
configuration:
|
89
|
+
|
90
|
+
after "deploy:symlink", "deploy:google_analytics"
|
91
|
+
|
92
|
+
namespace :deploy do
|
93
|
+
desc "Update local Google Analytics files"
|
94
|
+
task :google_analytics, :role => :web do
|
95
|
+
run "cd #{current_path} && rake google_analytics:update RAILS_ENV=#{ENV['RAILS_ENV']}"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
The above Capistrano recipe will almost certainly need some adjustments based
|
100
|
+
on how you run your deployments, but you should get the idea.
|
101
|
+
|
102
|
+
== Overriding application-default values
|
103
|
+
|
104
|
+
If you're using one Rails application to serve pages across multiple domains,
|
105
|
+
you may wish to override the domain and tracker ID values on a
|
106
|
+
controller-by-controller or view-by-view basis. You can do this by setting the
|
107
|
+
override_domain_name and override_tracker_id properties. These properties are
|
108
|
+
automatically reset after each use, so the values you set for domain_name and
|
109
|
+
tracker_id (usually in an initializer) will apply to all other requests.
|
110
|
+
|
111
|
+
before_filter :local_analytics
|
112
|
+
|
113
|
+
def local_analytics
|
114
|
+
Rubaidh::GoogleAnalytics.override_domain_name = 'foo.com'
|
115
|
+
Rubaidh::GoogleAnalytics.override_tracker_id = 'UA-123456-7'
|
116
|
+
end
|
117
|
+
|
118
|
+
See the documentation for the GoogleAnalytics class for other configuration
|
119
|
+
options.
|
120
|
+
|
121
|
+
Note: You will need to have the mocha gem installed to run the tests for this
|
122
|
+
plugin.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the google_analytics plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the google_analytics plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'GoogleAnalytics'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README.rdoc')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
@@ -0,0 +1,233 @@
|
|
1
|
+
module Rubaidh # :nodoc:
|
2
|
+
# This module gets mixed in to ActionController::Base
|
3
|
+
module GoogleAnalyticsMixin
|
4
|
+
# The javascript code to enable Google Analytics on the current page.
|
5
|
+
# Normally you won't need to call this directly; the +add_google_analytics_code+
|
6
|
+
# after filter will insert it for you.
|
7
|
+
def google_analytics_code
|
8
|
+
GoogleAnalytics.google_analytics_code(request.ssl?) if GoogleAnalytics.enabled?(request.format)
|
9
|
+
end
|
10
|
+
|
11
|
+
# An after_filter to automatically add the analytics code.
|
12
|
+
# If you intend to use the link_to_tracked view helpers, you need to set Rubaidh::GoogleAnalytics.defer_load = false
|
13
|
+
# to load the code at the top of the page
|
14
|
+
# (see http://www.google.com/support/googleanalytics/bin/answer.py?answer=55527&topic=11006)
|
15
|
+
def add_google_analytics_code
|
16
|
+
if GoogleAnalytics.defer_load
|
17
|
+
response.body.sub! '</body>', "#{google_analytics_code}</body>" if response.body.respond_to?(:sub!)
|
18
|
+
else
|
19
|
+
response.body.sub! '<body>', "<body>#{google_analytics_code}" if response.body.respond_to?(:sub!)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class GoogleAnalyticsConfigurationError < StandardError; end
|
25
|
+
|
26
|
+
# The core functionality to connect a Rails application
|
27
|
+
# to a Google Analytics installation.
|
28
|
+
class GoogleAnalytics
|
29
|
+
|
30
|
+
@@tracker_id = nil
|
31
|
+
##
|
32
|
+
# :singleton-method:
|
33
|
+
# Specify the Google Analytics ID for this web site. This can be found
|
34
|
+
# as the value of +_getTracker+ if you are using the new (ga.js) tracking
|
35
|
+
# code, or the value of +_uacct+ if you are using the old (urchin.js)
|
36
|
+
# tracking code.
|
37
|
+
cattr_accessor :tracker_id
|
38
|
+
|
39
|
+
@@domain_name = nil
|
40
|
+
##
|
41
|
+
# :singleton-method:
|
42
|
+
# Specify a different domain name from the default. You'll want to use
|
43
|
+
# this if you have several subdomains that you want to combine into
|
44
|
+
# one report. See the Google Analytics documentation for more
|
45
|
+
# information.
|
46
|
+
cattr_accessor :domain_name
|
47
|
+
|
48
|
+
@@legacy_mode = false
|
49
|
+
##
|
50
|
+
# :singleton-method:
|
51
|
+
# Specify whether the legacy Google Analytics code should be used. By
|
52
|
+
# default, the new Google Analytics code is used.
|
53
|
+
cattr_accessor :legacy_mode
|
54
|
+
|
55
|
+
@@analytics_url = 'http://www.google-analytics.com/urchin.js'
|
56
|
+
##
|
57
|
+
# :singleton-method:
|
58
|
+
# The URL that analytics information is sent to. This defaults to the
|
59
|
+
# standard Google Analytics URL, and you're unlikely to need to change it.
|
60
|
+
# This has no effect unless you're in legacy mode.
|
61
|
+
cattr_accessor :analytics_url
|
62
|
+
|
63
|
+
@@analytics_ssl_url = 'https://ssl.google-analytics.com/urchin.js'
|
64
|
+
##
|
65
|
+
# :singleton-method:
|
66
|
+
# The URL that analytics information is sent to when using SSL. This defaults to the
|
67
|
+
# standard Google Analytics URL, and you're unlikely to need to change it.
|
68
|
+
# This has no effect unless you're in legacy mode.
|
69
|
+
cattr_accessor :analytics_ssl_url
|
70
|
+
|
71
|
+
@@environments = ['production']
|
72
|
+
##
|
73
|
+
# :singleton-method:
|
74
|
+
# The environments in which to enable the Google Analytics code. Defaults
|
75
|
+
# to 'production' only. Supply an array of environment names to change this.
|
76
|
+
cattr_accessor :environments
|
77
|
+
|
78
|
+
@@formats = [:html, :all]
|
79
|
+
##
|
80
|
+
# :singleton-method:
|
81
|
+
# The request formats where tracking code should be added. Defaults to +[:html, :all]+. The entry for
|
82
|
+
# +:all+ is necessary to make Google recognize that tracking is installed on a
|
83
|
+
# site; it is not the same as responding to all requests. Supply an array
|
84
|
+
# of formats to change this.
|
85
|
+
cattr_accessor :formats
|
86
|
+
|
87
|
+
@@defer_load = true
|
88
|
+
##
|
89
|
+
# :singleton-method:
|
90
|
+
# Set this to true (the default) if you want to load the Analytics javascript at
|
91
|
+
# the bottom of page. Set this to false if you want to load the Analytics
|
92
|
+
# javascript at the top of the page. The page will render faster if you set this to
|
93
|
+
# true, but that will break the linking functions in Rubaidh::GoogleAnalyticsViewHelper.
|
94
|
+
cattr_accessor :defer_load
|
95
|
+
|
96
|
+
@@local_javascript = false
|
97
|
+
##
|
98
|
+
# :singleton-method:
|
99
|
+
# Set this to true to use a local copy of the ga.js (or urchin.js) file.
|
100
|
+
# This gives you the added benefit of serving the JS directly from your
|
101
|
+
# server, which in case of a big geographical difference between your server
|
102
|
+
# and Google's can speed things up for your visitors. Use the
|
103
|
+
# 'google_analytics:update' rake task to update the local JS copies.
|
104
|
+
cattr_accessor :local_javascript
|
105
|
+
|
106
|
+
##
|
107
|
+
# :singleton-method:
|
108
|
+
# Set this to override the initialized domain name for a single render. Useful
|
109
|
+
# when you're serving to multiple hosts from a single codebase. Typically you'd
|
110
|
+
# set up a before filter in the appropriate controller:
|
111
|
+
# before_filter :override_domain_name
|
112
|
+
# def override_domain_name
|
113
|
+
# Rubaidh::GoogleAnalytics.override_domain_name = 'foo.com'
|
114
|
+
# end
|
115
|
+
cattr_accessor :override_domain_name
|
116
|
+
|
117
|
+
##
|
118
|
+
# :singleton-method:
|
119
|
+
# Set this to override the initialized tracker ID for a single render. Useful
|
120
|
+
# when you're serving to multiple hosts from a single codebase. Typically you'd
|
121
|
+
# set up a before filter in the appropriate controller:
|
122
|
+
# before_filter :override_tracker_id
|
123
|
+
# def override_tracker_id
|
124
|
+
# Rubaidh::GoogleAnalytics.override_tracker_id = 'UA-123456-7'
|
125
|
+
# end
|
126
|
+
cattr_accessor :override_tracker_id
|
127
|
+
|
128
|
+
##
|
129
|
+
# :singleton-method:
|
130
|
+
# Set this to override the automatically generated path to the page in the
|
131
|
+
# Google Analytics reports for a single render. Typically you'd set this up on an
|
132
|
+
# action-by-action basis:
|
133
|
+
# def show
|
134
|
+
# Rubaidh::GoogleAnalytics.override_trackpageview = "path_to_report"
|
135
|
+
# ...
|
136
|
+
cattr_accessor :override_trackpageview
|
137
|
+
|
138
|
+
# Return true if the Google Analytics system is enabled and configured
|
139
|
+
# correctly for the specified format
|
140
|
+
def self.enabled?(format)
|
141
|
+
raise Rubaidh::GoogleAnalyticsConfigurationError if tracker_id.blank? || analytics_url.blank?
|
142
|
+
environments.include?(RAILS_ENV) && formats.include?(format.to_sym)
|
143
|
+
end
|
144
|
+
|
145
|
+
# Construct the javascript code to be inserted on the calling page. The +ssl+
|
146
|
+
# parameter can be used to force the SSL version of the code in legacy mode only.
|
147
|
+
def self.google_analytics_code(ssl = false)
|
148
|
+
return legacy_google_analytics_code(ssl) if legacy_mode
|
149
|
+
|
150
|
+
extra_code = domain_name.blank? ? nil : "pageTracker._setDomainName(\"#{domain_name}\");"
|
151
|
+
if !override_domain_name.blank?
|
152
|
+
extra_code = "pageTracker._setDomainName(\"#{override_domain_name}\");"
|
153
|
+
self.override_domain_name = nil
|
154
|
+
end
|
155
|
+
|
156
|
+
code = if local_javascript
|
157
|
+
<<-HTML
|
158
|
+
<script src="#{LocalAssetTagHelper.new.javascript_path( 'ga.js' )}" type="text/javascript">
|
159
|
+
</script>
|
160
|
+
HTML
|
161
|
+
else
|
162
|
+
<<-HTML
|
163
|
+
<script type="text/javascript">
|
164
|
+
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
|
165
|
+
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
|
166
|
+
</script>
|
167
|
+
HTML
|
168
|
+
end
|
169
|
+
|
170
|
+
code << <<-HTML
|
171
|
+
<script type="text/javascript">
|
172
|
+
<!--//--><![CDATA[//><!--
|
173
|
+
var pageTracker = _gat._getTracker('#{request_tracker_id}');
|
174
|
+
#{extra_code}
|
175
|
+
pageTracker._initData();
|
176
|
+
pageTracker._trackPageview(#{request_tracked_path});
|
177
|
+
//--><!]]>
|
178
|
+
</script>
|
179
|
+
HTML
|
180
|
+
end
|
181
|
+
|
182
|
+
# Construct the legacy version of the Google Analytics code. The +ssl+
|
183
|
+
# parameter specifies whether or not to return the SSL version of the code.
|
184
|
+
def self.legacy_google_analytics_code(ssl = false)
|
185
|
+
extra_code = domain_name.blank? ? nil : "_udn = \"#{domain_name}\";"
|
186
|
+
if !override_domain_name.blank?
|
187
|
+
extra_code = "_udn = \"#{override_domain_name}\";"
|
188
|
+
self.override_domain_name = nil
|
189
|
+
end
|
190
|
+
|
191
|
+
url = legacy_analytics_js_url(ssl)
|
192
|
+
|
193
|
+
code = <<-HTML
|
194
|
+
<script src="#{url}" type="text/javascript">
|
195
|
+
</script>
|
196
|
+
<script type="text/javascript">
|
197
|
+
_uacct = "#{request_tracker_id}";
|
198
|
+
#{extra_code}
|
199
|
+
urchinTracker(#{request_tracked_path});
|
200
|
+
</script>
|
201
|
+
HTML
|
202
|
+
end
|
203
|
+
|
204
|
+
# Generate the correct URL for the legacy Analytics JS file
|
205
|
+
def self.legacy_analytics_js_url(ssl = false)
|
206
|
+
if local_javascript
|
207
|
+
LocalAssetTagHelper.new.javascript_path( 'urchin.js' )
|
208
|
+
else
|
209
|
+
ssl ? analytics_ssl_url : analytics_url
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
# Determine the tracker ID for this request
|
214
|
+
def self.request_tracker_id
|
215
|
+
use_tracker_id = override_tracker_id.blank? ? tracker_id : override_tracker_id
|
216
|
+
self.override_tracker_id = nil
|
217
|
+
use_tracker_id
|
218
|
+
end
|
219
|
+
|
220
|
+
# Determine the path to report for this request
|
221
|
+
def self.request_tracked_path
|
222
|
+
use_tracked_path = override_trackpageview.blank? ? '' : "'#{override_trackpageview}'"
|
223
|
+
self.override_trackpageview = nil
|
224
|
+
use_tracked_path
|
225
|
+
end
|
226
|
+
|
227
|
+
end
|
228
|
+
|
229
|
+
class LocalAssetTagHelper # :nodoc:
|
230
|
+
# For helping with local javascripts
|
231
|
+
include ActionView::Helpers::AssetTagHelper
|
232
|
+
end
|
233
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module Rubaidh
|
2
|
+
# Collection of methods similar to the ones in ActionView::Helpers::UrlHelper,
|
3
|
+
# with the addition of outbound link tracking. See the Google Analytics help
|
4
|
+
# at http://www.google.com/support/googleanalytics/bin/answer.py?answer=55527
|
5
|
+
# for more information on outbound link tracking.
|
6
|
+
module GoogleAnalyticsViewHelper
|
7
|
+
# Creates a link tag of the given +name+ using a URL created by the set of +options+,
|
8
|
+
# with outbound link tracking under +track_path+ in Google Analytics. The +html_options+
|
9
|
+
# will accept a hash of attributes for the link tag.
|
10
|
+
def link_to_tracked(name, track_path = "/", options = {}, html_options = {})
|
11
|
+
raise AnalyticsError.new("You must set Rubaidh::GoogleAnalytics.defer_load = false to use outbound link tracking") if GoogleAnalytics.defer_load == true
|
12
|
+
html_options.merge!({:onclick => tracking_call(track_path)})
|
13
|
+
link_to name, options, html_options
|
14
|
+
end
|
15
|
+
|
16
|
+
# Creates a link tag of the given +name+ using a URL created by the set of +options+
|
17
|
+
# if +condition+ is true, with outbound link tracking under +track_path+ in Google Analytics.
|
18
|
+
# The +html_options+ will accept a hash of attributes for the link tag.
|
19
|
+
def link_to_tracked_if(condition, name, track_path = "/", options = {}, html_options = {}, &block)
|
20
|
+
raise AnalyticsError.new("You must set Rubaidh::GoogleAnalytics.defer_load = false to use outbound link tracking") if GoogleAnalytics.defer_load == true
|
21
|
+
html_options.merge!({:onclick => tracking_call(track_path)})
|
22
|
+
link_to_unless !condition, name, options, html_options, &block
|
23
|
+
end
|
24
|
+
|
25
|
+
# Creates a link tag of the given +name+ using a URL created by the set of +options+
|
26
|
+
# unless +condition+ is true, with outbound link tracking under +track_path+ in Google Analytics.
|
27
|
+
# The +html_options+ will accept a hash of attributes for the link tag.
|
28
|
+
def link_to_tracked_unless(condition, name, track_path = "/", options = {}, html_options = {}, &block)
|
29
|
+
raise AnalyticsError.new("You must set Rubaidh::GoogleAnalytics.defer_load = false to use outbound link tracking") if GoogleAnalytics.defer_load == true
|
30
|
+
html_options.merge!({:onclick => tracking_call(track_path)})
|
31
|
+
link_to_unless condition, name, options, html_options, &block
|
32
|
+
end
|
33
|
+
|
34
|
+
# Creates a link tag of the given +name+ using a URL created by the set of +options+
|
35
|
+
# unless the current request URI is the same as the link's, with outbound link tracking
|
36
|
+
# under +track_path+ in Google Analytics. If the request URI is the same as the link
|
37
|
+
# URI, only the name is returned, or the block is yielded, if one exists.
|
38
|
+
# The +html_options+ will accept a hash of attributes for the link tag.
|
39
|
+
def link_to_tracked_unless_current(name, track_path = "/", options = {}, html_options = {}, &block)
|
40
|
+
raise AnalyticsError.new("You must set Rubaidh::GoogleAnalytics.defer_load = false to use outbound link tracking") if GoogleAnalytics.defer_load == true
|
41
|
+
html_options.merge!({:onclick =>tracking_call(track_path)})
|
42
|
+
link_to_unless current_page?(options), name, options, html_options, &block
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def tracking_call(track_path)
|
48
|
+
if GoogleAnalytics.legacy_mode
|
49
|
+
"javascript:urchinTracker('#{track_path}');"
|
50
|
+
else
|
51
|
+
"javascript:pageTracker._trackPageview('#{track_path}');"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
# Error raised by tracking methods if Rubaidh::GoogleAnalytics.defer_load is not configured
|
58
|
+
# properly to enable tracking.
|
59
|
+
class AnalyticsError < StandardError
|
60
|
+
attr_reader :message
|
61
|
+
|
62
|
+
def initialize(message)
|
63
|
+
@message = message
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
data/rails/init.rb
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
require 'rubaidh/google_analytics'
|
2
|
+
require 'rubaidh/view_helpers'
|
3
|
+
ActionController::Base.send :include, Rubaidh::GoogleAnalyticsMixin
|
4
|
+
ActionController::Base.send :after_filter, :add_google_analytics_code
|
5
|
+
ActionView::Base.send :include, Rubaidh::GoogleAnalyticsViewHelper
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'rubygems'
|
4
|
+
require 'mocha'
|
5
|
+
RAILS_ENV = 'test'
|
6
|
+
|
7
|
+
class GoogleAnalyticsTest < Test::Unit::TestCase
|
8
|
+
def setup
|
9
|
+
@ga = Rubaidh::GoogleAnalytics.new
|
10
|
+
@ga.tracker_id = "the tracker id"
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_createable
|
14
|
+
assert_not_nil(@ga)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_domain_name_defaults_to_nil
|
18
|
+
assert_nil(@ga.domain_name)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_legacy_mode_defaults_to_false
|
22
|
+
assert_equal(false, @ga.legacy_mode)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_default_analytics_url
|
26
|
+
assert_equal("http://www.google-analytics.com/urchin.js", @ga.analytics_url)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_default_analytics_ssl_url
|
30
|
+
assert_equal('https://ssl.google-analytics.com/urchin.js', @ga.analytics_ssl_url)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_default_environments
|
34
|
+
assert_equal(false, @ga.environments.include?('test'))
|
35
|
+
assert_equal(false, @ga.environments.include?('development'))
|
36
|
+
assert_equal(true, @ga.environments.include?('production'))
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_default_formats
|
40
|
+
assert_equal(false, @ga.formats.include?(:xml))
|
41
|
+
assert_equal(true, @ga.formats.include?(:html))
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_defer_load_defaults_to_true
|
45
|
+
assert_equal(true, @ga.defer_load)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_local_javascript_defaults_to_false
|
49
|
+
assert_equal(false, @ga.local_javascript)
|
50
|
+
end
|
51
|
+
|
52
|
+
# test self.enabled
|
53
|
+
def test_enabled_requires_tracker_id
|
54
|
+
Rubaidh::GoogleAnalytics.stubs(:tracker_id).returns(nil)
|
55
|
+
assert_raise(Rubaidh::GoogleAnalyticsConfigurationError) { Rubaidh::GoogleAnalytics.enabled?(:html) }
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_enabled_requires_analytics_url
|
59
|
+
Rubaidh::GoogleAnalytics.stubs(:analytics_url).returns(nil)
|
60
|
+
assert_raise(Rubaidh::GoogleAnalyticsConfigurationError) { Rubaidh::GoogleAnalytics.enabled?(:html) }
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_enabled_returns_false_if_current_environment_not_enabled
|
64
|
+
Rubaidh::GoogleAnalytics.stubs(:environments).returns(['production'])
|
65
|
+
assert_equal(false, Rubaidh::GoogleAnalytics.enabled?(:html))
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_enabled_with_default_format
|
69
|
+
Rubaidh::GoogleAnalytics.stubs(:environments).returns(['test'])
|
70
|
+
assert_equal(true, Rubaidh::GoogleAnalytics.enabled?(:html))
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_enabled_with_not_included_format
|
74
|
+
Rubaidh::GoogleAnalytics.stubs(:environments).returns(['test'])
|
75
|
+
assert_equal(false, Rubaidh::GoogleAnalytics.enabled?(:xml))
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_enabled_with_added_format
|
79
|
+
Rubaidh::GoogleAnalytics.stubs(:environments).returns(['test'])
|
80
|
+
Rubaidh::GoogleAnalytics.stubs(:formats).returns([:xml])
|
81
|
+
assert_equal(true, Rubaidh::GoogleAnalytics.enabled?(:xml))
|
82
|
+
end
|
83
|
+
|
84
|
+
# test request_tracker_id
|
85
|
+
def test_request_tracker_id_without_override
|
86
|
+
Rubaidh::GoogleAnalytics.stubs(:tracker_id).returns("1234")
|
87
|
+
assert_equal("1234", Rubaidh::GoogleAnalytics.request_tracker_id)
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_request_tracker_id_with_override
|
91
|
+
Rubaidh::GoogleAnalytics.stubs(:tracker_id).returns("1234")
|
92
|
+
Rubaidh::GoogleAnalytics.override_tracker_id = "4567"
|
93
|
+
assert_equal("4567", Rubaidh::GoogleAnalytics.request_tracker_id)
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_request_tracker_id_resets_override
|
97
|
+
Rubaidh::GoogleAnalytics.override_tracker_id = "4567"
|
98
|
+
Rubaidh::GoogleAnalytics.stubs(:tracker_id).returns("1234")
|
99
|
+
foo = Rubaidh::GoogleAnalytics.request_tracker_id
|
100
|
+
assert_nil(Rubaidh::GoogleAnalytics.override_tracker_id)
|
101
|
+
end
|
102
|
+
|
103
|
+
# test request_tracked_path
|
104
|
+
def test_request_tracked_path_without_override
|
105
|
+
assert_equal('', Rubaidh::GoogleAnalytics.request_tracked_path)
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_request_tracked_path_with_override
|
109
|
+
Rubaidh::GoogleAnalytics.override_trackpageview = "/my/path"
|
110
|
+
assert_equal("'/my/path'", Rubaidh::GoogleAnalytics.request_tracked_path)
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_request_tracked_path_resets_override
|
114
|
+
Rubaidh::GoogleAnalytics.override_trackpageview = "/my/path"
|
115
|
+
foo = Rubaidh::GoogleAnalytics.request_tracked_path
|
116
|
+
assert_nil(Rubaidh::GoogleAnalytics.override_trackpageview)
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
ENV['RAILS_ENV'] = 'test'
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'test/unit'
|
5
|
+
require 'action_controller'
|
6
|
+
require 'active_record'
|
7
|
+
|
8
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/rubaidh/google_analytics.rb')
|
9
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/rubaidh/view_helpers.rb')
|
10
|
+
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
include Rubaidh::GoogleAnalyticsViewHelper
|
3
|
+
include ActionView::Helpers::UrlHelper
|
4
|
+
include ActionView::Helpers::TagHelper
|
5
|
+
|
6
|
+
class ViewHelpersTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
Rubaidh::GoogleAnalytics.defer_load = false
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_link_to_tracked_should_return_a_tracked_link
|
13
|
+
assert_equal "<a href=\"http://www.example.com\" onclick=\"javascript:pageTracker._trackPageview('/sites/linked');\">Link</a>", link_to_tracked('Link', '/sites/linked', "http://www.example.com" )
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_link_to_tracked_with_legacy_should_return_a_tracked_link
|
17
|
+
Rubaidh::GoogleAnalytics.legacy_mode = true
|
18
|
+
assert_equal "<a href=\"http://www.example.com\" onclick=\"javascript:urchinTracker('/sites/linked');\">Link</a>", link_to_tracked('Link', '/sites/linked', "http://www.example.com" )
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_link_to_tracked_should_error_if_defer_load
|
22
|
+
Rubaidh::GoogleAnalytics.defer_load = true
|
23
|
+
assert_raise(Rubaidh::AnalyticsError) { link_to_tracked('Link', '/sites/linked', "http://www.example.com" ) }
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_link_to_tracked_if_with_true_should_return_a_tracked_link
|
27
|
+
assert_equal "<a href=\"http://www.example.com\" onclick=\"javascript:pageTracker._trackPageview('/sites/linked');\">Link</a>", link_to_tracked_if(true, 'Link', '/sites/linked', "http://www.example.com" )
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_link_to_tracked_if_with_false_should_return_unlinked_text
|
31
|
+
assert_equal "Link", link_to_tracked_if(false, 'Link', '/sites/linked', "http://www.example.com" )
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_link_to_tracked_if_should_error_if_defer_load
|
35
|
+
Rubaidh::GoogleAnalytics.defer_load = true
|
36
|
+
assert_raise(Rubaidh::AnalyticsError) { link_to_tracked_if(false, 'Link', '/sites/linked', "http://www.example.com" ) }
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_link_to_tracked_unless_with_false_should_return_a_tracked_link
|
40
|
+
assert_equal "<a href=\"http://www.example.com\" onclick=\"javascript:pageTracker._trackPageview('/sites/linked');\">Link</a>", link_to_tracked_unless(false, 'Link', '/sites/linked', "http://www.example.com" )
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_link_to_tracked_unless_with_true_should_return_unlinked_text
|
44
|
+
assert_equal "Link", link_to_tracked_unless(true, 'Link', '/sites/linked', "http://www.example.com" )
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_link_to_tracked_unless_should_error_if_defer_load
|
48
|
+
Rubaidh::GoogleAnalytics.defer_load = true
|
49
|
+
assert_raise(Rubaidh::AnalyticsError) { link_to_tracked_unless(false, 'Link', '/sites/linked', "http://www.example.com" ) }
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_link_to_tracked_unless_current
|
53
|
+
#postponed
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubaidh-google_analytics
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.20081111
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Graeme Mathieson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-11-11 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: actionpack
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
version:
|
24
|
+
description: "By default this gem will output google analytics code forevery page automatically, if it's configured correctly.This is done by adding: Rubaidh::GoogleAnalytics.tracker_id = 'UA-12345-67'"
|
25
|
+
email: mathie@rubaidh.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- README.rdoc
|
34
|
+
- Rakefile
|
35
|
+
- rails/init.rb
|
36
|
+
- test/google_analytics_test.rb
|
37
|
+
- test/test_helper.rb
|
38
|
+
- test/view_helpers_test.rb
|
39
|
+
- lib/rubaidh/google_analytics.rb
|
40
|
+
- lib/rubaidh/view_helpers.rb
|
41
|
+
- task/google_analytics.rake
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/rubaidh/google_analytics/tree/master
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.2.0
|
65
|
+
signing_key:
|
66
|
+
specification_version: 2
|
67
|
+
summary: "[Rails] This is a quick 'n' dirty module to easily enableGoogle Analytics support in your application."
|
68
|
+
test_files: []
|
69
|
+
|