google-analytics-rails 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,3 +3,4 @@
3
3
  Gemfile.lock
4
4
  pkg/*
5
5
  .yardoc
6
+ doc
data/CHANGELOG.markdown CHANGED
@@ -1,3 +1,9 @@
1
+ 0.0.4
2
+ =====
3
+
4
+ * Support for custom analytics script src (DoubleClick is supported).
5
+ * Add a `:domain` option to `analytics_init`.
6
+
1
7
  0.0.3
2
8
  =====
3
9
 
data/README.markdown CHANGED
@@ -1,4 +1,4 @@
1
- Fast Google Analytics setup for Rails. This gem is mostly intended for small to medium websites with a simple analytics strategy.
1
+ Fast Google Analytics setup for Rails 3. This gem is mostly intended for small to medium websites with a simple analytics strategy.
2
2
 
3
3
  Installation
4
4
  ============
@@ -31,6 +31,18 @@ Production only
31
31
 
32
32
  <%= analytics_init if Rails.env.production? %>
33
33
 
34
+ With DoubleClick instead of vanilla Google Analytics script
35
+ -----------------------------------------------------------
36
+
37
+ `config/environments/production.rb`:
38
+
39
+ # replace this with your tracker code
40
+ GA.tracker = "UA-xxxxxx-x"
41
+ GA.source_script = "('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'"
42
+
43
+ `app/views/layout/application.html.erb`, in the `<head>` tag :
44
+
45
+ <%= analytics_init if Rails.env.production? %>
34
46
 
35
47
  Different accounts for development and production
36
48
  -------------------------------------------------
@@ -52,7 +64,7 @@ Different accounts for development and production
52
64
  License
53
65
  =======
54
66
 
55
- [google-analytics-rails](https://github.com/bgarret.google-analytics-rails) is released under the MIT license:
67
+ [google-analytics-rails](https://github.com/bgarret/google-analytics-rails) is released under the MIT license:
56
68
 
57
69
  * http://www.opensource.org/licenses/MIT
58
70
 
@@ -9,8 +9,8 @@ Gem::Specification.new do |s|
9
9
  s.authors = ["Benoit Garret"]
10
10
  s.email = ["benoit.garret@gadz.org"]
11
11
  s.homepage = "https://github.com/bgarret/google-analytics-rails"
12
- s.summary = %q{Rails helpers to manage google analytics tracking}
13
- s.description = %q{Rails helpers to manage google analytics tracking}
12
+ s.summary = %q{Rails 3 helpers to manage google analytics tracking}
13
+ s.description = %q{Rails 3 helpers to manage google analytics tracking}
14
14
 
15
15
  s.rubyforge_project = "google-analytics-rails"
16
16
 
@@ -1,9 +1,13 @@
1
+ # coding: utf-8
2
+
1
3
  require 'google-analytics/async_tracking_queue'
2
4
  require 'google-analytics/events'
3
5
 
4
6
  module GoogleAnalytics
5
7
  # @private
6
8
  PLACEHOLDER_TRACKER = "UA-xxxxxx-x"
9
+ # @private
10
+ DEFAULT_SCRIPT_SOURCE = "('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'"
7
11
 
8
12
  # Get the current tracker id (*UA-xxxxxx-x*).
9
13
  # @return [String]
@@ -21,6 +25,24 @@ module GoogleAnalytics
21
25
  def self.valid_tracker?
22
26
  tracker.nil? || tracker == "" || tracker == PLACEHOLDER_TRACKER ? false : true
23
27
  end
28
+
29
+ # Get the current ga src.
30
+ # This allows for example to use the compatible doubleclick code :
31
+ # ```
32
+ # ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'
33
+ # ```
34
+ # @see http://support.google.com/analytics/bin/answer.py?hl=en&answer=2444872 for more info
35
+ #
36
+ # @return [String]
37
+ def self.script_source
38
+ @@src ||= DEFAULT_SCRIPT_SOURCE
39
+ end
40
+
41
+ # Set the current ga src.
42
+ # @return [String]
43
+ def self.script_source=(src)
44
+ @@src = src
45
+ end
24
46
  end
25
47
 
26
48
  # Alias for {GoogleAnalytics}
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  module GoogleAnalytics
2
4
  class AsyncTrackingQueue
3
5
  def initialize
@@ -19,7 +21,7 @@ var _gaq = _gaq || [];
19
21
  #{@events.map { |event| event.to_s }.join("\n")}
20
22
  (function() {
21
23
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
22
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
24
+ ga.src = #{GoogleAnalytics.script_source};
23
25
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
24
26
  })();
25
27
  </script>
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  module GoogleAnalytics
2
4
  autoload :Events, 'google-analytics/events/events'
3
5
  autoload :Event, 'google-analytics/events/event'
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  module GoogleAnalytics
2
4
  class Event
3
5
  attr_reader :name, :params
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  module GoogleAnalytics
2
4
  class EventCollection
3
5
  include Enumerable
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  module GoogleAnalytics
2
4
  class EventCollectionRenderer
3
5
  def initialize(event_collection, tracker_id)
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  require 'json'
2
4
 
3
5
  module GoogleAnalytics
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  module GoogleAnalytics
2
4
  module Events
3
5
  class SetAccount < Event
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  require 'google-analytics/rails/view_helpers'
2
4
 
3
5
  module GoogleAnalytics::Rails
@@ -1,3 +1,5 @@
1
+ # coding: utf-8
2
+
1
3
  require 'active_support/core_ext/string/output_safety'
2
4
  require 'active_support/core_ext/object/blank'
3
5
 
@@ -57,6 +59,8 @@ module GoogleAnalytics::Rails
57
59
  # The optional virtual page view to track through {GoogleAnalytics::Events::TrackPageview}
58
60
  # @option options [String] :tracker
59
61
  # The tracker to use instead of the default {GoogleAnalytics.tracker}
62
+ # @option options [String] :domain
63
+ # The domain name to track, see {GoogleAnalytics::Events::SetDomainName}
60
64
  # @option options [Boolean] :anonymize
61
65
  # Whether to anonymize the visitor ip or not.
62
66
  # This is required for european websites and actively enforced for german ones,
@@ -78,6 +82,7 @@ module GoogleAnalytics::Rails
78
82
 
79
83
  local = options.delete(:local) || false
80
84
  anonymize = options.delete(:anonymize) || false
85
+ domain = options.delete(:domain) || (local ? "none" : "auto")
81
86
  events = options.delete(:add_events) || []
82
87
  events = [events] unless events.is_a?(Array)
83
88
 
@@ -87,8 +92,8 @@ module GoogleAnalytics::Rails
87
92
  events.unshift GA::Events::TrackPageview.new(options[:page])
88
93
  # anonymize if needed before tracking the page view
89
94
  events.unshift GA::Events::AnonymizeIp.new if anonymize
95
+ events.unshift GA::Events::SetDomainName.new(domain)
90
96
  if local
91
- events.unshift GA::Events::SetDomainName.new('none')
92
97
  events.unshift GA::Events::SetAllowLinker.new(true)
93
98
  end
94
99
  events.unshift GA::Events::SetAccount.new(tracker)
@@ -1,4 +1,6 @@
1
+ # coding: utf-8
2
+
1
3
  module GoogleAnalytics
2
4
  # Gem version
3
- VERSION = "0.0.3"
5
+ VERSION = "0.0.4"
4
6
  end
@@ -1,6 +1,10 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class AsyncTrackingQueueTest < Test::Unit::TestCase
4
+ def teardown
5
+ GoogleAnalytics.script_source = nil
6
+ end
7
+
4
8
  VALID_SNIPPET = <<-JAVASCRIPT
5
9
  <script type="text/javascript">
6
10
  var _gaq = _gaq || [];
@@ -29,4 +33,34 @@ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga
29
33
 
30
34
  assert_equal(VALID_SNIPPET, gaq.to_s)
31
35
  end
36
+
37
+ VALID_DOUBLECLICK_SNIPPET = <<-JAVASCRIPT
38
+ <script type="text/javascript">
39
+ var _gaq = _gaq || [];
40
+ _gaq.push(['event1',1]);
41
+ _gaq.push(['event2',2]);
42
+ _gaq.push(['t2.event1',1]);
43
+ _gaq.push(['t2.event2',2]);
44
+ (function() {
45
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
46
+ ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
47
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
48
+ })();
49
+ </script>
50
+ JAVASCRIPT
51
+
52
+ def test_queue_renders_valid_javascript_snippit_with_custom_src
53
+ GoogleAnalytics.script_source = "('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'"
54
+ gaq = GAQ.new
55
+
56
+ # Add 2 events to the default tracker
57
+ gaq << GA::Event.new('event1', 1)
58
+ gaq << GA::Event.new('event2', 2)
59
+
60
+ # Add 2 events for an alternate tracker
61
+ gaq.push(GA::Event.new('event1', 1), 't2')
62
+ gaq.push(GA::Event.new('event2', 2), 't2')
63
+
64
+ assert_equal(VALID_DOUBLECLICK_SNIPPET, gaq.to_s)
65
+ end
32
66
  end
@@ -8,6 +8,7 @@ class ViewHelpersTest < Test::Unit::TestCase
8
8
  <script type="text/javascript">
9
9
  var _gaq = _gaq || [];
10
10
  _gaq.push(['_setAccount','TEST']);
11
+ _gaq.push(['_setDomainName','auto']);
11
12
  _gaq.push(['_trackPageview']);
12
13
  (function() {
13
14
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
@@ -25,6 +26,7 @@ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga
25
26
  <script type="text/javascript">
26
27
  var _gaq = _gaq || [];
27
28
  _gaq.push(['_setAccount','TEST']);
29
+ _gaq.push(['_setDomainName','auto']);
28
30
  _gaq.push(['_trackPageview','/some/virtual/url']);
29
31
  (function() {
30
32
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
@@ -42,6 +44,7 @@ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga
42
44
  <script type="text/javascript">
43
45
  var _gaq = _gaq || [];
44
46
  _gaq.push(['_setAccount','UA-CUSTOM-XX']);
47
+ _gaq.push(['_setDomainName','auto']);
45
48
  _gaq.push(['_trackPageview']);
46
49
  (function() {
47
50
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
@@ -55,6 +58,24 @@ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga
55
58
  assert_equal(VALID_INIT_WITH_CUSTOM_TRACKER, analytics_init(:tracker => 'UA-CUSTOM-XX'))
56
59
  end
57
60
 
61
+ VALID_INIT_WITH_CUSTOM_DOMAIN = <<-JAVASCRIPT
62
+ <script type="text/javascript">
63
+ var _gaq = _gaq || [];
64
+ _gaq.push(['_setAccount','TEST']);
65
+ _gaq.push(['_setDomainName','example.com']);
66
+ _gaq.push(['_trackPageview']);
67
+ (function() {
68
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
69
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
70
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
71
+ })();
72
+ </script>
73
+ JAVASCRIPT
74
+
75
+ def test_analytics_init_with_custom_domain
76
+ assert_equal(VALID_INIT_WITH_CUSTOM_DOMAIN, analytics_init(:domain => 'example.com'))
77
+ end
78
+
58
79
  VALID_LOCAL_INIT = <<-JAVASCRIPT
59
80
  <script type="text/javascript">
60
81
  var _gaq = _gaq || [];
@@ -78,6 +99,7 @@ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga
78
99
  <script type="text/javascript">
79
100
  var _gaq = _gaq || [];
80
101
  _gaq.push(['_setAccount','TEST']);
102
+ _gaq.push(['_setDomainName','auto']);
81
103
  _gaq.push(['_gat._anonymizeIp']);
82
104
  _gaq.push(['_trackPageview']);
83
105
  (function() {
@@ -96,6 +118,7 @@ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga
96
118
  <script type="text/javascript">
97
119
  var _gaq = _gaq || [];
98
120
  _gaq.push(['_setAccount','TEST']);
121
+ _gaq.push(['_setDomainName','auto']);
99
122
  _gaq.push(['_trackPageview']);
100
123
  _gaq.push(['_setAllowLinker',true]);
101
124
  (function() {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-analytics-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-29 00:00:00.000000000 Z
12
+ date: 2013-03-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -75,7 +75,7 @@ dependencies:
75
75
  - - ~>
76
76
  - !ruby/object:Gem::Version
77
77
  version: '3.0'
78
- description: Rails helpers to manage google analytics tracking
78
+ description: Rails 3 helpers to manage google analytics tracking
79
79
  email:
80
80
  - benoit.garret@gadz.org
81
81
  executables: []
@@ -119,18 +119,24 @@ required_ruby_version: !ruby/object:Gem::Requirement
119
119
  - - ! '>='
120
120
  - !ruby/object:Gem::Version
121
121
  version: '0'
122
+ segments:
123
+ - 0
124
+ hash: -2299747414610710058
122
125
  required_rubygems_version: !ruby/object:Gem::Requirement
123
126
  none: false
124
127
  requirements:
125
128
  - - ! '>='
126
129
  - !ruby/object:Gem::Version
127
130
  version: '0'
131
+ segments:
132
+ - 0
133
+ hash: -2299747414610710058
128
134
  requirements: []
129
135
  rubyforge_project: google-analytics-rails
130
136
  rubygems_version: 1.8.24
131
137
  signing_key:
132
138
  specification_version: 3
133
- summary: Rails helpers to manage google analytics tracking
139
+ summary: Rails 3 helpers to manage google analytics tracking
134
140
  test_files:
135
141
  - test/async_tracking_queue_test.rb
136
142
  - test/event_collection_renderer_test.rb