gabba 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  pkg/*
2
2
  *.gem
3
3
  .bundle
4
+ Guardfile
data/README CHANGED
@@ -1,12 +1,41 @@
1
- == Simple class to send custom server-side events to Google Analytics
1
+ Simple class to send custom server-side events to Google Analytics
2
+ ==================================================================
2
3
 
3
4
  Heavily influenced by the http://code.google.com/p/serversidegoogleanalytics
4
5
 
5
6
  HOW TO USE:
7
+ -----------
6
8
 
7
- - Track page views
8
- Gabba::Gabba.new("UT-1234", "mydomain.com").page_view("something", "track/me")
9
+ * Track page views
9
10
 
10
- - Track custom events
11
- Gabba::Gabba.new("UT-1234", "mydomain.com").event("Videos", "Play", "ID", "123")
11
+ Gabba::Gabba.new("UT-1234", "mydomain.com").page_view("something", "track/me")
12
+
13
+ * Track custom events
14
+
15
+ Gabba::Gabba.new("UT-1234", "mydomain.com").event("Videos", "Play", "ID", "123", true)
16
+
17
+ * Setting custom vars
18
+
19
+ # Index: 1 through 5
20
+ index = 1
21
+
22
+ # Scope: VISITOR, SESSION or PAGE
23
+ scope = Gabba::Gabba::VISITOR
24
+
25
+ # Set var
26
+ gabba.set_custom_var(index, 'Name', 'Value', scope)
27
+
28
+ # Track the event (all vars will be included)
29
+ gabba.event(...)
30
+
31
+ # Track the page view (all vars will be included)
32
+ gabba.page_view(...)
33
+
34
+ * Removing custom vars
35
+
36
+ # Index: 1 through 5
37
+ index = 1
38
+
39
+ # Delete var with this index
40
+ gabba.delete_custom_var index- Track a non-interactive custom event
12
41
 
@@ -14,6 +14,13 @@ module Gabba
14
14
  GOOGLE_HOST = "www.google-analytics.com"
15
15
  BEACON_PATH = "/__utm.gif"
16
16
  USER_AGENT = "Gabba #{VERSION} Agent"
17
+
18
+ # Custom var levels
19
+ VISITOR = 1
20
+ SESSION = 2
21
+ PAGE = 3
22
+
23
+ ESCAPES = %w{ ' ! * ) }
17
24
 
18
25
  attr_accessor :utmwv, :utmn, :utmhn, :utmcs, :utmul, :utmdt, :utmp, :utmac, :utmt, :utmcc, :user_agent
19
26
 
@@ -28,15 +35,48 @@ module Gabba
28
35
  @utmac = ga_acct
29
36
  @utmhn = domain
30
37
  @user_agent = agent
38
+
39
+ @custom_vars = []
31
40
  end
32
41
 
42
+ def set_custom_var(index, name, value, scope)
43
+ raise "Index must be between 1 and 5" unless (1..5).include?(index)
44
+ raise "Scope must be 1 (VISITOR), 2 (SESSION) or 3 (PAGE)" unless (1..3).include?(scope)
45
+
46
+ @custom_vars[index] = [ name, value, scope ]
47
+ end
48
+
49
+ def delete_custom_var(index)
50
+ raise "Index must be between 1 and 5" unless (1..5).include?(index)
51
+
52
+ @custom_vars.delete_at(index)
53
+ end
54
+
55
+ def custom_var_data
56
+ names = []
57
+ values = []
58
+ scopes = []
59
+
60
+ idx = 1
61
+ @custom_vars.each_with_index do |(n, v, s), i|
62
+ next if !n || !v || (/\w/ !~ n) || (/\w/ !~ v)
63
+ prefix = "#{i}!" if idx != i
64
+ names << "#{prefix}#{escape(n)}"
65
+ values << "#{prefix}#{escape(v)}"
66
+ scopes << "#{prefix}#{escape(s)}"
67
+ idx = i + 1
68
+ end
69
+
70
+ names.empty? ? "" : "8(#{names.join('*')})9(#{values.join('*')})11(#{scopes.join('*')})"
71
+ end
72
+
33
73
  def page_view(title, page, utmhid = random_id)
34
74
  check_account_params
35
75
  hey(page_view_params(title, page, utmhid))
36
76
  end
37
77
 
38
78
  def page_view_params(title, page, utmhid = random_id)
39
- {
79
+ options = {
40
80
  :utmwv => @utmwv,
41
81
  :utmn => @utmn,
42
82
  :utmhn => @utmhn,
@@ -48,20 +88,28 @@ module Gabba
48
88
  :utmac => @utmac,
49
89
  :utmcc => @utmcc || cookie_params
50
90
  }
91
+
92
+ # Add custom vars if present
93
+ cvd = custom_var_data
94
+ options[:utme] = cvd if /\w/ =~ cvd
95
+
96
+ options
51
97
  end
52
98
 
53
- def event(category, action, label = nil, value = nil, utmhid = random_id)
99
+ def event(category, action, label = nil, value = nil, utmni = false, utmhid = random_id)
54
100
  check_account_params
55
- hey(event_params(category, action, label, value, utmhid))
101
+ hey(event_params(category, action, label, value, utmni, utmhid))
56
102
  end
57
103
 
58
- def event_params(category, action, label = nil, value = nil, utmhid = nil)
104
+ def event_params(category, action, label = nil, value = nil, utmni = false, utmhid = false)
105
+ raise ArgumentError.new("utmni must be a boolean") if (utmni.class != TrueClass && utmni.class != FalseClass)
59
106
  {
60
107
  :utmwv => @utmwv,
61
108
  :utmn => @utmn,
62
109
  :utmhn => @utmhn,
110
+ :utmni => (1 if utmni), # 1 for non interactive event, excluded from bounce calcs
63
111
  :utmt => 'event',
64
- :utme => event_data(category, action, label, value),
112
+ :utme => "#{event_data(category, action, label, value)}#{custom_var_data}",
65
113
  :utmcs => @utmcs,
66
114
  :utmul => @utmul,
67
115
  :utmhid => utmhid,
@@ -156,6 +204,7 @@ module Gabba
156
204
  # makes the tracking call to Google Analytics
157
205
  def hey(params)
158
206
  query = params.map {|k,v| "#{k}=#{URI.escape(v.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}" }.join('&')
207
+
159
208
  response = Net::HTTP.start(GOOGLE_HOST) do |http|
160
209
  request = Net::HTTP::Get.new("#{BEACON_PATH}?#{query}")
161
210
  request["User-Agent"] = URI.escape(user_agent)
@@ -171,6 +220,14 @@ module Gabba
171
220
  rand 8999999999 + 1000000000
172
221
  end
173
222
 
223
+ def escape(t)
224
+ return t if !t || (/\w/ !~ t.to_s)
225
+
226
+ t.to_s.gsub(/[\*'!\)]/) do |m|
227
+ "'#{ESCAPES.index(m)}"
228
+ end
229
+ end
230
+
174
231
  end # Gabba Class
175
232
 
176
- end
233
+ end
@@ -1,5 +1,5 @@
1
1
  module Gabba
2
2
  unless const_defined?('VERSION')
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -32,7 +32,7 @@ describe Gabba::Gabba do
32
32
  @gabba = Gabba::Gabba.new("abc", "123")
33
33
  @gabba.utmn = "1009731272"
34
34
  @gabba.utmcc = ''
35
- stub_analytics @gabba.event_params("cat1", "act1", "lab1", "val1", "6783939397")
35
+ stub_analytics @gabba.event_params("cat1", "act1", "lab1", "val1", false, "6783939397")
36
36
  end
37
37
 
38
38
  it "must require GA account" do
@@ -52,7 +52,11 @@ describe Gabba::Gabba do
52
52
  end
53
53
 
54
54
  it "must do event request to google" do
55
- @gabba.event("cat1", "act1", "lab1", "val1", "6783939397").code.must_equal("200")
55
+ @gabba.event("cat1", "act1", "lab1", "val1", false, "6783939397").code.must_equal("200")
56
+ end
57
+
58
+ it "must be able to send non interactive events" do
59
+ @gabba.event("cat1", "act1", "lab1", "val1", true).code.must_equal("200")
56
60
  end
57
61
 
58
62
  end
@@ -99,6 +103,67 @@ describe Gabba::Gabba do
99
103
  end
100
104
  end
101
105
 
106
+ describe "setting a custom var" do
107
+ before do
108
+ @gabba = Gabba::Gabba.new("abc", "123")
109
+ @gabba.utmn = "1009731272"
110
+ @gabba.utmcc = ''
111
+ end
112
+
113
+ it "must return data for a valid var" do
114
+ @gabba.set_custom_var 1, 'A (B*\'!)', 'Yes', Gabba::Gabba::SESSION
115
+ @gabba.custom_var_data.must_equal "8(A (B'2'0'1'3)9(Yes)11(2)"
116
+ end
117
+
118
+ it "must return data for several valid vards" do
119
+ @gabba.set_custom_var 1, 'A', 'Yes', Gabba::Gabba::SESSION
120
+ @gabba.set_custom_var 2, 'B', 'No', Gabba::Gabba::VISITOR
121
+ @gabba.custom_var_data.must_equal "8(A*B)9(Yes*No)11(2*1)"
122
+ end
123
+
124
+ it "must return an empty string if vars aren't set" do
125
+ @gabba.custom_var_data.must_equal ""
126
+ end
127
+
128
+ it "must not include var with an empty value" do
129
+ @gabba.set_custom_var 1, 'A', 'Yes', Gabba::Gabba::SESSION
130
+ @gabba.set_custom_var 2, 'B', '', Gabba::Gabba::VISITOR
131
+ @gabba.set_custom_var 3, 'C', ' ', Gabba::Gabba::VISITOR
132
+ @gabba.set_custom_var 4, 'D', nil, Gabba::Gabba::VISITOR
133
+ @gabba.custom_var_data.must_equal "8(A)9(Yes)11(2)"
134
+ end
135
+
136
+ it "must mention index of the var if non sequential" do
137
+ @gabba.set_custom_var 2, 'A', 'Y', Gabba::Gabba::SESSION
138
+ @gabba.set_custom_var 4, 'D', 'N', Gabba::Gabba::VISITOR
139
+ @gabba.custom_var_data.must_equal "8(2!A*4!D)9(2!Y*4!N)11(2!2*4!1)"
140
+ end
141
+
142
+ it "must raise an error if index is outside the 1-5 (incl) range" do
143
+ lambda { @gabba.set_custom_var(0, 'A', 'B', 1) }.must_raise(RuntimeError)
144
+ lambda { @gabba.set_custom_var(6, 'A', 'B', 1) }.must_raise(RuntimeError)
145
+ end
146
+
147
+ it "must raise an error if scope is outside the 1-3 (incl) range" do
148
+ lambda { @gabba.set_custom_var(1, 'A', 'B', 0) }.must_raise(RuntimeError)
149
+ lambda { @gabba.set_custom_var(1, 'A', 'B', 4) }.must_raise(RuntimeError)
150
+ end
151
+ end
152
+
153
+ describe 'delete custom var' do
154
+ before do
155
+ @gabba = Gabba::Gabba.new("abc", "123")
156
+ @gabba.utmn = "1009731272"
157
+ @gabba.utmcc = ''
158
+ end
159
+
160
+ it "must return data for a valid var" do
161
+ @gabba.set_custom_var 1, 'A (B*\'!)', 'Yes', Gabba::Gabba::SESSION
162
+ @gabba.delete_custom_var 1
163
+ @gabba.custom_var_data.must_equal ""
164
+ end
165
+ end
166
+
102
167
  def stub_analytics(expected_params)
103
168
  s = stub_request(:get, /www.google-analytics.com\/__utm.gif\?utmac=#{expected_params[:utmac]}&.*/).
104
169
  to_return(:status => 200, :body => "", :headers => {})
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gabba
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ron Evans
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-28 00:00:00 -07:00
19
- default_executable:
18
+ date: 2012-03-23 00:00:00 Z
20
19
  dependencies: []
21
20
 
22
21
  description: Easy server-side tracking for Google Analytics
@@ -40,7 +39,6 @@ files:
40
39
  - lib/gabba/version.rb
41
40
  - spec/gabba_spec.rb
42
41
  - spec/spec_helper.rb
43
- has_rdoc: true
44
42
  homepage: ""
45
43
  licenses: []
46
44
 
@@ -70,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
68
  requirements: []
71
69
 
72
70
  rubyforge_project: gabba
73
- rubygems_version: 1.6.2
71
+ rubygems_version: 1.8.6
74
72
  signing_key:
75
73
  specification_version: 3
76
74
  summary: Easy server-side tracking for Google Analytics