gabba 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -1
- data/Gemfile.lock +6 -0
- data/lib/gabba/gabba.rb +20 -24
- data/lib/gabba/version.rb +1 -1
- data/spec/gabba_spec.rb +17 -3
- data/spec/spec_helper.rb +3 -0
- metadata +4 -4
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -6,7 +6,12 @@ PATH
|
|
6
6
|
GEM
|
7
7
|
remote: http://rubygems.org/
|
8
8
|
specs:
|
9
|
+
addressable (2.2.2)
|
10
|
+
crack (0.1.8)
|
9
11
|
minitest (2.0.0)
|
12
|
+
webmock (1.6.1)
|
13
|
+
addressable (>= 2.2.2)
|
14
|
+
crack (>= 0.1.7)
|
10
15
|
|
11
16
|
PLATFORMS
|
12
17
|
ruby
|
@@ -14,3 +19,4 @@ PLATFORMS
|
|
14
19
|
DEPENDENCIES
|
15
20
|
gabba!
|
16
21
|
minitest
|
22
|
+
webmock
|
data/lib/gabba/gabba.rb
CHANGED
@@ -11,9 +11,8 @@ module Gabba
|
|
11
11
|
class GoogleAnalyticsNetworkError < RuntimeError; end
|
12
12
|
|
13
13
|
class Gabba
|
14
|
-
|
15
|
-
|
16
|
-
BEACON_URL = "/__utm.gif"
|
14
|
+
GOOGLE_HOST = "www.google-analytics.com"
|
15
|
+
BEACON_PATH = "/__utm.gif"
|
17
16
|
USER_AGENT = "Gabba #{VERSION} Agent"
|
18
17
|
|
19
18
|
attr_accessor :utmwv, :utmn, :utmhn, :utmcs, :utmul, :utmdt, :utmp, :utmac, :utmt, :utmcc, :user_agent
|
@@ -36,9 +35,9 @@ module Gabba
|
|
36
35
|
hey(page_view_params(title, page, utmhid))
|
37
36
|
end
|
38
37
|
|
39
|
-
def event(category, action, label = nil, value = nil)
|
38
|
+
def event(category, action, label = nil, value = nil, utmhid = rand(8999999999) + 1000000000)
|
40
39
|
check_account_params
|
41
|
-
hey(event_params(category, action, label, value))
|
40
|
+
hey(event_params(category, action, label, value, utmhid))
|
42
41
|
end
|
43
42
|
|
44
43
|
def page_view_params(title, page, utmhid = rand(8999999999) + 1000000000)
|
@@ -52,7 +51,7 @@ module Gabba
|
|
52
51
|
:utmhid => utmhid,
|
53
52
|
:utmp => page,
|
54
53
|
:utmac => @utmac,
|
55
|
-
:utmcc => cookie_params
|
54
|
+
:utmcc => @utmcc || cookie_params
|
56
55
|
}
|
57
56
|
end
|
58
57
|
|
@@ -67,12 +66,12 @@ module Gabba
|
|
67
66
|
:utmul => @utmul,
|
68
67
|
:utmhid => utmhid,
|
69
68
|
:utmac => @utmac,
|
70
|
-
:utmcc => cookie_params
|
69
|
+
:utmcc => @utmcc || cookie_params
|
71
70
|
}
|
72
71
|
end
|
73
|
-
|
72
|
+
|
74
73
|
def event_data(category, action, label = nil, value = nil)
|
75
|
-
data = "5(#{category}*action" + (label ? "*#{label})" : ")")
|
74
|
+
data = "5(#{category}*action" + (label ? "*#{label})" : ")")
|
76
75
|
data += "(#{value})" if value
|
77
76
|
end
|
78
77
|
|
@@ -80,7 +79,7 @@ module Gabba
|
|
80
79
|
def cookie_params(utma1 = rand(89999999) + 10000000, utma2 = rand(1147483647) + 1000000000, today = Time.now)
|
81
80
|
"__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
81
|
end
|
83
|
-
|
82
|
+
|
84
83
|
# sanity check that we have needed params to even call GA
|
85
84
|
def check_account_params
|
86
85
|
raise NoGoogleAnalyticsAccountError unless @utmac
|
@@ -88,21 +87,18 @@ module Gabba
|
|
88
87
|
end
|
89
88
|
|
90
89
|
# makes the tracking call to Google Analytics
|
91
|
-
def hey(params
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
90
|
+
def hey(params)
|
91
|
+
query = params.map {|k,v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
|
92
|
+
response = Net::HTTP.start(GOOGLE_HOST) do |http|
|
93
|
+
request = Net::HTTP::Get.new("#{BEACON_PATH}?#{query}")
|
94
|
+
request["User-Agent"] = URI.escape(user_agent)
|
95
|
+
request["Accept"] = "*/*"
|
96
|
+
http.request(request)
|
96
97
|
end
|
97
|
-
|
98
|
+
|
99
|
+
raise GoogleAnalyticsNetworkError unless response.code == "200"
|
100
|
+
response
|
98
101
|
end
|
99
|
-
|
100
|
-
# convert params hash to query string
|
101
|
-
def hash_to_querystring(hash = {})
|
102
|
-
hash.keys.inject('') do |query_string, key|
|
103
|
-
query_string << '&' unless key == hash.keys.first
|
104
|
-
query_string << "#{URI.encode(key.to_s)}=#{URI.encode(hash[key].to_s)}"
|
105
|
-
end
|
106
|
-
end
|
107
102
|
end
|
103
|
+
|
108
104
|
end
|
data/lib/gabba/version.rb
CHANGED
data/spec/gabba_spec.rb
CHANGED
@@ -5,6 +5,9 @@ describe Gabba::Gabba do
|
|
5
5
|
describe "when tracking page views" do
|
6
6
|
before do
|
7
7
|
@gabba = Gabba::Gabba.new("abc", "123")
|
8
|
+
@gabba.utmn = "1009731272"
|
9
|
+
@gabba.utmcc = ''
|
10
|
+
stub_analytics @gabba.page_view_params("title", "/page/path", "6783939397")
|
8
11
|
end
|
9
12
|
|
10
13
|
it "must require GA account" do
|
@@ -19,14 +22,17 @@ describe Gabba::Gabba do
|
|
19
22
|
@gabba.page_view_params("hiya", "/tracker/page")[:utmdt].must_equal("hiya")
|
20
23
|
end
|
21
24
|
|
22
|
-
it "must
|
23
|
-
@gabba.
|
25
|
+
it "must do page view request to google" do
|
26
|
+
@gabba.page_view("title", "/page/path", "6783939397").code.must_equal("200")
|
24
27
|
end
|
25
28
|
end
|
26
29
|
|
27
30
|
describe "when tracking custom events" do
|
28
31
|
before do
|
29
32
|
@gabba = Gabba::Gabba.new("abc", "123")
|
33
|
+
@gabba.utmn = "1009731272"
|
34
|
+
@gabba.utmcc = ''
|
35
|
+
stub_analytics @gabba.event_params("cat1", "act1", "lab1", "val1", "6783939397")
|
30
36
|
end
|
31
37
|
|
32
38
|
it "must require GA account" do
|
@@ -41,6 +47,14 @@ describe Gabba::Gabba do
|
|
41
47
|
@gabba.event_data("cat1", "act1", "lab1", "val1").wont_be_nil
|
42
48
|
end
|
43
49
|
|
50
|
+
it "must do event request to google" do
|
51
|
+
@gabba.event("cat1", "act1", "lab1", "val1", "6783939397").code.must_equal("200")
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
def stub_analytics(expected_params)
|
57
|
+
s = stub_request(:get, /www.google-analytics.com\/__utm.gif\?utmac=#{expected_params[:utmac]}&.*/).
|
58
|
+
to_return(:status => 200, :body => "", :headers => {})
|
44
59
|
end
|
45
|
-
|
46
60
|
end
|
data/spec/spec_helper.rb
CHANGED
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:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
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: 2010-
|
18
|
+
date: 2010-12-18 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|