gabba 0.0.3 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gabba (0.0.1)
4
+ gabba (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/lib/gabba/gabba.rb CHANGED
@@ -22,25 +22,20 @@ module Gabba
22
22
  @utmcs = "UTF-8" # charset
23
23
  @utmul = "en-us" # language
24
24
 
25
- @utmn = rand(8999999999) + 1000000000
26
- @utmhid = rand(8999999999) + 1000000000
25
+ @utmn = random_id
26
+ @utmhid = random_id
27
27
 
28
28
  @utmac = ga_acct
29
29
  @utmhn = domain
30
30
  @user_agent = agent
31
31
  end
32
32
 
33
- def page_view(title, page, utmhid = rand(8999999999) + 1000000000)
33
+ def page_view(title, page, utmhid = random_id)
34
34
  check_account_params
35
35
  hey(page_view_params(title, page, utmhid))
36
36
  end
37
-
38
- def event(category, action, label = nil, value = nil, utmhid = rand(8999999999) + 1000000000)
39
- check_account_params
40
- hey(event_params(category, action, label, value, utmhid))
41
- end
42
-
43
- def page_view_params(title, page, utmhid = rand(8999999999) + 1000000000)
37
+
38
+ def page_view_params(title, page, utmhid = random_id)
44
39
  {
45
40
  :utmwv => @utmwv,
46
41
  :utmn => @utmn,
@@ -54,8 +49,13 @@ module Gabba
54
49
  :utmcc => @utmcc || cookie_params
55
50
  }
56
51
  end
57
-
58
- def event_params(category, action, label = nil, value = nil, utmhid = rand(8999999999) + 1000000000)
52
+
53
+ def event(category, action, label = nil, value = nil, utmhid = random_id)
54
+ check_account_params
55
+ hey(event_params(category, action, label, value, utmhid))
56
+ end
57
+
58
+ def event_params(category, action, label = nil, value = nil, utmhid = nil)
59
59
  {
60
60
  :utmwv => @utmwv,
61
61
  :utmn => @utmn,
@@ -75,9 +75,75 @@ module Gabba
75
75
  data += "(#{value})" if value
76
76
  data
77
77
  end
78
+
79
+ def transaction(order_id, total, store_name = nil, tax = nil, shipping = nil, city = nil, region = nil, country = nil, utmhid = random_id)
80
+ check_account_params
81
+ hey(transaction_params(order_id, total, store_name, tax, shipping, city, region, country, utmhid))
82
+ end
83
+
84
+ def transaction_params(order_id, total, store_name, tax, shipping, city, region, country, utmhid)
85
+ # '1234', // utmtid URL-encoded order ID - required
86
+ # 'Acme Clothing', // utmtst affiliation or store name
87
+ # '11.99', // utmtto total - required
88
+ # '1.29', // utmttx tax
89
+ # '5', // utmtsp shipping
90
+ # 'San Jose', // utmtci city
91
+ # 'California', // utmtrg state or province
92
+ # 'USA' // utmtco country
93
+ {
94
+ :utmwv => @utmwv,
95
+ :utmn => @utmn,
96
+ :utmhn => @utmhn,
97
+ :utmt => 'tran',
98
+ :utmcs => @utmcs,
99
+ :utmul => @utmul,
100
+ :utmhid => utmhid,
101
+ :utmac => @utmac,
102
+ :utmcc => @utmcc || cookie_params,
103
+ :utmtid => order_id,
104
+ :utmtst => store_name,
105
+ :utmtto => total,
106
+ :utmttx => tax,
107
+ :utmtsp => shipping,
108
+ :utmtci => city,
109
+ :utmtrg => region,
110
+ :utmtco => country
111
+ }
112
+ end
113
+
114
+ def add_item(order_id, item_sku, price, quantity, name = nil, category = nil, utmhid = random_id)
115
+ check_account_params
116
+ hey(item_params(order_id, item_sku, name, category, price, quantity, utmhid))
117
+ end
118
+
119
+ def item_params(order_id, item_sku, name, category, price, quantity, utmhid)
120
+ # '1234', // utmtid URL-encoded order ID - required
121
+ # 'DD44', // utmipc SKU/code - required
122
+ # 'T-Shirt', // utmipn product name
123
+ # 'Green Medium', // utmiva category or variation
124
+ # '11.99', // utmipr unit price - required
125
+ # '1' // utmiqt quantity - required
126
+ {
127
+ :utmwv => @utmwv,
128
+ :utmn => @utmn,
129
+ :utmhn => @utmhn,
130
+ :utmt => 'item',
131
+ :utmcs => @utmcs,
132
+ :utmul => @utmul,
133
+ :utmhid => utmhid,
134
+ :utmac => @utmac,
135
+ :utmcc => @utmcc || cookie_params,
136
+ :utmtid => order_id,
137
+ :utmipc => item_sku,
138
+ :utmipn => name,
139
+ :utmiva => category,
140
+ :utmipr => price,
141
+ :utmiqt => quantity
142
+ }
143
+ end
78
144
 
79
145
  # create magical cookie params used by GA for its own nefarious purposes
80
- def cookie_params(utma1 = rand(89999999) + 10000000, utma2 = rand(1147483647) + 1000000000, today = Time.now)
146
+ def cookie_params(utma1 = random_id, utma2 = rand(1147483647) + 1000000000, today = Time.now)
81
147
  "__utma=1.#{utma1}00145214523.#{utma2}.#{today.to_i}.#{today.to_i}.15;+__utmz=1.#{today.to_i}.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);"
82
148
  end
83
149
 
@@ -89,7 +155,7 @@ module Gabba
89
155
 
90
156
  # makes the tracking call to Google Analytics
91
157
  def hey(params)
92
- query = params.map {|k,v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
158
+ query = params.map {|k,v| "#{k}=#{URI.escape(v.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}" }.join('&')
93
159
  response = Net::HTTP.start(GOOGLE_HOST) do |http|
94
160
  request = Net::HTTP::Get.new("#{BEACON_PATH}?#{query}")
95
161
  request["User-Agent"] = URI.escape(user_agent)
@@ -100,6 +166,11 @@ module Gabba
100
166
  raise GoogleAnalyticsNetworkError unless response.code == "200"
101
167
  response
102
168
  end
103
- end
169
+
170
+ def random_id
171
+ rand 8999999999 + 1000000000
172
+ end
173
+
174
+ end # Gabba Class
104
175
 
105
176
  end
data/lib/gabba/version.rb CHANGED
@@ -1,3 +1,5 @@
1
1
  module Gabba
2
- VERSION = "0.0.3"
2
+ unless const_defined?('VERSION')
3
+ VERSION = "0.1.1"
4
+ end
3
5
  end
data/spec/gabba_spec.rb CHANGED
@@ -56,6 +56,48 @@ describe Gabba::Gabba do
56
56
  end
57
57
 
58
58
  end
59
+
60
+ describe "when tracking an item" do
61
+ before do
62
+ @gabba = Gabba::Gabba.new("abc", "123")
63
+ @gabba.utmn = "1009731272"
64
+ @gabba.utmcc = ''
65
+ stub_analytics @gabba.item_params("orderid", "1234", "widget", "widgets", "9.99", "1", "6783939397")
66
+ end
67
+
68
+ it "must require GA account" do
69
+ lambda {Gabba::Gabba.new(nil, nil).add_item("orderid", "1234", "widget", "widgets", "9.99", "1", "6783939397")}.must_raise(Gabba::NoGoogleAnalyticsAccountError)
70
+ end
71
+
72
+ it "must require GA domain" do
73
+ lambda {Gabba::Gabba.new("abs", nil).add_item("orderid", "1234", "widget", "widgets", "9.99", "1", "6783939397")}.must_raise(Gabba::NoGoogleAnalyticsDomainError)
74
+ end
75
+
76
+ it "must do add item request to google" do
77
+ @gabba.add_item("orderid", "1234", "widget", "widgets", "9.99", "1", "6783939397").code.must_equal("200")
78
+ end
79
+ end
80
+
81
+ describe "when tracking a transaction" do
82
+ before do
83
+ @gabba = Gabba::Gabba.new("abc", "123")
84
+ @gabba.utmn = "1009731272"
85
+ @gabba.utmcc = ''
86
+ stub_analytics @gabba.transaction_params("orderid", "9.99", "acme stores", ".25", "1.00", "San Jose", "CA", "United States", "6783939397")
87
+ end
88
+
89
+ it "must require GA account" do
90
+ lambda {Gabba::Gabba.new(nil, nil).transaction("orderid", "9.99", "acme stores", ".25", "1.00", "San Jose", "CA", "United States", "6783939397")}.must_raise(Gabba::NoGoogleAnalyticsAccountError)
91
+ end
92
+
93
+ it "must require GA domain" do
94
+ lambda {Gabba::Gabba.new("abs", nil).transaction("orderid", "9.99", "acme stores", ".25", "1.00", "San Jose", "CA", "United States", "6783939397")}.must_raise(Gabba::NoGoogleAnalyticsDomainError)
95
+ end
96
+
97
+ it "must do transaction request to google" do
98
+ @gabba.transaction("orderid", "9.99", "acme stores", ".25", "1.00", "San Jose", "CA", "United States", "6783939397").code.must_equal("200")
99
+ end
100
+ end
59
101
 
60
102
  def stub_analytics(expected_params)
61
103
  s = stub_request(:get, /www.google-analytics.com\/__utm.gif\?utmac=#{expected_params[:utmac]}&.*/).
metadata CHANGED
@@ -2,12 +2,12 @@
2
2
  name: gabba
3
3
  version: !ruby/object:Gem::Version
4
4
  hash: 25
5
- prerelease: false
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 0
9
- - 3
10
- version: 0.0.3
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ron Evans
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-02 00:00:00 -08:00
18
+ date: 2011-05-28 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -70,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
70
  requirements: []
71
71
 
72
72
  rubyforge_project: gabba
73
- rubygems_version: 1.3.7
73
+ rubygems_version: 1.6.2
74
74
  signing_key:
75
75
  specification_version: 3
76
76
  summary: Easy server-side tracking for Google Analytics