Skip to content

Commit

Permalink
cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Reinsch committed Dec 13, 2010
1 parent 9931696 commit 641dfda
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
3 changes: 2 additions & 1 deletion lib/gabba.rb
@@ -1 +1,2 @@
require File.dirname(__FILE__) + '/gabba/gabba.rb'
require 'gabba/version'
require 'gabba/gabba'
48 changes: 21 additions & 27 deletions lib/gabba/gabba.rb
Expand Up @@ -2,48 +2,45 @@
require "uri"
require "net/http"
require 'cgi'
require File.dirname(__FILE__) + '/version'

module Gabba

class NoGoogleAnalyticsAccountError < RuntimeError; end
class NoGoogleAnalyticsDomainError < RuntimeError; end
class GoogleAnalyticsNetworkError < RuntimeError; end

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

attr_accessor :utmwv, :utmn, :utmhn, :utmcs, :utmul, :utmdt, :utmp, :utmac, :utmt, :utmcc, :user_agent

def initialize(ga_acct, domain, agent = Gabba::USER_AGENT)
@utmwv = "4.4sh" # GA version
@utmcs = "UTF-8" # charset
@utmul = "en-us" # language

@utmn = rand(8999999999) + 1000000000
@utmhid = rand(8999999999) + 1000000000

@utmac = ga_acct
@utmhn = domain
@user_agent = agent
end

def page_view(title, page, utmhid = rand(8999999999) + 1000000000)
check_account_params
hey(page_view_params(title, page, utmhid))
end

def event(category, action, label = nil, value = nil)
check_account_params
hey(event_params(category, action, label, value))
end

def page_view_params(title, page, utmhid = rand(8999999999) + 1000000000)
{
:utmwv => @utmwv,
{ :utmwv => @utmwv,
:utmn => @utmn,
:utmhn => @utmhn,
:utmcs => @utmcs,
Expand All @@ -52,13 +49,11 @@ def page_view_params(title, page, utmhid = rand(8999999999) + 1000000000)
:utmhid => utmhid,
:utmp => page,
:utmac => @utmac,
:utmcc => cookie_params
}
:utmcc => cookie_params }
end

def event_params(category, action, label = nil, value = nil, utmhid = rand(8999999999) + 1000000000)
{
:utmwv => @utmwv,
{ :utmwv => @utmwv,
:utmn => @utmn,
:utmhn => @utmhn,
:utmt => 'event',
Expand All @@ -67,20 +62,19 @@ def event_params(category, action, label = nil, value = nil, utmhid = rand(89999
:utmul => @utmul,
:utmhid => utmhid,
:utmac => @utmac,
:utmcc => cookie_params
}
:utmcc => cookie_params }
end

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
end

# 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)
"__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

# sanity check that we have needed params to even call GA
def check_account_params
raise NoGoogleAnalyticsAccountError unless @utmac
Expand All @@ -96,13 +90,13 @@ def hey(params, referer = "-")
end
raise GoogleAnalyticsNetworkError unless res.code == "200"
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
14 changes: 7 additions & 7 deletions spec/gabba_spec.rb
@@ -1,20 +1,20 @@
require File.dirname(__FILE__) + '/spec_helper'

describe Gabba::Gabba do

describe "when tracking page views" do
before do
@gabba = Gabba::Gabba.new("abc", "123")
end

it "must require GA account" do
lambda {Gabba::Gabba.new(nil, nil).page_view("thing", "thing")}.must_raise(Gabba::NoGoogleAnalyticsAccountError)
end

it "must require GA domain" do
lambda {Gabba::Gabba.new("abs", nil).page_view("thing", "thing")}.must_raise(Gabba::NoGoogleAnalyticsDomainError)
end

it "must be able to create page_view_params" do
@gabba.page_view_params("hiya", "/tracker/page")[:utmdt].must_equal("hiya")
end
Expand All @@ -28,19 +28,19 @@
before do
@gabba = Gabba::Gabba.new("abc", "123")
end

it "must require GA account" do
lambda {Gabba::Gabba.new(nil, nil).event("cat1", "act1", "lab1", "val1")}.must_raise(Gabba::NoGoogleAnalyticsAccountError)
end

it "must require GA domain" do
lambda {Gabba::Gabba.new("abs", nil).event("cat1", "act1", "lab1", "val1")}.must_raise(Gabba::NoGoogleAnalyticsDomainError)
end

it "must be able to create event data" do
@gabba.event_data("cat1", "act1", "lab1", "val1").wont_be_nil
end

end

end

0 comments on commit 641dfda

Please sign in to comment.