googleanalytics 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/google_analytics.rb +93 -0
- data/test/google_analytics_test.rb +40 -0
- data/test/test_helper.rb +3 -0
- metadata +57 -0
@@ -0,0 +1,93 @@
|
|
1
|
+
module Google
|
2
|
+
module Analytics
|
3
|
+
module Mixin
|
4
|
+
def google_analytics(&block)
|
5
|
+
yield code
|
6
|
+
end
|
7
|
+
|
8
|
+
def code
|
9
|
+
@google_code ||= Code.new(Config)
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_google_analytics_code
|
13
|
+
response.body.sub! /(<[bB][oO][dD][yY][^>]*>)/, "\\1#{code.to_s}" if response.body.respond_to?(:sub!)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Config
|
18
|
+
cattr_accessor :tracker_id, :domain_name, :environments
|
19
|
+
|
20
|
+
@@tracker_id = nil
|
21
|
+
@@domain_name = 'auto'
|
22
|
+
@@environments = ['production']
|
23
|
+
end
|
24
|
+
|
25
|
+
class Code
|
26
|
+
attr_accessor :options, :config, :extra_option
|
27
|
+
|
28
|
+
def initialize(config)
|
29
|
+
@config = config
|
30
|
+
@options = []
|
31
|
+
@extra_option = ''
|
32
|
+
end
|
33
|
+
|
34
|
+
def enabled?
|
35
|
+
@config.environments.include?(Rails.env)
|
36
|
+
end
|
37
|
+
|
38
|
+
def push(options={})
|
39
|
+
@options.push(options)
|
40
|
+
end
|
41
|
+
|
42
|
+
def push_code(code)
|
43
|
+
@extra_option << code
|
44
|
+
end
|
45
|
+
|
46
|
+
def options_to_s
|
47
|
+
option_string = ''
|
48
|
+
|
49
|
+
@options.each do |option|
|
50
|
+
if option.class == Hash
|
51
|
+
key = option.keys.first
|
52
|
+
values = option[key]
|
53
|
+
|
54
|
+
if values.respond_to?(:join)
|
55
|
+
value_string = values.join("', '")
|
56
|
+
option_string << "_gaq.push(['#{key}', '#{value_string}'])\n"
|
57
|
+
elsif !values.nil? && !values.empty?
|
58
|
+
option_string << "_gaq.push(['#{key}', '#{values}'])\n"
|
59
|
+
end
|
60
|
+
else
|
61
|
+
option_string << "_gaq.push(['#{option}'])\n"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
option_string
|
66
|
+
end
|
67
|
+
|
68
|
+
def to_s
|
69
|
+
return unless enabled?
|
70
|
+
|
71
|
+
<<-HTML
|
72
|
+
<script type="text/javascript">
|
73
|
+
try{
|
74
|
+
var _gaq = _gaq || [];
|
75
|
+
_gaq.push(['_setAccount', '#{@config.tracker_id}']);
|
76
|
+
_gaq.push(['_setDomainName', '#{@config.domain_name}']);
|
77
|
+
#{options_to_s}
|
78
|
+
_gaq.push(['_trackPageview']);
|
79
|
+
|
80
|
+
#{@extra_code}
|
81
|
+
|
82
|
+
(function() {
|
83
|
+
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
84
|
+
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
85
|
+
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
|
86
|
+
})();
|
87
|
+
} catch(err) {}
|
88
|
+
</script>
|
89
|
+
HTML
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'rubygems'
|
4
|
+
require 'mocha'
|
5
|
+
ENV['RAILS_ENV'] = 'test'
|
6
|
+
|
7
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/google_analytics.rb')
|
8
|
+
|
9
|
+
class TestMixin
|
10
|
+
class MockRequest
|
11
|
+
attr_accessor :format
|
12
|
+
end
|
13
|
+
class MockResponse
|
14
|
+
attr_accessor :body
|
15
|
+
end
|
16
|
+
|
17
|
+
include Google::Analytics::Mixin
|
18
|
+
|
19
|
+
attr_accessor :request, :response
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
self.request = MockRequest.new
|
23
|
+
self.response = MockResponse.new
|
24
|
+
end
|
25
|
+
|
26
|
+
# override the mixin's method
|
27
|
+
def code
|
28
|
+
"Google Code"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class GoogleAnalyticsTest < ActiveSupport::TestCase
|
33
|
+
test 'add_google_analytics_code' do
|
34
|
+
mixin = TestMixin.new
|
35
|
+
|
36
|
+
mixin.response.body = "<body><p>some text</p></body>"
|
37
|
+
mixin.add_google_analytics_code
|
38
|
+
assert_equal mixin.response.body, '<body>Google Code<p>some text</p></body>'
|
39
|
+
end
|
40
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: googleanalytics
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Wilhelmi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-07 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Google Analytics support library.
|
17
|
+
email: mnin@mnin.de
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/google_analytics.rb
|
26
|
+
- test/google_analytics_test.rb
|
27
|
+
- test/test_helper.rb
|
28
|
+
has_rdoc: true
|
29
|
+
homepage: http://www.mnin.de/
|
30
|
+
licenses: []
|
31
|
+
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: "0"
|
42
|
+
version:
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
requirements: []
|
50
|
+
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.3.5
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Google Analytics support library
|
56
|
+
test_files: []
|
57
|
+
|