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 CHANGED
@@ -3,4 +3,5 @@ source "http://rubygems.org"
3
3
  # Specify your gem's dependencies in gabba.gemspec
4
4
  gemspec
5
5
 
6
- gem 'minitest'
6
+ gem 'minitest'
7
+ gem 'webmock'
@@ -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
@@ -11,9 +11,8 @@ module Gabba
11
11
  class GoogleAnalyticsNetworkError < RuntimeError; end
12
12
 
13
13
  class Gabba
14
- GOOGLE_URL = "http://www.google-analytics.com"
15
- TRACKING_URL = "/ga.js"
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, referer = "-")
92
- uri = URI.parse("#{GOOGLE_URL}#{BEACON_URL}?#{hash_to_querystring(params)}")
93
- req = Net::HTTP::Get.new(uri.path, {"User-Agent" => URI.escape(user_agent)})
94
- res = Net::HTTP.start(uri.host, uri.port) do |http|
95
- http.request(req)
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
- raise GoogleAnalyticsNetworkError unless res.code == "200"
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
@@ -1,3 +1,3 @@
1
1
  module Gabba
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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 be able to create hash of page_view_params" do
23
- @gabba.hash_to_querystring(@gabba.page_view_params("hiya", "/tracker/page")).wont_be_nil
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
@@ -3,4 +3,7 @@ gem 'minitest'
3
3
  require 'minitest/autorun'
4
4
  require 'minitest/pride'
5
5
 
6
+ require 'webmock'
7
+ include WebMock::API
8
+
6
9
  require File.dirname(__FILE__) + '/../lib/gabba/gabba'
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: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
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-11-28 00:00:00 -08:00
18
+ date: 2010-12-18 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21