ga_cookie_parser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ga_cookie_parser.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,16 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ga_cookie_parser (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ shoulda (2.11.3)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ ga_cookie_parser!
16
+ shoulda
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Simon Robson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,55 @@
1
+ = Google Analytics Cookie Parser
2
+
3
+ Simple ruby library for parsing the __umtz and __umta cookies which Google Analytics uses to track referrers and visitors.
4
+
5
+ You may wish to use this library to integrate data from Google Analytics into your internal CRM or sales tracking application.
6
+
7
+ == Usage
8
+
9
+ Create an instance, or use the convenience method GaCookieParser.parse, providing a hash with keys :umtz and/or :umtz as argument.
10
+
11
+ utmz = cookies["__utmz"] # in a rails controller context
12
+ utma = cookies["__utma"]
13
+
14
+ @data = GaCookieParser::GaCookieParser.new(:utmz => utmz, :utma => utma)
15
+
16
+ Then, @data provides #utmz_hash and #utma_hash methods which return hashes as follows:
17
+
18
+ # @data.utmz_hash
19
+ # note that not all of these will be present, and some are mutually exclusive
20
+ {
21
+ :domain_hash => '12979384',
22
+ :timestamp => '1294887021', # unix timestamp
23
+ :session_counter => '1',
24
+ :campaign_number => '1',
25
+ :utmcsr => 'google', # source
26
+ :utmccn => '(organic)', # campaign name
27
+ :utmcmd => 'organic', # medium
28
+ :utmctr => 'search term', # term(s)
29
+ :utmcct => '/ref.php, # content (referring page in case of referrals)
30
+ :utmgclid => 'CI7wh8C6tKYCFU2DpAod7z97IQ' # gclid, linking referral back to adwords
31
+ }
32
+
33
+ # @data.utma_hash
34
+ {
35
+ :domain_hash => '12979384',
36
+ :visitor_id => '1392679796', # a random number
37
+ :initial_visit_at => '1289844797', # unix timestamp
38
+ :previous_visit_at => '1294798990', # unix timestamp
39
+ :current_visit_at => '1294866800', # unix timestamp
40
+ :session_counter => '1'
41
+ }
42
+
43
+ == Notes
44
+
45
+ Documentation on these cookies is somewhat sparse. Here are the main resources used in putting this library together:
46
+
47
+ * {Google documentation}[http://code.google.com/apis/analytics/docs/concepts/gaConceptsCookies.html] on the Google Analytics cookies
48
+ * {Integrating Google Analytics With a CRM}[http://cutroni.com/blog/2007/10/29/integrating-google-analytics-with-a-crm/] blog post by Justin Cutroni
49
+ * {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
+ * {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
+ * {Python Google Analytics Cookie Parser}[https://github.com/RyOnLife/Python-Google-Analytics-Cookie-Parser] (from which I borrowed some variable names)
52
+
53
+ == Copyright
54
+
55
+ Copyright (c) 2011 Simon Robson. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/*_test.rb'
8
+ test.verbose = true
9
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ga_cookie_parser/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ga_cookie_parser"
7
+ s.version = GaCookieParser::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Simon Robson"]
10
+ s.email = ["shrobson@gmail.com"]
11
+ s.homepage = ""
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.}
14
+
15
+ s.rubyforge_project = "ga_cookie_parser"
16
+
17
+ s.add_development_dependency "shoulda"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ end
@@ -0,0 +1,3 @@
1
+ module GaCookieParser
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,57 @@
1
+ module GaCookieParser
2
+
3
+ def self.parse(*args)
4
+ GaCookieParser.new(*args)
5
+ end
6
+
7
+
8
+ class GaCookieParser
9
+
10
+ attr_reader :utmz, :utma, :utmz_hash, :utma_hash
11
+
12
+ def initialize(cookies = {})
13
+ @utmz = cookies[:utmz]
14
+ @utma = cookies[:utma]
15
+ parse_cookies
16
+ end
17
+
18
+ def utmz?
19
+ !(@utmz.nil? || @utmz.empty?)
20
+ end
21
+
22
+ def utma?
23
+ !(@utma.nil? || @utma.empty?)
24
+ end
25
+
26
+ def parse_cookies
27
+ parse_utmz
28
+ parse_utma
29
+ end
30
+
31
+ def parse_utmz
32
+ return if (@utmz.nil? || @utmz.empty?)
33
+ @utmz_hash = h = {}
34
+ h[:domain_hash], h[:timestamp], h[:session_counter], h[:campaign_number], kv_pairs = @utmz.split(".", 5)
35
+
36
+ kv_pairs && kv_pairs.split("|").each do |pair|
37
+ k, v = pair.split("=")
38
+ h[k.to_sym] = v if k && v
39
+ end
40
+
41
+ if h[:utmgclid]
42
+ h[:utmcsr] = 'google'
43
+ h[:utmcmd] = 'cpc'
44
+ end
45
+
46
+ end
47
+
48
+ def parse_utma
49
+ return if (@utma.nil? || @utma.empty?)
50
+ @utma_hash = h = {}
51
+ h[:domain_hash], h[:visitor_id], h[:initial_visit_at], h[:previous_visit_at], h[:current_visit_at], h[:session_counter] = @utma.split(".")
52
+ end
53
+
54
+ end
55
+
56
+
57
+ end
@@ -0,0 +1,130 @@
1
+ require('test_helper')
2
+
3
+ class GaCookieParserTest < Test::Unit::TestCase
4
+ include GaCookieParser
5
+
6
+ def setup
7
+ @utmz = "12979384.1294887021.1.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=test terms"
8
+ @utma = "12979384.1392679796.1289844797.1294798990.1294866800.4"
9
+ end
10
+
11
+
12
+ context "calling the constructor with no arguments" do
13
+ setup do
14
+ @result = GaCookieParser.new
15
+ end
16
+
17
+ should "result in nil values for utmz and utma" do
18
+ assert_nil @result.utma
19
+ assert_nil @result.utmz
20
+ end
21
+ end
22
+
23
+ context "creating a parser with a hash of raw cookie values" do
24
+ setup do
25
+ @result = GaCookieParser.new(:utmz => @utmz, :utma => @utma)
26
+ end
27
+
28
+ should "provide access to the raw values through utmz and utma" do
29
+ assert !@result.utmz.nil?
30
+ assert_equal @utma, @result.utma
31
+ assert_equal @utmz, @result.utmz
32
+ end
33
+
34
+ should "provide access to the parsed cookie hashes through utmz_hash and utma_hash" do
35
+ assert_not_nil @result.utmz_hash
36
+ assert_not_nil @result.utma_hash
37
+ end
38
+
39
+ should "accurately parse the utmz cookie" do
40
+ @p = @result.utmz_hash
41
+ assert_equal '12979384', @p[:domain_hash]
42
+ assert_equal '1294887021', @p[:timestamp]
43
+ assert_equal '1', @p[:session_counter]
44
+ assert_equal '2', @p[:campaign_number]
45
+ assert_equal 'google', @p[:utmcsr]
46
+ assert_equal '(organic)', @p[:utmccn]
47
+ assert_equal 'organic', @p[:utmcmd]
48
+ assert_equal 'test terms', @p[:utmctr]
49
+ assert_nil @p[:utmcct]
50
+ assert_nil @p[:utmgclid]
51
+ end
52
+
53
+ should "accurately parse the utma cookie" do
54
+ @p = @result.utma_hash
55
+ assert_equal '12979384', @p[:domain_hash]
56
+ assert_equal '1392679796', @p[:visitor_id]
57
+ assert_equal '1289844797', @p[:initial_visit_at]
58
+ assert_equal '1294798990', @p[:previous_visit_at]
59
+ assert_equal '1294866800', @p[:current_visit_at]
60
+ assert_equal '4', @p[:session_counter]
61
+ end
62
+
63
+ end
64
+
65
+ context "creating a parser with some cookie values missing" do
66
+ setup do
67
+ @missing_utmz = GaCookieParser.new(:utma => @utma)
68
+ @missing_utma = GaCookieParser.new(:utmz => @utmz)
69
+ end
70
+
71
+ should "return nil for the parsed hashes of those cookies" do
72
+ assert_nil @missing_utmz.utmz_hash
73
+ assert_nil @missing_utma.utma_hash
74
+ end
75
+
76
+ should "indicate the values are not present" do
77
+ assert !@missing_utmz.utmz?
78
+ assert @missing_utmz.utma?
79
+
80
+ assert !@missing_utma.utma?
81
+ assert @missing_utma.utmz?
82
+ end
83
+ end
84
+
85
+ context "parsing an adwords-linked cookie" do
86
+ setup do
87
+ @search = GaCookieParser.new(:utmz => "57979384.1294829655.1.1.utmgclid=CL_EqKvAtKYCFU80pAodyGj_IA|utmccn=(not set)|utmcmd=(not set)|utmctr=lolcats")
88
+ @content_network = GaCookieParser.new(:utmz => "23979724.1294828059.1.1.utmgclid=CI7wh8C6tKYCFU2DpAod7z97IQ|utmccn=(not set)|utmcmd=(not set)")
89
+ end
90
+
91
+ should "record the gclid" do
92
+ assert_equal "CL_EqKvAtKYCFU80pAodyGj_IA", @search.utmz_hash[:utmgclid]
93
+ assert_equal "CI7wh8C6tKYCFU2DpAod7z97IQ", @content_network.utmz_hash[:utmgclid]
94
+ end
95
+
96
+ should "set the utmcsr to 'google' and utmcmd to 'cpc'" do
97
+ assert_equal "google", @search.utmz_hash[:utmcsr]
98
+ assert_equal "cpc", @search.utmz_hash[:utmcmd]
99
+ assert_equal "google", @content_network.utmz_hash[:utmcsr]
100
+ assert_equal "cpc", @content_network.utmz_hash[:utmcmd]
101
+ end
102
+
103
+
104
+ end
105
+
106
+ context "calling the parse method on the module" do
107
+ setup do
108
+ @result = ::GaCookieParser.parse(:utmz => @utmz, :utma => @utma)
109
+ end
110
+
111
+ should "return a GaCookieParser instance correctly initialized" do
112
+ assert @result.is_a?(::GaCookieParser::GaCookieParser)
113
+ assert_not_nil @result.utmz_hash
114
+ assert_not_nil @result.utma_hash
115
+ end
116
+
117
+ end
118
+
119
+ context "parsing a bad cookie" do
120
+ setup do
121
+ @result = GaCookieParser.new(:utmz => "123XXX", :utma => "\"jk.,./l;o.mnhhlk")
122
+ end
123
+
124
+ should "not throw an error" do
125
+ assert @result
126
+ end
127
+ end
128
+
129
+
130
+ end
@@ -0,0 +1,4 @@
1
+ require('rubygems')
2
+ require('shoulda')
3
+
4
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/ga_cookie_parser')
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ga_cookie_parser
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Simon Robson
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-16 00:00:00 +07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: shoulda
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ description: Gem to parse the utmz and utma tracking coockies used by Google Analytics.
33
+ email:
34
+ - shrobson@gmail.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ files:
42
+ - .gitignore
43
+ - Gemfile
44
+ - Gemfile.lock
45
+ - LICENSE
46
+ - README.rdoc
47
+ - Rakefile
48
+ - ga_cookie_parser.gemspec
49
+ - lib/ga_cookie_parser.rb
50
+ - lib/ga_cookie_parser/version.rb
51
+ - test/ga_cookie_parser_test.rb
52
+ - test/test_helper.rb
53
+ has_rdoc: true
54
+ homepage: ""
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project: ga_cookie_parser
79
+ rubygems_version: 1.3.6
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Google Analytics Cookie Parsing Gem
83
+ test_files:
84
+ - test/ga_cookie_parser_test.rb
85
+ - test/test_helper.rb