ga_cookie_parser 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/Gemfile.lock +1 -1
- data/README.rdoc +54 -32
- data/ga_cookie_parser.gemspec +1 -1
- data/lib/ga_cookie_parser.rb +13 -1
- data/lib/ga_cookie_parser/version.rb +1 -1
- data/test/ga_cookie_parser_test.rb +30 -9
- metadata +41 -51
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -1,44 +1,58 @@
|
|
1
1
|
= Google Analytics Cookie Parser
|
2
2
|
|
3
|
-
Simple ruby library for parsing the __umtz and __umta cookies which Google Analytics uses to track referrers and visitors.
|
3
|
+
Simple ruby library for parsing the __umtz, __umtb and __umta cookies which Google Analytics uses to track referrers and visitors.
|
4
4
|
|
5
5
|
You may wish to use this library to integrate data from Google Analytics into your internal CRM or sales tracking application.
|
6
6
|
|
7
|
+
== Installation
|
8
|
+
|
9
|
+
The library is available as a gem.
|
10
|
+
|
11
|
+
gem install ga_cookie_parser
|
12
|
+
|
7
13
|
== Usage
|
8
14
|
|
9
15
|
Create an instance, or use the convenience method GaCookieParser.parse, providing a hash with keys :umtz and/or :umtz as argument.
|
10
16
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
Then, @data provides #utmz_hash and #utma_hash methods which return hashes as follows:
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
17
|
+
utmz = cookies["__utmz"] # in a rails controller context
|
18
|
+
utmb = cookies["__utmb"]
|
19
|
+
utma = cookies["__utma"]
|
20
|
+
@data = GaCookieParser::GaCookieParser.new(:utmz => utmz, :utmb => utmb, :utma => utma)
|
21
|
+
|
22
|
+
Then, @data provides #utmz_hash, #utmb_hash and #utma_hash methods which return hashes as follows:
|
23
|
+
|
24
|
+
# @data.utmz_hash
|
25
|
+
# note that not all of these will be present, and some are mutually exclusive
|
26
|
+
{
|
27
|
+
:domain_hash => '12979384',
|
28
|
+
:timestamp => '1294887021', # unix timestamp
|
29
|
+
:session_counter => '1',
|
30
|
+
:campaign_number => '1',
|
31
|
+
:utmcsr => 'google', # source
|
32
|
+
:utmccn => '(organic)', # campaign name
|
33
|
+
:utmcmd => 'organic', # medium
|
34
|
+
:utmctr => 'search term', # term(s)
|
35
|
+
:utmcct => '/ref.php, # content (referring page in case of referrals)
|
36
|
+
:utmgclid => 'CI7wh8C6tKYCFU2DpAod7z97IQ' # gclid, linking referral back to adwords
|
37
|
+
}
|
38
|
+
|
39
|
+
# @data.utma_hash
|
40
|
+
{
|
41
|
+
:domain_hash => '12979384',
|
42
|
+
:visitor_id => '1392679796', # a random number
|
43
|
+
:initial_visit_at => '1289844797', # unix timestamp
|
44
|
+
:previous_visit_at => '1294798990', # unix timestamp
|
45
|
+
:current_visit_at => '1294866800', # unix timestamp
|
46
|
+
:session_counter => '1'
|
47
|
+
}
|
48
|
+
|
49
|
+
# @data.utmb_hash
|
50
|
+
{
|
51
|
+
:domain_hash => '12979384',
|
52
|
+
:pageview => '3', # pageview count in this 30 min session
|
53
|
+
:outbound_click => '10', # number of outbound links clicked counting down from 10
|
54
|
+
:timestamp => '1294866800' # current timestamp
|
55
|
+
}
|
42
56
|
|
43
57
|
== Notes
|
44
58
|
|
@@ -49,6 +63,14 @@ Documentation on these cookies is somewhat sparse. Here are the main resources u
|
|
49
63
|
* {UPDATED: Integrating Google Analytics with a CRM}[http://cutroni.com/blog/2009/03/18/updated-integrating-google-analytics-with-a-crm/] blog post by Justin Cutroni
|
50
64
|
* {GA Basics: The Structure of Cookie Values}[http://blog.vkistudios.com/index.cfm/2010/8/31/GA-Basics-The-Structure-of-Cookie-Values] blog post by VKI Studios
|
51
65
|
* {Python Google Analytics Cookie Parser}[https://github.com/RyOnLife/Python-Google-Analytics-Cookie-Parser] (from which I borrowed some variable names)
|
66
|
+
* {Google Analytics Cookies (utma, utmb, utmz)}[http://www.webtrafficexchange.com/google-analytics-cookies-utma-utmb-utmz] (includes utmb structure)
|
67
|
+
|
68
|
+
=== UTMB
|
69
|
+
|
70
|
+
Was harder to find documentation on this (thanks to dominikform for the PR). The final link above contains the following details, copied here in case they disappear:
|
71
|
+
|
72
|
+
The content of the cookie include domain hash, pageview, number of outbound link click
|
73
|
+
counting down from 10, and current time stamp
|
52
74
|
|
53
75
|
== Copyright
|
54
76
|
|
data/ga_cookie_parser.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.email = ["shrobson@gmail.com"]
|
11
11
|
s.homepage = ""
|
12
12
|
s.summary = %q{Google Analytics Cookie Parsing Gem}
|
13
|
-
s.description = %q{Gem to parse the utmz and utma tracking coockies used by Google Analytics.}
|
13
|
+
s.description = %q{Gem to parse the utmz, utmb and utma tracking coockies used by Google Analytics.}
|
14
14
|
|
15
15
|
s.rubyforge_project = "ga_cookie_parser"
|
16
16
|
|
data/lib/ga_cookie_parser.rb
CHANGED
@@ -7,10 +7,11 @@ module GaCookieParser
|
|
7
7
|
|
8
8
|
class GaCookieParser
|
9
9
|
|
10
|
-
attr_reader :utmz, :utma, :utmz_hash, :utma_hash
|
10
|
+
attr_reader :utmz, :utmb, :utma, :utmz_hash, :utmb_hash, :utma_hash
|
11
11
|
|
12
12
|
def initialize(cookies = {})
|
13
13
|
@utmz = cookies[:utmz]
|
14
|
+
@utmb = cookies[:utmb]
|
14
15
|
@utma = cookies[:utma]
|
15
16
|
parse_cookies
|
16
17
|
end
|
@@ -19,12 +20,17 @@ module GaCookieParser
|
|
19
20
|
!(@utmz.nil? || @utmz.empty?)
|
20
21
|
end
|
21
22
|
|
23
|
+
def utmb?
|
24
|
+
!(@utmb.nil? || @utmb.empty?)
|
25
|
+
end
|
26
|
+
|
22
27
|
def utma?
|
23
28
|
!(@utma.nil? || @utma.empty?)
|
24
29
|
end
|
25
30
|
|
26
31
|
def parse_cookies
|
27
32
|
parse_utmz
|
33
|
+
parse_utmb
|
28
34
|
parse_utma
|
29
35
|
end
|
30
36
|
|
@@ -45,6 +51,12 @@ module GaCookieParser
|
|
45
51
|
|
46
52
|
end
|
47
53
|
|
54
|
+
def parse_utmb
|
55
|
+
return if (@utmb.nil? || @utmb.empty?)
|
56
|
+
@utmb_hash = h = {}
|
57
|
+
h[:domain_hash], h[:pageview], h[:outbound_click], h[:timestamp] = @utmb.split(".")
|
58
|
+
end
|
59
|
+
|
48
60
|
def parse_utma
|
49
61
|
return if (@utma.nil? || @utma.empty?)
|
50
62
|
@utma_hash = h = {}
|
@@ -5,6 +5,7 @@ class GaCookieParserTest < Test::Unit::TestCase
|
|
5
5
|
|
6
6
|
def setup
|
7
7
|
@utmz = "12979384.1294887021.1.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=test terms"
|
8
|
+
@utmb = "12979384.2.10.1294866800"
|
8
9
|
@utma = "12979384.1392679796.1289844797.1294798990.1294866800.4"
|
9
10
|
end
|
10
11
|
|
@@ -14,25 +15,28 @@ class GaCookieParserTest < Test::Unit::TestCase
|
|
14
15
|
@result = GaCookieParser.new
|
15
16
|
end
|
16
17
|
|
17
|
-
should "result in nil values for utmz and utma" do
|
18
|
+
should "result in nil values for utmz, utmb and utma" do
|
18
19
|
assert_nil @result.utma
|
20
|
+
assert_nil @result.utmb
|
19
21
|
assert_nil @result.utmz
|
20
22
|
end
|
21
23
|
end
|
22
24
|
|
23
25
|
context "creating a parser with a hash of raw cookie values" do
|
24
26
|
setup do
|
25
|
-
@result = GaCookieParser.new(:utmz => @utmz, :utma => @utma)
|
27
|
+
@result = GaCookieParser.new(:utmz => @utmz, :utmb => @utmb, :utma => @utma)
|
26
28
|
end
|
27
29
|
|
28
|
-
should "provide access to the raw values through utmz and utma" do
|
30
|
+
should "provide access to the raw values through utmz, utmb and utma" do
|
29
31
|
assert !@result.utmz.nil?
|
30
32
|
assert_equal @utma, @result.utma
|
33
|
+
assert_equal @utmb, @result.utmb
|
31
34
|
assert_equal @utmz, @result.utmz
|
32
35
|
end
|
33
36
|
|
34
|
-
should "provide access to the parsed cookie hashes through utmz_hash and utma_hash" do
|
37
|
+
should "provide access to the parsed cookie hashes through utmz_hash, utmb_hash and utma_hash" do
|
35
38
|
assert_not_nil @result.utmz_hash
|
39
|
+
assert_not_nil @result.utmb_hash
|
36
40
|
assert_not_nil @result.utma_hash
|
37
41
|
end
|
38
42
|
|
@@ -50,6 +54,14 @@ class GaCookieParserTest < Test::Unit::TestCase
|
|
50
54
|
assert_nil @p[:utmgclid]
|
51
55
|
end
|
52
56
|
|
57
|
+
should "accurately parse the utmb cookie" do
|
58
|
+
@p = @result.utmb_hash
|
59
|
+
assert_equal '12979384', @p[:domain_hash]
|
60
|
+
assert_equal '2', @p[:pageview]
|
61
|
+
assert_equal '10', @p[:outbound_click]
|
62
|
+
assert_equal '1294866800', @p[:timestamp]
|
63
|
+
end
|
64
|
+
|
53
65
|
should "accurately parse the utma cookie" do
|
54
66
|
@p = @result.utma_hash
|
55
67
|
assert_equal '12979384', @p[:domain_hash]
|
@@ -64,21 +76,29 @@ class GaCookieParserTest < Test::Unit::TestCase
|
|
64
76
|
|
65
77
|
context "creating a parser with some cookie values missing" do
|
66
78
|
setup do
|
67
|
-
@missing_utmz = GaCookieParser.new(:utma => @utma)
|
68
|
-
@missing_utma = GaCookieParser.new(:utmz => @utmz)
|
79
|
+
@missing_utmz = GaCookieParser.new(:utma => @utma, :utmb => @utmb)
|
80
|
+
@missing_utma = GaCookieParser.new(:utmb => @utmb, :utmz => @utmz)
|
81
|
+
@missing_utmb = GaCookieParser.new(:utma => @utma, :utmz => @utmz)
|
69
82
|
end
|
70
83
|
|
71
84
|
should "return nil for the parsed hashes of those cookies" do
|
72
85
|
assert_nil @missing_utmz.utmz_hash
|
86
|
+
assert_nil @missing_utmb.utmb_hash
|
73
87
|
assert_nil @missing_utma.utma_hash
|
74
88
|
end
|
75
89
|
|
76
90
|
should "indicate the values are not present" do
|
77
91
|
assert !@missing_utmz.utmz?
|
78
|
-
assert
|
92
|
+
assert @missing_utmz.utma?
|
93
|
+
assert @missing_utmz.utmb?
|
79
94
|
|
80
95
|
assert !@missing_utma.utma?
|
81
96
|
assert @missing_utma.utmz?
|
97
|
+
assert @missing_utma.utmb?
|
98
|
+
|
99
|
+
assert !@missing_utmb.utmb?
|
100
|
+
assert @missing_utmb.utmz?
|
101
|
+
assert @missing_utmb.utma?
|
82
102
|
end
|
83
103
|
end
|
84
104
|
|
@@ -105,12 +125,13 @@ class GaCookieParserTest < Test::Unit::TestCase
|
|
105
125
|
|
106
126
|
context "calling the parse method on the module" do
|
107
127
|
setup do
|
108
|
-
@result = ::GaCookieParser.parse(:utmz => @utmz, :utma => @utma)
|
128
|
+
@result = ::GaCookieParser.parse(:utmz => @utmz, :utmb => @utmb, :utma => @utma)
|
109
129
|
end
|
110
130
|
|
111
131
|
should "return a GaCookieParser instance correctly initialized" do
|
112
132
|
assert @result.is_a?(::GaCookieParser::GaCookieParser)
|
113
133
|
assert_not_nil @result.utmz_hash
|
134
|
+
assert_not_nil @result.utmb_hash
|
114
135
|
assert_not_nil @result.utma_hash
|
115
136
|
end
|
116
137
|
|
@@ -118,7 +139,7 @@ class GaCookieParserTest < Test::Unit::TestCase
|
|
118
139
|
|
119
140
|
context "parsing a bad cookie" do
|
120
141
|
setup do
|
121
|
-
@result = GaCookieParser.new(:utmz => "123XXX", :utma => "\"jk.,./l;o.mnhhlk")
|
142
|
+
@result = GaCookieParser.new(:utmz => "123XXX", :utmb => "lkvlveef", :utma => "\"jk.,./l;o.mnhhlk")
|
122
143
|
end
|
123
144
|
|
124
145
|
should "not throw an error" do
|
metadata
CHANGED
@@ -1,44 +1,40 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: ga_cookie_parser
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 1
|
8
|
-
- 0
|
9
|
-
version: 0.1.0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Simon Robson
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2013-05-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: shoulda
|
22
|
-
|
23
|
-
|
24
|
-
requirements:
|
25
|
-
- -
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
|
28
|
-
- 0
|
29
|
-
version: "0"
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
30
22
|
type: :development
|
31
|
-
|
32
|
-
|
33
|
-
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Gem to parse the utmz, utmb and utma tracking coockies used by Google
|
31
|
+
Analytics.
|
32
|
+
email:
|
34
33
|
- shrobson@gmail.com
|
35
34
|
executables: []
|
36
|
-
|
37
35
|
extensions: []
|
38
|
-
|
39
36
|
extra_rdoc_files: []
|
40
|
-
|
41
|
-
files:
|
37
|
+
files:
|
42
38
|
- .gitignore
|
43
39
|
- Gemfile
|
44
40
|
- Gemfile.lock
|
@@ -50,36 +46,30 @@ files:
|
|
50
46
|
- lib/ga_cookie_parser/version.rb
|
51
47
|
- test/ga_cookie_parser_test.rb
|
52
48
|
- test/test_helper.rb
|
53
|
-
|
54
|
-
homepage: ""
|
49
|
+
homepage: ''
|
55
50
|
licenses: []
|
56
|
-
|
57
51
|
post_install_message:
|
58
52
|
rdoc_options: []
|
59
|
-
|
60
|
-
require_paths:
|
53
|
+
require_paths:
|
61
54
|
- lib
|
62
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
requirements:
|
71
|
-
- -
|
72
|
-
- !ruby/object:Gem::Version
|
73
|
-
|
74
|
-
- 0
|
75
|
-
version: "0"
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
76
67
|
requirements: []
|
77
|
-
|
78
68
|
rubyforge_project: ga_cookie_parser
|
79
|
-
rubygems_version: 1.
|
69
|
+
rubygems_version: 1.8.25
|
80
70
|
signing_key:
|
81
71
|
specification_version: 3
|
82
72
|
summary: Google Analytics Cookie Parsing Gem
|
83
|
-
test_files:
|
73
|
+
test_files:
|
84
74
|
- test/ga_cookie_parser_test.rb
|
85
75
|
- test/test_helper.rb
|