google-analytics-rails 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -1,11 +1,11 @@
1
- Fast Google Analytics setup for Rails.
1
+ Fast Google Analytics setup for Rails. This gem is mostly intended for small to medium websites with a simple analytics strategy.
2
2
 
3
3
  Installation
4
4
  ============
5
5
 
6
6
  Add the following to your `Gemfile`:
7
7
 
8
- gem 'google-analytics-rails', :git => 'git://github.com/bgarret/google-analytics-rails.git'
8
+ gem 'google-analytics-rails'
9
9
 
10
10
  Then run:
11
11
 
@@ -38,12 +38,12 @@ Different accounts for development and production
38
38
  `config/environments/production.rb`:
39
39
 
40
40
  # replace this with your production tracker code
41
- GAR.tracker = "UA-xxxxxx-x"
41
+ GA.tracker = "UA-xxxxxx-x"
42
42
 
43
43
  `config/environments/development.rb`:
44
44
 
45
45
  # replace this with your development tracker code
46
- GAR.tracker = "UA-xxxxxx-x"
46
+ GA.tracker = "UA-xxxxxx-x"
47
47
 
48
48
  `app/views/layout/application.html.erb`, in the `<head>` tag :
49
49
 
@@ -55,3 +55,9 @@ License
55
55
  [google-analytics-rails](https://github.com/bgarret.google-analytics-rails) is released under the MIT license:
56
56
 
57
57
  * http://www.opensource.org/licenses/MIT
58
+
59
+ Thanks
60
+ ======
61
+
62
+ Many thanks to [the people that took time to submit patches](https://github.com/bgarret/google-analytics-rails/contributors).
63
+
@@ -21,5 +21,11 @@ module GoogleAnalytics
21
21
  def each
22
22
  @events.each { |e| yield e }
23
23
  end
24
+
25
+ def length
26
+ @events.length
27
+ end
28
+ alias_method :size, :length
29
+
24
30
  end
25
31
  end
@@ -18,8 +18,18 @@ module GoogleAnalytics
18
18
  end
19
19
  end
20
20
 
21
+ class SetSiteSpeedSampleRate < Event
22
+ # @param sample_rate [Integer] the percentage of page views that should be used
23
+ # for page speed metrics - defaults to 1 (aka 1%) if the event is missing.
24
+ # @see http://code.google.com/apis/analytics/docs/gaJS/gaJSApiBasicConfiguration.html#_gat.GA_Tracker_._setSiteSpeedSampleRate
25
+ def initialize(sample_rate)
26
+ super('_setSiteSpeedSampleRate', sample_rate)
27
+ end
28
+ end
29
+
21
30
  class TrackPageview < Event
22
- # @param page [String] optional virtual pageview tracking (see http://code.google.com/apis/analytics/docs/tracking/asyncMigrationExamples.html#VirtualPageviews)
31
+ # @param page [String] optional virtual pageview tracking
32
+ # @see http://code.google.com/apis/analytics/docs/tracking/asyncMigrationExamples.html#VirtualPageviews
23
33
  def initialize(page = nil)
24
34
  page && page != '' ? super('_trackPageview', page) : super('_trackPageview')
25
35
  end
@@ -35,44 +45,56 @@ module GoogleAnalytics
35
45
  end
36
46
  end
37
47
 
48
+ class SetCustomVar < Event
49
+ def initialize(index, name, value, opt_scope)
50
+ super('_setCustomVar', index.to_i, name.to_s, value.to_s, opt_scope.to_i)
51
+ end
52
+ end
53
+
54
+ # @see http://code.google.com/apis/analytics/docs/tracking/gaTrackingEcommerce.html
38
55
  module Ecommerce
56
+ # JavaScript equivalent:
57
+ #
58
+ # _gaq.push(['_addTrans',
59
+ # '1234', // order ID - required
60
+ # 'Acme Clothing', // affiliation or store name
61
+ # '11.99', // total - required
62
+ # '1.29', // tax
63
+ # '5', // shipping
64
+ # 'San Jose', // city
65
+ # 'California', // state or province
66
+ # 'USA' // country
67
+ # ]);
68
+ #
39
69
  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
70
  def initialize(order_id, store_name, total, tax, shipping, city, state_or_province, country)
53
71
  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
72
  end
55
73
  end
56
74
 
75
+ # JavaScript equivalent:
76
+ #
77
+ # _gaq.push(['_addItem',
78
+ # '1234', // order ID - required
79
+ # 'DD44', // SKU/code - required
80
+ # 'T-Shirt', // product name
81
+ # 'Green Medium', // category or variation
82
+ # '11.99', // unit price - required
83
+ # '1' // quantity - required
84
+ # ]);
85
+ #
57
86
  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
87
  def initialize(order_id, product_id, product_name, product_variation, unit_price, quantity)
69
88
  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
89
  end
71
90
 
72
91
  end
73
92
 
93
+ # JavaScript equivalent:
94
+ #
95
+ # _gaq.push(['_trackTrans']);
96
+ #
74
97
  class TrackTransaction < Event
75
- # _gaq.push(['_trackTrans']); // submits transaction to the Analytics servers
76
98
  def initialize
77
99
  super('_trackTrans')
78
100
  end
@@ -53,9 +53,9 @@ module GoogleAnalytics::Rails
53
53
  # See http://www.google.com/support/forum/p/Google%20Analytics/thread?tid=741739888e14c07a&hl=en
54
54
  # @option options [Array, GoogleAnalytics::Event] :add_events ([])
55
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
56
+ # @option options [String] :page
57
+ # The optional virtual page view to track through {GoogleAnalytics::Events::TrackPageview}
58
+ # @option options [String] :tracker
59
59
  # The tracker to use instead of the default {GoogleAnalytics.tracker}
60
60
  #
61
61
  # @example Set the local bit in development mode
@@ -105,6 +105,17 @@ module GoogleAnalytics::Rails
105
105
  analytics_render_event(GA::Events::TrackEvent.new(category, action, label, value))
106
106
  end
107
107
 
108
+ # Set a custom variable.
109
+ # @see http://code.google.com/apis/analytics/docs/tracking/gaTrackingCustomVariables.html
110
+ # You're allowed only 1-5 for the index.
111
+ # The lifetime is defined by:
112
+ # 1 = visitor-level
113
+ # 2 = session-level
114
+ # 3 = page-level (default)
115
+ def analytics_set_custom_var(index, name, value, opt_scope = 3)
116
+ analytics_render_event(GA::Events::SetCustomVar.new(index, name, value, opt_scope))
117
+ end
118
+
108
119
  # Track an ecommerce transaction
109
120
  # @see http://code.google.com/apis/analytics/docs/tracking/gaTrackingEcommerce.html
110
121
  def analytics_add_transaction(order_id, store_name, total, tax, shipping, city, state_or_province, country)
@@ -1,4 +1,4 @@
1
1
  module GoogleAnalytics
2
2
  # Gem version
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
@@ -21,4 +21,22 @@ class EventCollectionTest < Test::Unit::TestCase
21
21
  assert_equal(event1, items[1])
22
22
  assert_equal(event3, items[2])
23
23
  end
24
+
25
+ def test_event_collection_delegates_size_and_length
26
+ ec = GA::EventCollection.new
27
+
28
+ assert(ec.respond_to?(:size))
29
+ assert(ec.respond_to?(:length))
30
+
31
+ assert_equal(0, ec.size)
32
+ assert_equal(ec.size, ec.length)
33
+
34
+ ec << GA::Event.new('sample', 'test')
35
+ assert_equal(1, ec.size)
36
+ assert_equal(ec.size, ec.length)
37
+
38
+ ec << GA::Event.new('sample2', 'test2')
39
+ assert_equal(2, ec.size)
40
+ assert_equal(ec.size, ec.length)
41
+ end
24
42
  end
@@ -13,6 +13,12 @@ class GAEventsTest < Test::Unit::TestCase
13
13
  assert_equal(['foo.com'], event.params)
14
14
  end
15
15
 
16
+ def test_set_site_speed_sample_rate_event
17
+ event = GA::Events::SetSiteSpeedSampleRate.new(5)
18
+ assert_equal('_setSiteSpeedSampleRate', event.name)
19
+ assert_equal([5], event.params)
20
+ end
21
+
16
22
  def test_track_pageview_event
17
23
  event = GA::Events::TrackPageview.new
18
24
  assert_equal('_trackPageview', event.name)
@@ -49,6 +55,12 @@ class GAEventsTest < Test::Unit::TestCase
49
55
  assert_equal(['Search', 'Executed', 'Son of Sam', 1], event.params)
50
56
  end
51
57
 
58
+ def test_set_custom_var
59
+ event = GA::Events::SetCustomVar.new(1, 'VarName1', 'VarVal1', 1)
60
+ assert_equal('_setCustomVar', event.name)
61
+ assert_equal([1, 'VarName1', 'VarVal1', 1], event.params)
62
+ end
63
+
52
64
  def test_ecommerce_add_transaction_event
53
65
  event = GA::Events::Ecommerce::AddTransaction.new(1, 'ACME', 123.45, 13.27, 75.35, 'Dallas', 'TX', 'USA')
54
66
  assert_equal('_addTrans', event.name)
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-09 00:00:00.000000000Z
12
+ date: 2012-04-24 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: yard
16
- requirement: &80528650 !ruby/object:Gem::Requirement
16
+ requirement: &82570290 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *80528650
24
+ version_requirements: *82570290
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: redcarpet
27
- requirement: &80528440 !ruby/object:Gem::Requirement
27
+ requirement: &82570070 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *80528440
35
+ version_requirements: *82570070
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activesupport
38
- requirement: &80528190 !ruby/object:Gem::Requirement
38
+ requirement: &82569800 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '3.0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *80528190
46
+ version_requirements: *82569800
47
47
  description: Rails helpers to manage google analytics tracking
48
48
  email:
49
49
  - benoit.garret@gadz.org