google-analytics-rails 0.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/.gitignore +5 -0
- data/.yardopts +1 -0
- data/Gemfile +4 -0
- data/README.markdown +57 -0
- data/Rakefile +10 -0
- data/google-analytics-rails.gemspec +25 -0
- data/lib/google-analytics-rails.rb +34 -0
- data/lib/google-analytics/async_tracking_queue.rb +41 -0
- data/lib/google-analytics/events.rb +7 -0
- data/lib/google-analytics/events/event.rb +10 -0
- data/lib/google-analytics/events/event_collection.rb +25 -0
- data/lib/google-analytics/events/event_collection_renderer.rb +12 -0
- data/lib/google-analytics/events/event_renderer.rb +25 -0
- data/lib/google-analytics/events/events.rb +82 -0
- data/lib/google-analytics/rails/railtie.rb +10 -0
- data/lib/google-analytics/rails/view_helpers.rb +133 -0
- data/lib/google-analytics/version.rb +4 -0
- data/test/async_tracking_queue_test.rb +32 -0
- data/test/event_collection_renderer_test.rb +23 -0
- data/test/event_collection_test.rb +24 -0
- data/test/event_renderer_test.rb +13 -0
- data/test/gaq_events_test.rb +69 -0
- data/test/rails/views_helper_test.rb +101 -0
- data/test/test_helper.rb +5 -0
- metadata +110 -0
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
-m markdown --no-private --plugin rails
|
data/Gemfile
ADDED
data/README.markdown
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
Fast Google Analytics setup for Rails.
|
2
|
+
|
3
|
+
Installation
|
4
|
+
============
|
5
|
+
|
6
|
+
Add the following to your `Gemfile`:
|
7
|
+
|
8
|
+
gem 'google-analytics-rails', :git => 'git://github.com/bgarret/google-analytics-rails.git'
|
9
|
+
|
10
|
+
Then run:
|
11
|
+
|
12
|
+
bundle install
|
13
|
+
|
14
|
+
Documentation
|
15
|
+
=============
|
16
|
+
|
17
|
+
http://rubydoc.info/github/bgarret/google-analytics-rails
|
18
|
+
|
19
|
+
Example configurations
|
20
|
+
======================
|
21
|
+
|
22
|
+
Production only
|
23
|
+
---------------
|
24
|
+
|
25
|
+
`config/environments/production.rb`:
|
26
|
+
|
27
|
+
# replace this with your tracker code
|
28
|
+
GA.tracker = "UA-xxxxxx-x"
|
29
|
+
|
30
|
+
`app/views/layout/application.html.erb`, in the `<head>` tag :
|
31
|
+
|
32
|
+
<%= analytics_init if Rails.env.production? %>
|
33
|
+
|
34
|
+
|
35
|
+
Different accounts for development and production
|
36
|
+
-------------------------------------------------
|
37
|
+
|
38
|
+
`config/environments/production.rb`:
|
39
|
+
|
40
|
+
# replace this with your production tracker code
|
41
|
+
GAR.tracker = "UA-xxxxxx-x"
|
42
|
+
|
43
|
+
`config/environments/development.rb`:
|
44
|
+
|
45
|
+
# replace this with your development tracker code
|
46
|
+
GAR.tracker = "UA-xxxxxx-x"
|
47
|
+
|
48
|
+
`app/views/layout/application.html.erb`, in the `<head>` tag :
|
49
|
+
|
50
|
+
<%= analytics_init :local => Rails.env.development? %>
|
51
|
+
|
52
|
+
License
|
53
|
+
=======
|
54
|
+
|
55
|
+
[google-analytics-rails](https://github.com/bgarret.google-analytics-rails) is released under the MIT license:
|
56
|
+
|
57
|
+
* http://www.opensource.org/licenses/MIT
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "google-analytics/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "google-analytics-rails"
|
7
|
+
s.version = GoogleAnalytics::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Benoit Garret"]
|
10
|
+
s.email = ["benoit.garret@gadz.org"]
|
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}
|
14
|
+
|
15
|
+
s.rubyforge_project = "google-analytics-rails"
|
16
|
+
|
17
|
+
s.add_development_dependency "yard"
|
18
|
+
s.add_development_dependency "redcarpet"
|
19
|
+
s.add_development_dependency "activesupport", "~>3.0"
|
20
|
+
|
21
|
+
s.files = `git ls-files`.split("\n")
|
22
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
23
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
24
|
+
s.require_paths = ["lib"]
|
25
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'google-analytics/async_tracking_queue'
|
2
|
+
require 'google-analytics/events'
|
3
|
+
|
4
|
+
module GoogleAnalytics
|
5
|
+
# @private
|
6
|
+
PLACEHOLDER_TRACKER = "UA-xxxxxx-x"
|
7
|
+
|
8
|
+
# Get the current tracker id (*UA-xxxxxx-x*).
|
9
|
+
# @return [String]
|
10
|
+
def self.tracker
|
11
|
+
@@tracker ||= PLACEHOLDER_TRACKER
|
12
|
+
end
|
13
|
+
|
14
|
+
# Set the current tracker id.
|
15
|
+
# @param [String] tracker The tracker id (ie. "*UA-xxxxxx-x*").
|
16
|
+
def self.tracker=(tracker)
|
17
|
+
@@tracker = tracker
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [Boolean]
|
21
|
+
def self.valid_tracker?
|
22
|
+
tracker.nil? || tracker == "" || tracker == PLACEHOLDER_TRACKER ? false : true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Alias for {GoogleAnalytics}
|
27
|
+
GA = GoogleAnalytics
|
28
|
+
|
29
|
+
if defined?(Rails)
|
30
|
+
require 'google-analytics/rails/railtie'
|
31
|
+
|
32
|
+
# Alias for {GoogleAnalytics::Rails}
|
33
|
+
GAR = GoogleAnalytics::Rails
|
34
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module GoogleAnalytics
|
2
|
+
class AsyncTrackingQueue
|
3
|
+
def initialize
|
4
|
+
@events = []
|
5
|
+
end
|
6
|
+
|
7
|
+
def <<(event)
|
8
|
+
push(event)
|
9
|
+
end
|
10
|
+
|
11
|
+
def push(event, tracker_id = nil)
|
12
|
+
@events << renderer_for_event(event, tracker_id)
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s
|
16
|
+
<<-JAVASCRIPT
|
17
|
+
<script type="text/javascript">
|
18
|
+
var _gaq = _gaq || [];
|
19
|
+
#{@events.map { |event| event.to_s }.join("\n")}
|
20
|
+
(function() {
|
21
|
+
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';
|
23
|
+
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
24
|
+
})();
|
25
|
+
</script>
|
26
|
+
JAVASCRIPT
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def renderer_for_event(event, tracker_id)
|
32
|
+
case event
|
33
|
+
when Event then EventRenderer.new(event, tracker_id)
|
34
|
+
when EventCollection then EventCollectionRenderer.new(event, tracker_id)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Alias for {GoogleAnalytics::AsyncTrackingQueue}
|
41
|
+
GAQ = GoogleAnalytics::AsyncTrackingQueue
|
@@ -0,0 +1,7 @@
|
|
1
|
+
module GoogleAnalytics
|
2
|
+
autoload :Events, 'google-analytics/events/events'
|
3
|
+
autoload :Event, 'google-analytics/events/event'
|
4
|
+
autoload :EventRenderer, 'google-analytics/events/event_renderer'
|
5
|
+
autoload :EventCollection, 'google-analytics/events/event_collection'
|
6
|
+
autoload :EventCollectionRenderer, 'google-analytics/events/event_collection_renderer'
|
7
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module GoogleAnalytics
|
2
|
+
class EventCollection
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
class InvalidEventError < StandardError
|
6
|
+
def initialize(non_event)
|
7
|
+
super("EventCollection#<< expects instances of Event, you passed #{non_event}")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@events = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def <<(event)
|
16
|
+
raise InvalidEventError.new(event) unless event.is_a?(Event)
|
17
|
+
|
18
|
+
@events << event
|
19
|
+
end
|
20
|
+
|
21
|
+
def each
|
22
|
+
@events.each { |e| yield e }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module GoogleAnalytics
|
2
|
+
class EventCollectionRenderer
|
3
|
+
def initialize(event_collection, tracker_id)
|
4
|
+
@event_collection = event_collection
|
5
|
+
@tracker_id = tracker_id
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_s
|
9
|
+
@event_collection.map { |event| EventRenderer.new(event, @tracker_id).to_s }.join("\n")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module GoogleAnalytics
|
4
|
+
class EventRenderer
|
5
|
+
def initialize(event, tracker_id)
|
6
|
+
@event = event
|
7
|
+
@tracker_id = tracker_id
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_s
|
11
|
+
"_gaq.push(#{array_to_json([@tracker_id ? "#{@tracker_id}.#{@event.name}" : @event.name, *@event.params])});"
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def array_to_json(array)
|
17
|
+
"[" << array.map {|string| string_to_json(string) } .join(',') << "]"
|
18
|
+
end
|
19
|
+
|
20
|
+
def string_to_json(string)
|
21
|
+
# replace double quotes with single ones
|
22
|
+
string.to_json.gsub(/^"/, "'").gsub(/"$/, "'")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module GoogleAnalytics
|
2
|
+
module Events
|
3
|
+
class SetAccount < Event
|
4
|
+
def initialize(account_id)
|
5
|
+
super('_setAccount', account_id)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class SetDomainName < Event
|
10
|
+
def initialize(domain_name)
|
11
|
+
super('_setDomainName', domain_name)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class SetAllowLinker < Event
|
16
|
+
def initialize(allow)
|
17
|
+
super('_setAllowLinker', allow)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class TrackPageview < Event
|
22
|
+
# @param page [String] optional virtual pageview tracking (see http://code.google.com/apis/analytics/docs/tracking/asyncMigrationExamples.html#VirtualPageviews)
|
23
|
+
def initialize(page = nil)
|
24
|
+
page && page != '' ? super('_trackPageview', page) : super('_trackPageview')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class TrackEvent < Event
|
29
|
+
def initialize(category, action, label = nil, value = nil)
|
30
|
+
if label || value
|
31
|
+
super('_trackEvent', category.to_s, action.to_s, label ? label.to_s : nil, value ? value.to_i : nil)
|
32
|
+
else
|
33
|
+
super('_trackEvent', category.to_s, action.to_s)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module Ecommerce
|
39
|
+
class AddTransaction < Event
|
40
|
+
|
41
|
+
# _gaq.push(['_addTrans',
|
42
|
+
# '1234', // order ID - required
|
43
|
+
# 'Acme Clothing', // affiliation or store name
|
44
|
+
# '11.99', // total - required
|
45
|
+
# '1.29', // tax
|
46
|
+
# '5', // shipping
|
47
|
+
# 'San Jose', // city
|
48
|
+
# 'California', // state or province
|
49
|
+
# 'USA' // country
|
50
|
+
# ]);
|
51
|
+
#
|
52
|
+
def initialize(order_id, store_name, total, tax, shipping, city, state_or_province, country)
|
53
|
+
super('_addTrans', order_id.to_s, store_name.to_s, total.to_s, tax.to_s, shipping.to_s, city.to_s, state_or_province.to_s, country.to_s)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class AddItem < Event
|
58
|
+
|
59
|
+
# _gaq.push(['_addItem',
|
60
|
+
# '1234', // order ID - required
|
61
|
+
# 'DD44', // SKU/code - required
|
62
|
+
# 'T-Shirt', // product name
|
63
|
+
# 'Green Medium', // category or variation
|
64
|
+
# '11.99', // unit price - required
|
65
|
+
# '1' // quantity - required
|
66
|
+
# ]);
|
67
|
+
#
|
68
|
+
def initialize(order_id, product_id, product_name, product_variation, unit_price, quantity)
|
69
|
+
super('_addItem', order_id.to_s, product_id.to_s, product_name.to_s, product_variation.to_s, unit_price.to_s, quantity.to_s)
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
class TrackTransaction < Event
|
75
|
+
# _gaq.push(['_trackTrans']); // submits transaction to the Analytics servers
|
76
|
+
def initialize
|
77
|
+
super('_trackTrans')
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'active_support/core_ext/string/output_safety'
|
2
|
+
require 'active_support/core_ext/object/blank'
|
3
|
+
|
4
|
+
module GoogleAnalytics::Rails
|
5
|
+
# All the helper methods output raw javascript with single quoted strings. This allows more flexbility in choosing when the event gets sent (on page load, on user action, etc).
|
6
|
+
#
|
7
|
+
# The only exception is {#analytics_init}, which is wrapped in a `<script>` tag.
|
8
|
+
#
|
9
|
+
# @example This event is always sent on page load
|
10
|
+
#
|
11
|
+
# <script>
|
12
|
+
# <%= analytics_track_event "Videos", "Play", "Gone With the Wind" %>
|
13
|
+
# </script>
|
14
|
+
#
|
15
|
+
# @example This event is sent when the visitor clicks on the link
|
16
|
+
#
|
17
|
+
# # note the double quotes around the onclick attribute,
|
18
|
+
# # they are necessary because the javascript is single quoted
|
19
|
+
# <a href="my_url" onclick="<%= analytics_track_event "Videos", "Play", "Gone With the Wind" %>">Link</a>
|
20
|
+
#
|
21
|
+
# @example Full ecommerce example
|
22
|
+
#
|
23
|
+
# # create a new transaction
|
24
|
+
# analytics_add_transaction(
|
25
|
+
# '1234', # order ID - required
|
26
|
+
# 'Acme Clothing', # affiliation or store name
|
27
|
+
# '11.99', # total - required
|
28
|
+
# '1.29', # tax
|
29
|
+
# '5', # shipping
|
30
|
+
# 'San Jose', # city
|
31
|
+
# 'California', # state or province
|
32
|
+
# 'USA' # country
|
33
|
+
# )
|
34
|
+
#
|
35
|
+
# # add an item to the transaction
|
36
|
+
# analytics_add_item(
|
37
|
+
# '1234', # order ID - required
|
38
|
+
# 'DD44', # SKU/code - required
|
39
|
+
# 'T-Shirt', # product name
|
40
|
+
# 'Green Medium', # category or variation
|
41
|
+
# '11.99', # unit price - required
|
42
|
+
# '1' # quantity - required
|
43
|
+
# )
|
44
|
+
#
|
45
|
+
# # submit the transaction
|
46
|
+
# analytics_track_transaction
|
47
|
+
#
|
48
|
+
module ViewHelpers
|
49
|
+
# Initializes the Analytics javascript. Put it in the `<head>` tag.
|
50
|
+
#
|
51
|
+
# @param options [Hash]
|
52
|
+
# @option options [Boolean] :local (false) Sets the local development mode.
|
53
|
+
# See http://www.google.com/support/forum/p/Google%20Analytics/thread?tid=741739888e14c07a&hl=en
|
54
|
+
# @option options [Array, GoogleAnalytics::Event] :add_events ([])
|
55
|
+
# The page views are tracked by default, additional events can be added here.
|
56
|
+
# @options options [String] :page
|
57
|
+
# The optional virtual page view to track through {GA::Events::TrackPageview.new}
|
58
|
+
# @options options [String] :tracker
|
59
|
+
# The tracker to use instead of the default {GoogleAnalytics.tracker}
|
60
|
+
#
|
61
|
+
# @example Set the local bit in development mode
|
62
|
+
# analytics_init :local => Rails.env.development?
|
63
|
+
#
|
64
|
+
# @example Allow links across domains
|
65
|
+
# analytics_init :add_events => Events::SetAllowLinker.new(true)
|
66
|
+
#
|
67
|
+
# @return [String] a `<script>` tag, containing the analytics initialization sequence.
|
68
|
+
#
|
69
|
+
def analytics_init(options = {})
|
70
|
+
unless tracker = options.delete(:tracker).presence
|
71
|
+
tracker = GA.tracker
|
72
|
+
raise ArgumentError, "Tracker must be set! Did you set GA.tracker ?" unless tracker
|
73
|
+
end
|
74
|
+
|
75
|
+
local = options.delete(:local) || false
|
76
|
+
events = options.delete(:add_events) || []
|
77
|
+
events = [events] unless events.is_a?(Array)
|
78
|
+
|
79
|
+
queue = GAQ.new
|
80
|
+
|
81
|
+
# unshift => reverse order
|
82
|
+
events.unshift GA::Events::TrackPageview.new(options[:page])
|
83
|
+
events.unshift GA::Events::SetAccount.new(tracker)
|
84
|
+
|
85
|
+
if local
|
86
|
+
events.push GA::Events::SetDomainName.new('none')
|
87
|
+
events.push GA::Events::SetAllowLinker.new(true)
|
88
|
+
end
|
89
|
+
|
90
|
+
events.each do |event|
|
91
|
+
queue << event
|
92
|
+
end
|
93
|
+
|
94
|
+
queue.to_s.html_safe
|
95
|
+
end
|
96
|
+
|
97
|
+
# Track a custom event
|
98
|
+
# @see http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html
|
99
|
+
#
|
100
|
+
# @example
|
101
|
+
#
|
102
|
+
# analytics_track_event "Videos", "Play", "Gone With the Wind"
|
103
|
+
#
|
104
|
+
def analytics_track_event(category, action, label = nil, value = nil)
|
105
|
+
analytics_render_event(GA::Events::TrackEvent.new(category, action, label, value))
|
106
|
+
end
|
107
|
+
|
108
|
+
# Track an ecommerce transaction
|
109
|
+
# @see http://code.google.com/apis/analytics/docs/tracking/gaTrackingEcommerce.html
|
110
|
+
def analytics_add_transaction(order_id, store_name, total, tax, shipping, city, state_or_province, country)
|
111
|
+
analytics_render_event(GA::Events::Ecommerce::AddTransaction.new(order_id, store_name, total, tax, shipping, city, state_or_province, country))
|
112
|
+
end
|
113
|
+
|
114
|
+
# Add an item to the current transaction
|
115
|
+
# @see http://code.google.com/apis/analytics/docs/tracking/gaTrackingEcommerce.html
|
116
|
+
def analytics_add_item(order_id, product_id, product_name, product_variation, unit_price, quantity)
|
117
|
+
analytics_render_event(GA::Events::Ecommerce::AddItem.new(order_id, product_id, product_name, product_variation, unit_price, quantity))
|
118
|
+
end
|
119
|
+
|
120
|
+
# Flush the current transaction
|
121
|
+
# @see http://code.google.com/apis/analytics/docs/tracking/gaTrackingEcommerce.html
|
122
|
+
def analytics_track_transaction
|
123
|
+
analytics_render_event(GA::Events::Ecommerce::TrackTransaction.new)
|
124
|
+
end
|
125
|
+
|
126
|
+
private
|
127
|
+
|
128
|
+
def analytics_render_event(event)
|
129
|
+
raise ArgumentError, "Tracker must be set! Did you set GA.tracker ?" unless GA.valid_tracker?
|
130
|
+
GA::EventRenderer.new(event, nil).to_s.html_safe
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class AsyncTrackingQueueTest < Test::Unit::TestCase
|
4
|
+
VALID_SNIPPET = <<-JAVASCRIPT
|
5
|
+
<script type="text/javascript">
|
6
|
+
var _gaq = _gaq || [];
|
7
|
+
_gaq.push(['event1',1]);
|
8
|
+
_gaq.push(['event2',2]);
|
9
|
+
_gaq.push(['t2.event1',1]);
|
10
|
+
_gaq.push(['t2.event2',2]);
|
11
|
+
(function() {
|
12
|
+
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
13
|
+
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
14
|
+
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
15
|
+
})();
|
16
|
+
</script>
|
17
|
+
JAVASCRIPT
|
18
|
+
|
19
|
+
def test_queue_renders_valid_javascript_snippit
|
20
|
+
gaq = GAQ.new
|
21
|
+
|
22
|
+
# Add 2 events to the default tracker
|
23
|
+
gaq << GA::Event.new('event1', 1)
|
24
|
+
gaq << GA::Event.new('event2', 2)
|
25
|
+
|
26
|
+
# Add 2 events for an alternate tracker
|
27
|
+
gaq.push(GA::Event.new('event1', 1), 't2')
|
28
|
+
gaq.push(GA::Event.new('event2', 2), 't2')
|
29
|
+
|
30
|
+
assert_equal(VALID_SNIPPET, gaq.to_s)
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class EventCollectionRendererTest < Test::Unit::TestCase
|
4
|
+
def test_event_collection_renderer_yield_proper_javascript_snippit_for_default_tracker
|
5
|
+
event_collection = GA::EventCollection.new
|
6
|
+
event_collection << GA::Event.new('event1', 1)
|
7
|
+
event_collection << GA::Event.new('event2', 2)
|
8
|
+
event_collection << GA::Event.new('event3', 3)
|
9
|
+
|
10
|
+
ecr = GA::EventCollectionRenderer.new(event_collection, nil)
|
11
|
+
assert_equal("_gaq.push(['event1',1]);\n_gaq.push(['event2',2]);\n_gaq.push(['event3',3]);", ecr.to_s)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_event_collection_renderer_yield_proper_javascript_snippit_for_custom_tracker
|
15
|
+
event_collection = GA::EventCollection.new
|
16
|
+
event_collection << GA::Event.new('event1', 1)
|
17
|
+
event_collection << GA::Event.new('event2', 2)
|
18
|
+
event_collection << GA::Event.new('event3', 3)
|
19
|
+
|
20
|
+
ecr = GA::EventCollectionRenderer.new(event_collection, 't2')
|
21
|
+
assert_equal("_gaq.push(['t2.event1',1]);\n_gaq.push(['t2.event2',2]);\n_gaq.push(['t2.event3',3]);", ecr.to_s)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class EventCollectionTest < Test::Unit::TestCase
|
4
|
+
def test_event_collection_raises_on_non_event_insertion
|
5
|
+
ec = GA::EventCollection.new
|
6
|
+
assert_raise(GA::EventCollection::InvalidEventError) { ec << "This is invalid" }
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_event_collection_is_enumerable_and_iterates_in_insertion_order
|
10
|
+
ec = GA::EventCollection.new
|
11
|
+
|
12
|
+
assert(ec.respond_to?(:each))
|
13
|
+
|
14
|
+
ec << (event0 = GA::Event.new('sample', 'test'))
|
15
|
+
ec << (event1 = GA::Event.new('sample2', 'test2'))
|
16
|
+
ec << (event3 = GA::Event.new('sample3', 'test3'))
|
17
|
+
|
18
|
+
items = ec.map { |e| e }
|
19
|
+
|
20
|
+
assert_equal(event0, items[0])
|
21
|
+
assert_equal(event1, items[1])
|
22
|
+
assert_equal(event3, items[2])
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class EventRendererTest < Test::Unit::TestCase
|
4
|
+
def test_event_renderer_yield_proper_javascript_snippit_for_default_tracker
|
5
|
+
er = GA::EventRenderer.new(GA::Event.new('_someEvent', 1, 2, 3), nil)
|
6
|
+
assert_equal("_gaq.push(['_someEvent',1,2,3]);", er.to_s)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_event_renderer_yield_proper_javascript_snippit_for_custom_tracker
|
10
|
+
er = GA::EventRenderer.new(GA::Event.new('_someEvent', 1, 2, 3), 't2')
|
11
|
+
assert_equal("_gaq.push(['t2._someEvent',1,2,3]);", er.to_s)
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class GAEventsTest < Test::Unit::TestCase
|
4
|
+
def test_set_account_event
|
5
|
+
event = GA::Events::SetAccount.new('ABC123')
|
6
|
+
assert_equal('_setAccount', event.name)
|
7
|
+
assert_equal(['ABC123'], event.params)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_set_domain_name_event
|
11
|
+
event = GA::Events::SetDomainName.new('foo.com')
|
12
|
+
assert_equal('_setDomainName', event.name)
|
13
|
+
assert_equal(['foo.com'], event.params)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_track_pageview_event
|
17
|
+
event = GA::Events::TrackPageview.new
|
18
|
+
assert_equal('_trackPageview', event.name)
|
19
|
+
assert_equal([], event.params)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_track_pageview_event_with_virtual_page
|
23
|
+
event = GA::Events::TrackPageview.new('/foo/bar')
|
24
|
+
assert_equal('_trackPageview', event.name)
|
25
|
+
assert_equal(['/foo/bar'], event.params)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_track_event_without_category_or_label
|
29
|
+
event = GA::Events::TrackEvent.new('Search', 'Executed')
|
30
|
+
assert_equal('_trackEvent', event.name)
|
31
|
+
assert_equal(['Search', 'Executed'], event.params)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_track_event_with_label
|
35
|
+
event = GA::Events::TrackEvent.new('Search', 'Executed', 'Son of Sam')
|
36
|
+
assert_equal('_trackEvent', event.name)
|
37
|
+
assert_equal(['Search', 'Executed', 'Son of Sam', nil], event.params)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_track_event_with_value
|
41
|
+
event = GA::Events::TrackEvent.new('Search', 'Executed', nil, 1)
|
42
|
+
assert_equal('_trackEvent', event.name)
|
43
|
+
assert_equal(['Search', 'Executed', nil, 1], event.params)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_track_event_with_label_and_value
|
47
|
+
event = GA::Events::TrackEvent.new('Search', 'Executed', 'Son of Sam', 1)
|
48
|
+
assert_equal('_trackEvent', event.name)
|
49
|
+
assert_equal(['Search', 'Executed', 'Son of Sam', 1], event.params)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_ecommerce_add_transaction_event
|
53
|
+
event = GA::Events::Ecommerce::AddTransaction.new(1, 'ACME', 123.45, 13.27, 75.35, 'Dallas', 'TX', 'USA')
|
54
|
+
assert_equal('_addTrans', event.name)
|
55
|
+
assert_equal(['1', 'ACME', '123.45', '13.27', '75.35', 'Dallas', 'TX', 'USA'], event.params)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_ecommerce_add_item_event
|
59
|
+
event = GA::Events::Ecommerce::AddItem.new(1, 123, 'Bacon', 'Chunky', 5.00, 42)
|
60
|
+
assert_equal('_addItem', event.name)
|
61
|
+
assert_equal(['1', '123', 'Bacon', 'Chunky', '5.0', '42'], event.params)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_ecommerce_track_trans_event
|
65
|
+
event = GA::Events::Ecommerce::TrackTransaction.new
|
66
|
+
assert_equal('_trackTrans', event.name)
|
67
|
+
assert_equal([], event.params)
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'google-analytics/rails/view_helpers'
|
3
|
+
|
4
|
+
class ViewHelpersTest < Test::Unit::TestCase
|
5
|
+
include GoogleAnalytics::Rails::ViewHelpers
|
6
|
+
|
7
|
+
VALID_INIT = <<-JAVASCRIPT
|
8
|
+
<script type="text/javascript">
|
9
|
+
var _gaq = _gaq || [];
|
10
|
+
_gaq.push(['_setAccount','TEST']);
|
11
|
+
_gaq.push(['_trackPageview']);
|
12
|
+
(function() {
|
13
|
+
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
14
|
+
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
15
|
+
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
16
|
+
})();
|
17
|
+
</script>
|
18
|
+
JAVASCRIPT
|
19
|
+
|
20
|
+
def test_analytics_init
|
21
|
+
assert_equal(VALID_INIT, analytics_init)
|
22
|
+
end
|
23
|
+
|
24
|
+
VALID_INIT_WITH_VIRTUAL_PAGEVIEW = <<-JAVASCRIPT
|
25
|
+
<script type="text/javascript">
|
26
|
+
var _gaq = _gaq || [];
|
27
|
+
_gaq.push(['_setAccount','TEST']);
|
28
|
+
_gaq.push(['_trackPageview','/some/virtual/url']);
|
29
|
+
(function() {
|
30
|
+
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
31
|
+
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
32
|
+
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
33
|
+
})();
|
34
|
+
</script>
|
35
|
+
JAVASCRIPT
|
36
|
+
|
37
|
+
def test_analytics_init_with_virtual_pageview
|
38
|
+
assert_equal(VALID_INIT_WITH_VIRTUAL_PAGEVIEW, analytics_init(:page => '/some/virtual/url'))
|
39
|
+
end
|
40
|
+
|
41
|
+
VALID_INIT_WITH_CUSTOM_TRACKER = <<-JAVASCRIPT
|
42
|
+
<script type="text/javascript">
|
43
|
+
var _gaq = _gaq || [];
|
44
|
+
_gaq.push(['_setAccount','UA-CUSTOM-XX']);
|
45
|
+
_gaq.push(['_trackPageview']);
|
46
|
+
(function() {
|
47
|
+
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
48
|
+
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
49
|
+
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
50
|
+
})();
|
51
|
+
</script>
|
52
|
+
JAVASCRIPT
|
53
|
+
|
54
|
+
def test_analytics_init_with_custom_tracker
|
55
|
+
assert_equal(VALID_INIT_WITH_CUSTOM_TRACKER, analytics_init(:tracker => 'UA-CUSTOM-XX'))
|
56
|
+
end
|
57
|
+
|
58
|
+
VALID_LOCAL_INIT = <<-JAVASCRIPT
|
59
|
+
<script type="text/javascript">
|
60
|
+
var _gaq = _gaq || [];
|
61
|
+
_gaq.push(['_setAccount','TEST']);
|
62
|
+
_gaq.push(['_trackPageview']);
|
63
|
+
_gaq.push(['_setDomainName','none']);
|
64
|
+
_gaq.push(['_setAllowLinker',true]);
|
65
|
+
(function() {
|
66
|
+
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
67
|
+
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
68
|
+
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
69
|
+
})();
|
70
|
+
</script>
|
71
|
+
JAVASCRIPT
|
72
|
+
|
73
|
+
def test_local_analytics_init
|
74
|
+
assert_equal(VALID_LOCAL_INIT, analytics_init(:local => true))
|
75
|
+
end
|
76
|
+
|
77
|
+
VALID_EVENT_INIT = <<-JAVASCRIPT
|
78
|
+
<script type="text/javascript">
|
79
|
+
var _gaq = _gaq || [];
|
80
|
+
_gaq.push(['_setAccount','TEST']);
|
81
|
+
_gaq.push(['_trackPageview']);
|
82
|
+
_gaq.push(['_setAllowLinker',true]);
|
83
|
+
(function() {
|
84
|
+
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
85
|
+
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
86
|
+
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
87
|
+
})();
|
88
|
+
</script>
|
89
|
+
JAVASCRIPT
|
90
|
+
|
91
|
+
def test_analytics_init_with_events
|
92
|
+
assert_equal(VALID_EVENT_INIT, analytics_init(:add_events => GA::Events::SetAllowLinker.new(true)))
|
93
|
+
end
|
94
|
+
|
95
|
+
VALID_TRACK_EVENT = "_gaq.push(['_trackEvent','Videos','Play','Gone With the Wind',null]);"
|
96
|
+
|
97
|
+
def test_analytics_track_event
|
98
|
+
event = analytics_track_event("Videos", "Play", "Gone With the Wind")
|
99
|
+
assert_equal(VALID_TRACK_EVENT, event)
|
100
|
+
end
|
101
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google-analytics-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Benoit Garret
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-09 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: yard
|
16
|
+
requirement: &80528650 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *80528650
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: redcarpet
|
27
|
+
requirement: &80528440 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *80528440
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: activesupport
|
38
|
+
requirement: &80528190 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '3.0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *80528190
|
47
|
+
description: Rails helpers to manage google analytics tracking
|
48
|
+
email:
|
49
|
+
- benoit.garret@gadz.org
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .yardopts
|
56
|
+
- Gemfile
|
57
|
+
- README.markdown
|
58
|
+
- Rakefile
|
59
|
+
- google-analytics-rails.gemspec
|
60
|
+
- lib/google-analytics-rails.rb
|
61
|
+
- lib/google-analytics/async_tracking_queue.rb
|
62
|
+
- lib/google-analytics/events.rb
|
63
|
+
- lib/google-analytics/events/event.rb
|
64
|
+
- lib/google-analytics/events/event_collection.rb
|
65
|
+
- lib/google-analytics/events/event_collection_renderer.rb
|
66
|
+
- lib/google-analytics/events/event_renderer.rb
|
67
|
+
- lib/google-analytics/events/events.rb
|
68
|
+
- lib/google-analytics/rails/railtie.rb
|
69
|
+
- lib/google-analytics/rails/view_helpers.rb
|
70
|
+
- lib/google-analytics/version.rb
|
71
|
+
- test/async_tracking_queue_test.rb
|
72
|
+
- test/event_collection_renderer_test.rb
|
73
|
+
- test/event_collection_test.rb
|
74
|
+
- test/event_renderer_test.rb
|
75
|
+
- test/gaq_events_test.rb
|
76
|
+
- test/rails/views_helper_test.rb
|
77
|
+
- test/test_helper.rb
|
78
|
+
homepage: https://github.com/bgarret/google-analytics-rails
|
79
|
+
licenses: []
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project: google-analytics-rails
|
98
|
+
rubygems_version: 1.8.10
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: Rails helpers to manage google analytics tracking
|
102
|
+
test_files:
|
103
|
+
- test/async_tracking_queue_test.rb
|
104
|
+
- test/event_collection_renderer_test.rb
|
105
|
+
- test/event_collection_test.rb
|
106
|
+
- test/event_renderer_test.rb
|
107
|
+
- test/gaq_events_test.rb
|
108
|
+
- test/rails/views_helper_test.rb
|
109
|
+
- test/test_helper.rb
|
110
|
+
has_rdoc:
|