gabba 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +16 -0
- data/README +12 -0
- data/Rakefile +8 -0
- data/gabba.gemspec +21 -0
- data/lib/gabba.rb +1 -0
- data/lib/gabba/gabba.rb +108 -0
- data/lib/gabba/version.rb +3 -0
- data/spec/gabba_spec.rb +46 -0
- data/spec/spec_helper.rb +6 -0
- metadata +79 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
== Simple class to send custom server-side events to Google Analytics
|
2
|
+
|
3
|
+
Heavily influenced by the http://code.google.com/p/serversidegoogleanalytics
|
4
|
+
|
5
|
+
HOW TO USE:
|
6
|
+
|
7
|
+
- Track page views
|
8
|
+
Gabba::Gabba.new("UT-1234", "mydomain.com").page_view("something", "track/me")
|
9
|
+
|
10
|
+
- Track custom events
|
11
|
+
Gabba::Gabba.new("UT-1234", "mydomain.com").event("Videos", "Play", "ID", "123")
|
12
|
+
|
data/Rakefile
ADDED
data/gabba.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "gabba/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "gabba"
|
7
|
+
s.version = Gabba::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Ron Evans"]
|
10
|
+
s.email = ["ron dot evans at gmail dot com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Easy server-side tracking for Google Analytics}
|
13
|
+
s.description = %q{Easy server-side tracking for Google Analytics}
|
14
|
+
|
15
|
+
s.rubyforge_project = "gabba"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
data/lib/gabba.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/gabba/gabba.rb'
|
data/lib/gabba/gabba.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
# yo, easy server-side tracking for Google Analytics... hey!
|
2
|
+
require "uri"
|
3
|
+
require "net/http"
|
4
|
+
require 'cgi'
|
5
|
+
require File.dirname(__FILE__) + '/version'
|
6
|
+
|
7
|
+
module Gabba
|
8
|
+
|
9
|
+
class NoGoogleAnalyticsAccountError < RuntimeError; end
|
10
|
+
class NoGoogleAnalyticsDomainError < RuntimeError; end
|
11
|
+
class GoogleAnalyticsNetworkError < RuntimeError; end
|
12
|
+
|
13
|
+
class Gabba
|
14
|
+
GOOGLE_URL = "http://www.google-analytics.com"
|
15
|
+
TRACKING_URL = "/ga.js"
|
16
|
+
BEACON_URL = "/__utm.gif"
|
17
|
+
USER_AGENT = "Gabba #{VERSION} Agent"
|
18
|
+
|
19
|
+
attr_accessor :utmwv, :utmn, :utmhn, :utmcs, :utmul, :utmdt, :utmp, :utmac, :utmt, :utmcc, :user_agent
|
20
|
+
|
21
|
+
def initialize(ga_acct, domain, agent = Gabba::USER_AGENT)
|
22
|
+
@utmwv = "4.4sh" # GA version
|
23
|
+
@utmcs = "UTF-8" # charset
|
24
|
+
@utmul = "en-us" # language
|
25
|
+
|
26
|
+
@utmn = rand(8999999999) + 1000000000
|
27
|
+
@utmhid = rand(8999999999) + 1000000000
|
28
|
+
|
29
|
+
@utmac = ga_acct
|
30
|
+
@utmhn = domain
|
31
|
+
@user_agent = agent
|
32
|
+
end
|
33
|
+
|
34
|
+
def page_view(title, page, utmhid = rand(8999999999) + 1000000000)
|
35
|
+
check_account_params
|
36
|
+
hey(page_view_params(title, page, utmhid))
|
37
|
+
end
|
38
|
+
|
39
|
+
def event(category, action, label = nil, value = nil)
|
40
|
+
check_account_params
|
41
|
+
hey(event_params(category, action, label, value))
|
42
|
+
end
|
43
|
+
|
44
|
+
def page_view_params(title, page, utmhid = rand(8999999999) + 1000000000)
|
45
|
+
{
|
46
|
+
:utmwv => @utmwv,
|
47
|
+
:utmn => @utmn,
|
48
|
+
:utmhn => @utmhn,
|
49
|
+
:utmcs => @utmcs,
|
50
|
+
:utmul => @utmul,
|
51
|
+
:utmdt => title,
|
52
|
+
:utmhid => utmhid,
|
53
|
+
:utmp => page,
|
54
|
+
:utmac => @utmac,
|
55
|
+
:utmcc => cookie_params
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
def event_params(category, action, label = nil, value = nil, utmhid = rand(8999999999) + 1000000000)
|
60
|
+
{
|
61
|
+
:utmwv => @utmwv,
|
62
|
+
:utmn => @utmn,
|
63
|
+
:utmhn => @utmhn,
|
64
|
+
:utmt => 'event',
|
65
|
+
:utme => event_data(category, action, label, value),
|
66
|
+
:utmcs => @utmcs,
|
67
|
+
:utmul => @utmul,
|
68
|
+
:utmhid => utmhid,
|
69
|
+
:utmac => @utmac,
|
70
|
+
:utmcc => cookie_params
|
71
|
+
}
|
72
|
+
end
|
73
|
+
|
74
|
+
def event_data(category, action, label = nil, value = nil)
|
75
|
+
data = "5(#{category}*action" + (label ? "*#{label})" : ")")
|
76
|
+
data += "(#{value})" if value
|
77
|
+
end
|
78
|
+
|
79
|
+
# 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)
|
81
|
+
"__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
|
+
end
|
83
|
+
|
84
|
+
# sanity check that we have needed params to even call GA
|
85
|
+
def check_account_params
|
86
|
+
raise NoGoogleAnalyticsAccountError unless @utmac
|
87
|
+
raise NoGoogleAnalyticsDomainError unless @utmhn
|
88
|
+
end
|
89
|
+
|
90
|
+
# 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)
|
96
|
+
end
|
97
|
+
raise GoogleAnalyticsNetworkError unless res.code == "200"
|
98
|
+
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
|
+
end
|
108
|
+
end
|
data/spec/gabba_spec.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe Gabba::Gabba do
|
4
|
+
|
5
|
+
describe "when tracking page views" do
|
6
|
+
before do
|
7
|
+
@gabba = Gabba::Gabba.new("abc", "123")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "must require GA account" do
|
11
|
+
lambda {Gabba::Gabba.new(nil, nil).page_view("thing", "thing")}.must_raise(Gabba::NoGoogleAnalyticsAccountError)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "must require GA domain" do
|
15
|
+
lambda {Gabba::Gabba.new("abs", nil).page_view("thing", "thing")}.must_raise(Gabba::NoGoogleAnalyticsDomainError)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "must be able to create page_view_params" do
|
19
|
+
@gabba.page_view_params("hiya", "/tracker/page")[:utmdt].must_equal("hiya")
|
20
|
+
end
|
21
|
+
|
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
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "when tracking custom events" do
|
28
|
+
before do
|
29
|
+
@gabba = Gabba::Gabba.new("abc", "123")
|
30
|
+
end
|
31
|
+
|
32
|
+
it "must require GA account" do
|
33
|
+
lambda {Gabba::Gabba.new(nil, nil).event("cat1", "act1", "lab1", "val1")}.must_raise(Gabba::NoGoogleAnalyticsAccountError)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "must require GA domain" do
|
37
|
+
lambda {Gabba::Gabba.new("abs", nil).event("cat1", "act1", "lab1", "val1")}.must_raise(Gabba::NoGoogleAnalyticsDomainError)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "must be able to create event data" do
|
41
|
+
@gabba.event_data("cat1", "act1", "lab1", "val1").wont_be_nil
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gabba
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ron Evans
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-11-28 00:00:00 -08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Easy server-side tracking for Google Analytics
|
23
|
+
email:
|
24
|
+
- ron dot evans at gmail dot com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- Gemfile.lock
|
35
|
+
- README
|
36
|
+
- Rakefile
|
37
|
+
- gabba.gemspec
|
38
|
+
- lib/gabba.rb
|
39
|
+
- lib/gabba/gabba.rb
|
40
|
+
- lib/gabba/version.rb
|
41
|
+
- spec/gabba_spec.rb
|
42
|
+
- spec/spec_helper.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: ""
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
hash: 3
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project: gabba
|
73
|
+
rubygems_version: 1.3.7
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Easy server-side tracking for Google Analytics
|
77
|
+
test_files:
|
78
|
+
- spec/gabba_spec.rb
|
79
|
+
- spec/spec_helper.rb
|