Skip to content

Commit

Permalink
Add webmock based specs, refactoring, and update to version 0.0.2 tha…
Browse files Browse the repository at this point in the history
…nks to mreinsch for some suggestions
  • Loading branch information
deadprogram committed Dec 18, 2010
1 parent 9931696 commit 19328bb
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 29 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ source "http://rubygems.org"
# Specify your gem's dependencies in gabba.gemspec # Specify your gem's dependencies in gabba.gemspec
gemspec gemspec


gem 'minitest' gem 'minitest'
gem 'webmock'
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ PATH
GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
specs: specs:
addressable (2.2.2)
crack (0.1.8)
minitest (2.0.0) minitest (2.0.0)
webmock (1.6.1)
addressable (>= 2.2.2)
crack (>= 0.1.7)


PLATFORMS PLATFORMS
ruby ruby


DEPENDENCIES DEPENDENCIES
gabba! gabba!
minitest minitest
webmock
44 changes: 20 additions & 24 deletions lib/gabba/gabba.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ class NoGoogleAnalyticsDomainError < RuntimeError; end
class GoogleAnalyticsNetworkError < RuntimeError; end class GoogleAnalyticsNetworkError < RuntimeError; end


class Gabba class Gabba
GOOGLE_URL = "http://www.google-analytics.com" GOOGLE_HOST = "www.google-analytics.com"
TRACKING_URL = "/ga.js" BEACON_PATH = "/__utm.gif"
BEACON_URL = "/__utm.gif"
USER_AGENT = "Gabba #{VERSION} Agent" USER_AGENT = "Gabba #{VERSION} Agent"


attr_accessor :utmwv, :utmn, :utmhn, :utmcs, :utmul, :utmdt, :utmp, :utmac, :utmt, :utmcc, :user_agent attr_accessor :utmwv, :utmn, :utmhn, :utmcs, :utmul, :utmdt, :utmp, :utmac, :utmt, :utmcc, :user_agent
Expand All @@ -36,9 +35,9 @@ def page_view(title, page, utmhid = rand(8999999999) + 1000000000)
hey(page_view_params(title, page, utmhid)) hey(page_view_params(title, page, utmhid))
end end


def event(category, action, label = nil, value = nil) def event(category, action, label = nil, value = nil, utmhid = rand(8999999999) + 1000000000)
check_account_params check_account_params
hey(event_params(category, action, label, value)) hey(event_params(category, action, label, value, utmhid))
end end


def page_view_params(title, page, utmhid = rand(8999999999) + 1000000000) def page_view_params(title, page, utmhid = rand(8999999999) + 1000000000)
Expand All @@ -52,7 +51,7 @@ def page_view_params(title, page, utmhid = rand(8999999999) + 1000000000)
:utmhid => utmhid, :utmhid => utmhid,
:utmp => page, :utmp => page,
:utmac => @utmac, :utmac => @utmac,
:utmcc => cookie_params :utmcc => @utmcc || cookie_params
} }
end end


Expand All @@ -67,42 +66,39 @@ def event_params(category, action, label = nil, value = nil, utmhid = rand(89999
:utmul => @utmul, :utmul => @utmul,
:utmhid => utmhid, :utmhid => utmhid,
:utmac => @utmac, :utmac => @utmac,
:utmcc => cookie_params :utmcc => @utmcc || cookie_params
} }
end end

def event_data(category, action, label = nil, value = nil) def event_data(category, action, label = nil, value = nil)
data = "5(#{category}*action" + (label ? "*#{label})" : ")") data = "5(#{category}*action" + (label ? "*#{label})" : ")")
data += "(#{value})" if value data += "(#{value})" if value
end end


# create magical cookie params used by GA for its own nefarious purposes # create magical cookie params used by GA for its own nefarious purposes
def cookie_params(utma1 = rand(89999999) + 10000000, utma2 = rand(1147483647) + 1000000000, today = Time.now) def cookie_params(utma1 = rand(89999999) + 10000000, utma2 = rand(1147483647) + 1000000000, today = Time.now)
"__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);" "__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);"
end end

# sanity check that we have needed params to even call GA # sanity check that we have needed params to even call GA
def check_account_params def check_account_params
raise NoGoogleAnalyticsAccountError unless @utmac raise NoGoogleAnalyticsAccountError unless @utmac
raise NoGoogleAnalyticsDomainError unless @utmhn raise NoGoogleAnalyticsDomainError unless @utmhn
end end


# makes the tracking call to Google Analytics # makes the tracking call to Google Analytics
def hey(params, referer = "-") def hey(params)
uri = URI.parse("#{GOOGLE_URL}#{BEACON_URL}?#{hash_to_querystring(params)}") query = params.map {|k,v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
req = Net::HTTP::Get.new(uri.path, {"User-Agent" => URI.escape(user_agent)}) response = Net::HTTP.start(GOOGLE_HOST) do |http|
res = Net::HTTP.start(uri.host, uri.port) do |http| request = Net::HTTP::Get.new("#{BEACON_PATH}?#{query}")
http.request(req) request["User-Agent"] = URI.escape(user_agent)
request["Accept"] = "*/*"
http.request(request)
end end
raise GoogleAnalyticsNetworkError unless res.code == "200"
raise GoogleAnalyticsNetworkError unless response.code == "200"
response
end end

# convert params hash to query string
def hash_to_querystring(hash = {})
hash.keys.inject('') do |query_string, key|
query_string << '&' unless key == hash.keys.first
query_string << "#{URI.encode(key.to_s)}=#{URI.encode(hash[key].to_s)}"
end
end
end end

end end
2 changes: 1 addition & 1 deletion lib/gabba/version.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,3 @@
module Gabba module Gabba
VERSION = "0.0.1" VERSION = "0.0.2"
end end
20 changes: 17 additions & 3 deletions spec/gabba_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
describe "when tracking page views" do describe "when tracking page views" do
before do before do
@gabba = Gabba::Gabba.new("abc", "123") @gabba = Gabba::Gabba.new("abc", "123")
@gabba.utmn = "1009731272"
@gabba.utmcc = ''
stub_analytics @gabba.page_view_params("title", "/page/path", "6783939397")
end end


it "must require GA account" do it "must require GA account" do
Expand All @@ -19,14 +22,17 @@
@gabba.page_view_params("hiya", "/tracker/page")[:utmdt].must_equal("hiya") @gabba.page_view_params("hiya", "/tracker/page")[:utmdt].must_equal("hiya")
end end


it "must be able to create hash of page_view_params" do it "must do page view request to google" do
@gabba.hash_to_querystring(@gabba.page_view_params("hiya", "/tracker/page")).wont_be_nil @gabba.page_view("title", "/page/path", "6783939397").code.must_equal("200")
end end
end end


describe "when tracking custom events" do describe "when tracking custom events" do
before do before do
@gabba = Gabba::Gabba.new("abc", "123") @gabba = Gabba::Gabba.new("abc", "123")
@gabba.utmn = "1009731272"
@gabba.utmcc = ''
stub_analytics @gabba.event_params("cat1", "act1", "lab1", "val1", "6783939397")
end end


it "must require GA account" do it "must require GA account" do
Expand All @@ -41,6 +47,14 @@
@gabba.event_data("cat1", "act1", "lab1", "val1").wont_be_nil @gabba.event_data("cat1", "act1", "lab1", "val1").wont_be_nil
end end


it "must do event request to google" do
@gabba.event("cat1", "act1", "lab1", "val1", "6783939397").code.must_equal("200")
end

end

def stub_analytics(expected_params)
s = stub_request(:get, /www.google-analytics.com\/__utm.gif\?utmac=#{expected_params[:utmac]}&.*/).
to_return(:status => 200, :body => "", :headers => {})
end end

end end
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
require 'minitest/autorun' require 'minitest/autorun'
require 'minitest/pride' require 'minitest/pride'


require 'webmock'
include WebMock::API

require File.dirname(__FILE__) + '/../lib/gabba/gabba' require File.dirname(__FILE__) + '/../lib/gabba/gabba'

0 comments on commit 19328bb

Please sign in to comment.