markcatley-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/README +27 -0
- data/Rakefile +22 -0
- data/lib/rubaidh/google_analytics.rb +70 -0
- data/rails/init.rb +3 -0
- data/test/google_analytics_test.rb +8 -0
- metadata +65 -0
data/README
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
GoogleAnalytics
|
2
|
+
===============
|
3
|
+
|
4
|
+
This is a quick 'n' dirty module to easily enable Google Analytics support
|
5
|
+
in your application. By default it'll output the analytics code for every
|
6
|
+
single page automatically, if it's configured correctly. This is done by
|
7
|
+
adding:
|
8
|
+
|
9
|
+
Rubaidh::GoogleAnalytics.tracker_id = 'UA-12345-67'
|
10
|
+
|
11
|
+
to your `config/environment.rb`, inserting your own tracker id. This can be
|
12
|
+
discovered by looking at the value assigned to +_uacct+ in the Javascript
|
13
|
+
code.
|
14
|
+
|
15
|
+
If you want to disable the code insertion for particular pages, add the following to controllers that don't want it:
|
16
|
+
|
17
|
+
skip_after_filter :add_google_analytics_code
|
18
|
+
|
19
|
+
If you are running rails 2.1 or above add install this by adding:
|
20
|
+
|
21
|
+
config.gem 'markcatley-google_analytics', :lib => 'rubaidh/google_analytics', :source => 'http://gems.github.com'
|
22
|
+
|
23
|
+
and run:
|
24
|
+
|
25
|
+
rake gems:install
|
26
|
+
|
27
|
+
Simple. :-)
|
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')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Rubaidh # :nodoc:
|
2
|
+
module GoogleAnalyticsMixin
|
3
|
+
def google_analytics_code(request = nil)
|
4
|
+
return unless GoogleAnalytics.enabled?
|
5
|
+
GoogleAnalytics.google_analytics_code(request)
|
6
|
+
end
|
7
|
+
|
8
|
+
# An after_filter to automatically add the analytics code.
|
9
|
+
def add_google_analytics_code
|
10
|
+
code = google_analytics_code(request)
|
11
|
+
return if code.blank?
|
12
|
+
response.body.gsub! '</body>', code + '</body>' if response.body.respond_to?(:gsub!)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class GoogleAnalytics
|
17
|
+
# Specify the Google Analytics ID for this web site. This can be found
|
18
|
+
# as the value of +_uacct+ in the Javascript excerpt
|
19
|
+
@@tracker_id = nil
|
20
|
+
cattr_accessor :tracker_id
|
21
|
+
|
22
|
+
# Specify a different domain name from the default. You'll want to use
|
23
|
+
# this if you have several subdomains that you want to combine into
|
24
|
+
# one report. See the Google Analytics documentation for more
|
25
|
+
# information.
|
26
|
+
@@domain_name = nil
|
27
|
+
cattr_accessor :domain_name
|
28
|
+
|
29
|
+
# I can't see why you'd want to do this, but you can always change the
|
30
|
+
# analytics URL.
|
31
|
+
@@analytics_url = 'http://www.google-analytics.com/urchin.js'
|
32
|
+
cattr_accessor :analytics_url
|
33
|
+
|
34
|
+
# I can't see why you'd want to do this, but you can always change the
|
35
|
+
# analytics URL (ssl version).
|
36
|
+
@@analytics_ssl_url = 'https://ssl.google-analytics.com/urchin.js'
|
37
|
+
cattr_accessor :analytics_ssl_url
|
38
|
+
|
39
|
+
# The environments in which to enable the Google Analytics code. Defaults
|
40
|
+
# to 'production' only.
|
41
|
+
@@environments = ['production']
|
42
|
+
cattr_accessor :environments
|
43
|
+
|
44
|
+
# Return true if the Google Analytics system is enabled and configured
|
45
|
+
# correctly.
|
46
|
+
def self.enabled?
|
47
|
+
(environments.include?(RAILS_ENV) and
|
48
|
+
not tracker_id.blank? and
|
49
|
+
not analytics_url.blank?)
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.google_analytics_code(request = nil)
|
53
|
+
extra_code = domain_name.blank? ? nil : "_udn = \"#{domain_name}\";"
|
54
|
+
url = (not request.blank? and request.ssl?) ? analytics_ssl_url : analytics_url
|
55
|
+
|
56
|
+
# OK, I'm not very bright -- I tried to turn this into a partial and
|
57
|
+
# failed miserably! So it'll have to live here for now.
|
58
|
+
code = <<-HTML
|
59
|
+
<script src="#{url}" type="text/javascript">
|
60
|
+
</script>
|
61
|
+
<script type="text/javascript">
|
62
|
+
_uacct = "#{tracker_id}";
|
63
|
+
#{extra_code}
|
64
|
+
urchinTracker();
|
65
|
+
</script>
|
66
|
+
HTML
|
67
|
+
code
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/rails/init.rb
ADDED
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: markcatley-google_analytics
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Graeme Mathieson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-07-08 00:00:00 -07: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
|
34
|
+
- Rakefile
|
35
|
+
- rails/init.rb
|
36
|
+
- test/google_analytics_test.rb
|
37
|
+
- lib/rubaidh/google_analytics.rb
|
38
|
+
has_rdoc: false
|
39
|
+
homepage: http://github.com/rubaidh/google_analytics/tree/master
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
|
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.2.0
|
61
|
+
signing_key:
|
62
|
+
specification_version: 2
|
63
|
+
summary: "[Rails] This is a quick 'n' dirty module to easily enableGoogle Analytics support in your application."
|
64
|
+
test_files: []
|
65
|
+
|