eyestreet-analytics_goo 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ coverage
2
+ pkg
3
+ rdoc
4
+ tags
5
+ test/tmp
6
+ test/version_tmp
7
+ tmp
data/CHANGELOG ADDED
@@ -0,0 +1,44 @@
1
+ [3b07757 | Tue Jun 02 19:39:50 UTC 2009] Rob Christie <robchristie@gmail.com>
2
+
3
+ * More changes to support rails environments.
4
+
5
+ [3452913 | Tue Jun 02 18:39:51 UTC 2009] Rob Christie <robchristie@gmail.com>
6
+
7
+ * Changed configuration to optionally allow mixins to Rails core classes. Changed to the use of markdown for my readme.
8
+
9
+ [e75017f | Tue Jun 02 07:18:37 UTC 2009] Rob Christie <robchristie@gmail.com>
10
+
11
+ * Migrating to a gem installation.
12
+
13
+ [5b7698f | Tue Jun 02 06:00:49 UTC 2009] Rob Christie <robchristie@gmail.com>
14
+
15
+ * Updated name of project.
16
+
17
+ [00a001f | Tue Jun 02 05:16:40 UTC 2009] Rob Christie <robchristie@gmail.com>
18
+
19
+ * Updated based on naming and configuration changes.
20
+
21
+ [17529a1 | Mon Jun 01 20:10:51 UTC 2009] Rob Christie <robchristie@gmail.com>
22
+
23
+ * Updated class so that random values are changed on each call to track_page_view.
24
+
25
+ [e0d96e7 | Mon Jun 01 19:14:54 UTC 2009] Rob Christie <robchristie@gmail.com>
26
+
27
+ * Updated changelog.
28
+
29
+ [b6ec97c | Mon Jun 01 19:12:50 UTC 2009] Rob Christie <robchristie@gmail.com>
30
+
31
+ * Renamed #track_it to #track_page_view, so you can eventually add other Analytics supported methods like #track_event.
32
+
33
+ [99e5798 | Mon Jun 01 18:55:32 UTC 2009] Rob Christie <robchristie@gmail.com>
34
+
35
+ * Updated CHANGELOG after running script to pull and format messages from git.
36
+
37
+ [bbc664d | Mon Jun 01 18:48:16 UTC 2009] Rob Christie <robchristie@gmail.com>
38
+
39
+ * Remove the AnalyticsConfig object and we now pass in a hash of initialization values. Also added support for passing in multiple hashes of initialization values.
40
+
41
+ [81ce0f8 | Wed May 20 14:29:06 UTC 2009] Rob Christie <robchristie@gmail.com>
42
+
43
+ * initial commit
44
+
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
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.markdown ADDED
@@ -0,0 +1,63 @@
1
+ # AnalyticsGoo
2
+
3
+ AnalyticsGoo is a gem (or plugin) that provides server side non-javascript tracking using google analytics.
4
+
5
+ ## Install
6
+
7
+ gem install eyestreet-analytics_goo
8
+
9
+ ## Example
10
+ The simple usage is the following:
11
+
12
+ tracker = AnalyticsGoo.config(:google_analytics,
13
+ :analytics_id => "UA-3536616-5",
14
+ :domain => "demo.eyestreet.com")
15
+
16
+ tracker.track_page_view("/foo")
17
+
18
+ The first parameter passed to the config initializes the desired analytics adapter. Currently we only support
19
+ Google Analytics. The subsequent hash provide a name for the account, the analytics id, and a domain.
20
+
21
+ ## Rails Usage
22
+
23
+ In your environment.rb add the following:
24
+
25
+ config.gem "analytics_goo", :lib => "analytics_goo"
26
+
27
+
28
+ You can call things in the same way as above, or you can mix in some additional rails specific functionality that is shown below.
29
+ In an intializer like config/initializers/analytics_goo.rb or in your appropriate environment.rb file
30
+
31
+ AnalyticsGoo.config(:google_analytics, :analytics_id => "UA-3536616-5",
32
+ :domain => "demo.eyestreet.com",
33
+ :rails_core_mixins => true,
34
+ :environment => "production")
35
+
36
+ * analytics type - Currently we only support :google_analytics
37
+
38
+ The analytics option hash takes the following:
39
+
40
+ * :analytics_id
41
+ * :domain
42
+ * :rails_core_mixins - Defaults to false. If set to true then you get an accessor method on rails core classes for analytics_goo
43
+ * :environment - The RAILS_ENV environment that the analytics code should be called in. In all other environments it is a noop.
44
+
45
+ Then in your models, controllers, and mailers you can do the following:
46
+
47
+ user = User.find(:first)
48
+ user.analytics_goo.track_page_view("/found_my_first_user")
49
+
50
+ ## Testing
51
+
52
+ Check test/test_helper.rb to make sure you have the required gems installed.
53
+
54
+ rake test
55
+ or
56
+ autotest
57
+
58
+ # Credits
59
+
60
+ AnalyticsGoo is maintained by [Rob Christie](mailto:rob.christie@eyestreet.com) and is funded by [EyeStreet Software](http://www.eyestreet.com).
61
+
62
+
63
+ Copyright (c) 2009 rwc9u, Eyestreet Software released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,39 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "analytics_goo"
9
+ gem.summary = %Q{TODO}
10
+ gem.email = "robchristie@gmail.com"
11
+ gem.homepage = "http://github.com/eyestreet/analytics_goo"
12
+ gem.files.include "rails/**/*"
13
+ gem.authors = ["Rob Christie"]
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ desc 'Default: run unit tests.'
22
+ task :default => :test
23
+
24
+ desc 'Test the analytics_goo plugin.'
25
+ Rake::TestTask.new(:test) do |t|
26
+ t.libs << 'lib'
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = true
30
+ end
31
+
32
+ desc 'Generate documentation for the analytics_goo plugin.'
33
+ Rake::RDocTask.new(:rdoc) do |rdoc|
34
+ rdoc.rdoc_dir = 'rdoc'
35
+ rdoc.title = 'AnalyticsGoo'
36
+ rdoc.options << '--line-numbers' << '--inline-source'
37
+ rdoc.rdoc_files.include('README')
38
+ rdoc.rdoc_files.include('lib/**/*.rb')
39
+ end
data/TODO ADDED
@@ -0,0 +1,9 @@
1
+ * Add javascript-less support for google analytics event tracking.
2
+
3
+ * Allow an asynchronous operation mode so that tracking has the option of not being done synchronously.
4
+
5
+ * Clean up tests.
6
+
7
+ * Pass along additional header information to google in the image request so that items like browser can be specified.
8
+
9
+ * Peak at the cookies that came in on the most recent request and then use those to build our utmcc.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,58 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{analytics_goo}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Rob Christie"]
9
+ s.date = %q{2009-06-06}
10
+ s.email = %q{robchristie@gmail.com}
11
+ s.extra_rdoc_files = [
12
+ "README.markdown"
13
+ ]
14
+ s.files = [
15
+ ".gitignore",
16
+ "CHANGELOG",
17
+ "MIT-LICENSE",
18
+ "README.markdown",
19
+ "Rakefile",
20
+ "TODO",
21
+ "VERSION",
22
+ "analytics_goo.gemspec",
23
+ "autotest/CHANGELOG",
24
+ "autotest/LICENSE",
25
+ "autotest/README.rdoc",
26
+ "autotest/discover.rb",
27
+ "autotest/railsplugin.rb",
28
+ "autotest/railsplugin_rspec.rb",
29
+ "init.rb",
30
+ "lib/analytics_goo.rb",
31
+ "lib/analytics_goo/google_analytics_adapter.rb",
32
+ "rails/init.rb",
33
+ "rails/init.rb",
34
+ "tasks/analytics_goo.rake",
35
+ "test/analytics_goo_test.rb",
36
+ "test/test_helper.rb"
37
+ ]
38
+ s.has_rdoc = true
39
+ s.homepage = %q{http://github.com/eyestreet/analytics_goo}
40
+ s.rdoc_options = ["--charset=UTF-8"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = %q{1.3.1}
43
+ s.summary = %q{TODO}
44
+ s.test_files = [
45
+ "test/analytics_goo_test.rb",
46
+ "test/test_helper.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 2
52
+
53
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
54
+ else
55
+ end
56
+ else
57
+ end
58
+ end
@@ -0,0 +1,7 @@
1
+
2
+ Git Master
3
+
4
+ * Added RSpec support [Adam Meehan]
5
+ * Added path_to_classname so that failed tests that pass run the suite again. [Ken Collins]
6
+
7
+
data/autotest/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+
2
+ Copyright (c) 2008 Action Moniker LLC., http://www.actionmoniker.com/
3
+
4
+ Permission is hereby granted, free of charge, to any person
5
+ obtaining a copy of this software and associated documentation
6
+ files (the "Software"), to deal in the Software without
7
+ restriction, including without limitation the rights to use,
8
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the
10
+ Software is furnished to do so, subject to the following
11
+ conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
+ OTHER DEALINGS IN THE SOFTWARE.
24
+
@@ -0,0 +1,40 @@
1
+
2
+ == Autotest::Railsplugin: Use autotest to develop your rails plugins!
3
+
4
+ This is a work in progress. To date the mappings for this autotest class are working
5
+ very well for me. Please read the 'railsplugin.rb' file for details. The short story is
6
+ that default autotest does not map well to rails test naming and this class addresses
7
+ that. Works for test/unit and rspec.
8
+
9
+
10
+ === INSTALLATION
11
+
12
+ * From your plugin root run this
13
+
14
+ git clone git://github.com/metaskills/autotest_railsplugin.git autotest
15
+
16
+ * You can now use autotest.
17
+
18
+ $ autotest
19
+ loading autotest/railsplugin
20
+
21
+
22
+ === CUSTOMISATION
23
+
24
+ If you need to customize the exceptions or mappings, create an .autotest file in the root
25
+ of the plugin and place them in there. An example below of adding a custom exception
26
+
27
+ Autotest.add_hook :initialize do |at|
28
+ at.add_exception %r%^\./spec/dont_touch%
29
+ end
30
+
31
+
32
+ === HELPING OUT
33
+
34
+ If you find that this class is not doing what you think it should. Perhaps when you
35
+ save a file it does not running the corresponding test and you want me to fix it, please
36
+ do the following.
37
+
38
+ Email the directory structure of both the library file and the test file you expected it
39
+ to run to ken *[at]* metaskills.net
40
+
@@ -0,0 +1,6 @@
1
+
2
+ Autotest.add_discovery do
3
+ 'railsplugin'
4
+ end
5
+
6
+
@@ -0,0 +1,53 @@
1
+ class Autotest::Railsplugin < Autotest
2
+
3
+ def initialize
4
+ super
5
+
6
+ # Default libs for autotest. So far they suit us just fine.
7
+ # self.libs = %w[. lib test].join(File::PATH_SEPARATOR)
8
+
9
+ # Ignore these directories in the plugin.
10
+ add_exception %r%^\./(?:autotest|tasks)%
11
+
12
+ # Ignore these ruby files in the root of the plugin folder.
13
+ add_exception %r%^\./(install|uninstall)\.rb$%
14
+
15
+ # Ignore these misc files in the root of the plugin folder.
16
+ add_exception %r%^\./(.*LICENSE|Rakefile|README.*|CHANGELOG.*)$%i
17
+
18
+ # Ignore any log file.
19
+ add_exception %r%.*\.log$%
20
+
21
+ clear_mappings
22
+
23
+ # Easy start. Any test file saved runs that file
24
+ self.add_mapping(%r%^test/.*_test.rb$%) do |filename, matchs|
25
+ filename
26
+ end
27
+
28
+ # If any file in lib matches up to a file in the same directory structure of
29
+ # the test directory, ofcourse having _test.rb at the end, will run that test.
30
+ self.add_mapping(%r%^lib/(.*)\.rb$%) do |filename, matchs|
31
+ filename_path = matchs[1]
32
+ files_matching %r%^test/#{filename_path}_test\.rb$%
33
+ end
34
+
35
+ # If any core test file like the helper, boot, database.yml change, then run
36
+ # all matching .*_test.rb files in the test directory.
37
+ add_mapping %r%^test/(boot|helper|test_helper|factories)\.rb|database.yml% do
38
+ files_matching %r%^test/.*_test\.rb$%
39
+ end
40
+
41
+ end
42
+
43
+ def path_to_classname(s)
44
+ sep = File::SEPARATOR
45
+ f = s.sub(/^test#{sep}?/, '').sub(/\.rb$/, '').split(sep)
46
+ f = f.map { |path| path.split(/_/).map { |seg| seg.capitalize }.join }
47
+ # f = f.map { |path| path =~ /Test$/ ? path : "#{path}Test" }
48
+ f.join('::')
49
+ end
50
+
51
+
52
+ end
53
+
@@ -0,0 +1,29 @@
1
+ require 'autotest/rspec'
2
+
3
+ class Autotest::RailspluginRspec < Autotest::Rspec
4
+
5
+ def initialize
6
+ super
7
+
8
+ # Default libs for autospec. So far they suit us just fine.
9
+ # self.libs = %w[. lib spec].join(File::PATH_SEPARATOR)
10
+
11
+ # Ignore these directories in the plugin.
12
+ add_exception %r%^\./(?:autotest|tasks)%
13
+
14
+ # Ignore these ruby files in the root of the plugin folder.
15
+ add_exception %r%^\./(install|uninstall)\.rb$%
16
+
17
+ # Ignore these misc files in the root of the plugin folder.
18
+ add_exception %r%^\./(.*LICENSE|Rakefile|README.*|CHANGELOG.*)$%i
19
+
20
+ # Ignore any log file.
21
+ add_exception %r%.*\.log$%
22
+
23
+ add_mapping %r%^spec/(boot|helper|factories)\.rb|database.yml% do
24
+ files_matching %r%^spec/.*_spec\.rb$%
25
+ end
26
+
27
+ end
28
+
29
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'rails', 'init')
@@ -0,0 +1,187 @@
1
+ # AnalyticsGoo
2
+ # The following came from :
3
+ # http://code.google.com/apis/analytics/docs/tracking/gaTrackingTroubleshooting.html
4
+
5
+ # The following parameters are currently not implemented but do not affect basic usage.
6
+ # utme X10 Data Parameter Value is encoded.
7
+
8
+ # utmipc
9
+ # Product Code. This is the sku code for a given product.
10
+ # utmipc=989898ajssi
11
+ # utmipn
12
+ # Product Name, which is a URL-encoded string. utmipn=tee%20shirt
13
+ # utmipr
14
+ # Unit Price. Set at the item level. Value is set to numbers only in U.S. currency format.
15
+ # utmipr=17100.32
16
+ # utmiqt
17
+ # Quantity. utmiqt=4
18
+ # utmiva
19
+ # Variations on an item. For example: large, medium, small, pink, white, black, green. String is URL-encoded.
20
+ # utmiva=red;
21
+ # utmt
22
+ # A special type variable applied to events, transactions, items and user-defined variables. utmt=Dog%20Owner
23
+ # utmtci
24
+ # Billing City utmtci=San%20Diego
25
+ # utmtco
26
+ # Billing Country utmtco=United%20Kingdom
27
+ # utmtid
28
+ # Order ID, URL-encoded string. utmtid=a2343898
29
+ # utmtrg
30
+ # Billing region, URL-encoded string. utmtrg=New%20Brunswick
31
+ # utmtsp
32
+ # Shipping cost. Values as for unit and price. utmtsp=23.95
33
+ # utmtst
34
+ # Affiliation. Typically used for brick and mortar applications in ecommerce. utmtst=google%20mtv%20store
35
+ # utmtto
36
+ # Total. Values as for unit and price. utmtto=334.56
37
+ # utmttx
38
+ # Tax. Values as for unit and price. utmttx=29.16
39
+
40
+ module AnalyticsGoo
41
+ class GoogleAnalyticsAdapter
42
+ attr_accessor :domain, :analytics_id, :env, :noop, :utmdt, :utmfl
43
+ # utmdt
44
+ # Page title, which is a URL-encoded string. utmdt=analytics%20page%20test
45
+ # utmfl
46
+ # Flash version
47
+
48
+ def initialize(ac)
49
+ # sets the environment that this should be run in
50
+ @analytics_id = ac[:analytics_id]
51
+ @domain = ac[:domain]
52
+ @env = ac[:environment]
53
+ @noop = ac[:noop] || false
54
+ @utmdt = ac[:utmdt] || ""
55
+ @utmfl = ""
56
+ end
57
+
58
+ GA_DOMAIN = "www.google-analytics.com"
59
+ # GA_DOMAIN = "dev.www.mobilediscovery.com"
60
+ GA_IMAGE = "__utm.gif"
61
+
62
+ def urchin_url(ssl = false)
63
+ protocol = (ssl == true) ? "https":"http"
64
+ "#{protocol}://#{GA_DOMAIN}/#{GA_IMAGE}"
65
+ end
66
+
67
+ # utmac Account String. Appears on all requests. utmac=UA-2202604-2
68
+ def utmac
69
+ self.analytics_id
70
+ end
71
+
72
+ # utmhn
73
+ # Host Name, which is a URL-encoded string. utmhn=x343.gmodules.com
74
+ def utmhn
75
+ self.domain
76
+ end
77
+
78
+ # utmcs
79
+ # Language encoding for the browser. Some browsers don't set this, in which case it is set to "-"
80
+ # utmcs=ISO-8859-1
81
+ def utmcs
82
+ "UTF-8"
83
+ end
84
+
85
+ # utmje
86
+ # Indicates if browser is Java-enabled. 1 is true. utmje=1
87
+ def utmje
88
+ "1"
89
+ end
90
+
91
+ # utmn
92
+ # Unique ID generated for each GIF request to prevent caching of the GIF image. utmn=1142651215
93
+ def utmn
94
+ ActiveSupport::SecureRandom.random_number(9999999999)
95
+ end
96
+
97
+ # utmsc
98
+ # Screen color depth utmsc=24-bit
99
+ def utmsc
100
+ "0-bit"
101
+ end
102
+
103
+ # utmsr
104
+ # Screen resolution utmsr=2400x1920&
105
+ def utmsr
106
+ "0x0"
107
+ end
108
+
109
+ # utmul
110
+ # Browser language. utmul=pt-br
111
+ def utmul
112
+ "en-us"
113
+ end
114
+
115
+ # utmwv
116
+ # Tracking code version utmwv=1
117
+ def utmwv
118
+ "4.3"
119
+ end
120
+
121
+ # utmp
122
+ # Page request of the current page. utmp=/testDirectory/myPage.html
123
+ # def utmp
124
+ # self.path
125
+ # end
126
+
127
+ # utmr
128
+ # Referral, complete URL. utmr=http://www.example.com/aboutUs/index.php?var=selected
129
+ def utmr
130
+ "-"
131
+ end
132
+
133
+
134
+ # may not be needed
135
+
136
+ # utmcn Starts a new campaign session. Either utmcn or utmcr is present on any given request. Changes the campaign tracking data; but does not start a new session
137
+ # utmcn=1
138
+ def utmcn
139
+ "1"
140
+ end
141
+
142
+ # utmcr
143
+ # Indicates a repeat campaign visit. This is set when any subsequent clicks occur on the same link. Either utmcn or utmcr is present on any given request.
144
+ # utmcr=1
145
+ def utmcr
146
+ "1"
147
+ end
148
+
149
+ # utmcc
150
+ # Cookie values. This request parameter sends all the cookies requested from the page.
151
+ # utmcc=__utma%3D117243.1695285.22%3B%2B __utmz%3D117945243.1202416366.21.10. utmcsr%3Db%7C utmccn%3D(referral)%7C utmcmd%3Dreferral%7C utmcct%3D%252Fissue%3B%2B
152
+ def utmcc
153
+ "__utma%3D#{utmcc_cookie}.#{utmcc_random}.#{utmcc_time}.#{utmcc_time}.#{utmcc_time}.10%3B%2B__utmz%3D#{utmcc_cookie}.#{utmcc_time}.1.1.utmcsr%3D(direct)%7Cutmccn%3D(direct)%7Cutmcmd%3D(none)%3B"
154
+ end
155
+
156
+ # send a request to get the image from google
157
+ def track_page_view(path)
158
+ res = ""
159
+ unless @noop == true
160
+ res = track_it(path)
161
+ end
162
+ res
163
+ end
164
+
165
+ protected
166
+ def track_it(path)
167
+ # puts "/__utm.gif?utmwv=#{self.utmwv}&utmn=#{self.utmn}&utmhn=#{self.utmhn}&utmcs=#{self.utmcs}&utmsr=#{self.utmsr}&utmsc=#{self.utmsc}&utmul=#{self.utmul}&utmje=#{self.utmje}&utmfl=#{URI::escape(self.utmfl)}&utmdt=#{URI::escape(self.utmdt)}&utmhid=#{utmhid}&utmr=#{self.utmr}&utmp=#{path}&utmac=#{self.utmac}&utmcc=#{self.utmcc} \n"
168
+ Net::HTTP.get_response(GA_DOMAIN,"/__utm.gif?utmwv=#{self.utmwv}&utmn=#{self.utmn}&utmhn=#{self.utmhn}&utmcs=#{self.utmcs}&utmsr=#{self.utmsr}&utmsc=#{self.utmsc}&utmul=#{self.utmul}&utmje=#{self.utmje}&utmfl=#{URI::escape(self.utmfl)}&utmdt=#{URI::escape(self.utmdt)}&utmhid=#{utmhid}&utmr=#{self.utmr}&utmp=#{path}&utmac=#{self.utmac}&utmcc=#{self.utmcc}")
169
+ end
170
+
171
+ def utmcc_cookie
172
+ ActiveSupport::SecureRandom.random_number(89999999) + 10000000
173
+ end
174
+
175
+ def utmcc_random
176
+ ActiveSupport::SecureRandom.random_number(1147483647) + 1000000000
177
+ end
178
+
179
+ def utmcc_time
180
+ Time.new.to_i
181
+ end
182
+
183
+ def utmhid
184
+ ActiveSupport::SecureRandom.random_number(999999999)
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,53 @@
1
+ require 'uri'
2
+ require 'cgi'
3
+ require 'net/http'
4
+ require 'activesupport'
5
+ require 'analytics_goo/google_analytics_adapter'
6
+
7
+ module AnalyticsGoo
8
+ # generic adapter not found exception
9
+ class AnalyticsAdapterNotFound < StandardError
10
+ end
11
+
12
+
13
+ # Factory for returning the appropriate analytics object. The <tt>type</tt> is
14
+ # a symbol that defines the type of analytics tracker you want to create. Currently,
15
+ # the only acceptable value is :google_analytics. The <tt>analytics</tt> hash holds
16
+ # the required keys of analytics_id and the domain of the adapter that you are configuring.
17
+ # Setting the key <tt>rails_core_mixins</tt> to
18
+ # true will mixin an accessor for this object into the core rails framework classes (active_record, action_controller and action_mailer). The default
19
+ # for this is false so that there are no rails dependencies on the gem. The key <tt>environment</tt> by default is set to "production". The
20
+ # rails mixin functionality checks this value against its RAILS_ENV then it calls out to the analytics only if the value is nil or matches.
21
+ def self.config(type, analytics = {})
22
+ begin
23
+ s = type.to_s + "_adapter"
24
+ adapter = "AnalyticsGoo::" + s.camelize
25
+ analytics[:environment] = "production" if analytics[:environment].nil?
26
+ if analytics[:rails_core_mixins] == true
27
+ analytics[:noop] = (RAILS_ENV != analytics[:environment])
28
+ tracker = adapter.constantize.new(analytics)
29
+ for framework in ([ :active_record, :action_controller, :action_mailer ])
30
+ framework.to_s.camelize.constantize.const_get("Base").send :include, AnalyticsGoo::InstanceMethods
31
+ end
32
+ silence_warnings { Object.const_set "ANALYTICS_TRACKER", tracker }
33
+ else
34
+ tracker = adapter.constantize.new(analytics)
35
+ end
36
+ tracker
37
+ rescue StandardError
38
+ raise AnalyticsAdapterNotFound
39
+ end
40
+ end
41
+
42
+ module InstanceMethods
43
+ # any methods here will apply to instances
44
+ def analytics_goo
45
+ if defined?(ANALYTICS_TRACKER)
46
+ ANALYTICS_TRACKER
47
+ else
48
+ nil
49
+ end
50
+ end
51
+ end
52
+ end
53
+
data/rails/init.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "analytics_goo"
2
+
3
+ # add our functionality to rails core classes
4
+ # for framework in ([ :active_record, :action_controller, :action_mailer ])
5
+ # framework.to_s.camelize.constantize.const_get("Base").send :include, AnalyticsGoo::InstanceMethods
6
+ # end
7
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :rails_analytics do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,130 @@
1
+ require 'test_helper'
2
+ require 'analytics_goo'
3
+
4
+ class AnalyticsGooTest < ActiveSupport::TestCase
5
+
6
+ context "AnalyticsGoo " do
7
+ setup do
8
+ @analytics_config = { :analytics_id => "UA-2202604-2",:domain => "test.local" }
9
+ end
10
+ context "contains a GoogleAnalyticsAdapter which when passed initialization data" do
11
+ should "be a valid class" do
12
+ assert AnalyticsGoo::GoogleAnalyticsAdapter.new(@analytics_config)
13
+ end
14
+ end
15
+ context "contains a GoogleAnalyticsAdapter when instantiated with the config method" do
16
+ should "be a valid class" do
17
+ assert AnalyticsGoo.config(:google_analytics, @analytics_config)
18
+ end
19
+ end
20
+ should "raise an exception when instantiated with an invalid adapter" do
21
+ assert_raise AnalyticsGoo::AnalyticsAdapterNotFound do
22
+ AnalyticsGoo.config(:moogle_analytics, @analytics_config)
23
+ end
24
+ end
25
+ context "can pass along an environment variable that" do
26
+ setup do
27
+ @analytics_config[:environment] = "test"
28
+ end
29
+ should "be passed along to the instantiated class" do
30
+ assert_equal "test", AnalyticsGoo.config(:google_analytics, @analytics_config).env
31
+ end
32
+ end
33
+ end
34
+
35
+ context "AnalyticsGoo " do
36
+ setup do
37
+ @analytics_config = { :analytics_id => "UA-2202604-2",:domain => "test.local"}
38
+ @ga = AnalyticsGoo::GoogleAnalyticsAdapter.new(@analytics_config)
39
+ @ga.utmdt = "This is the page title"
40
+ @ga.utmfl = "9.0 r124"
41
+ end
42
+ context "when initialized with an analytics id" do
43
+ should "return that id" do
44
+ assert_equal "UA-2202604-2", @ga.utmac
45
+ end
46
+ end
47
+ context "when initialized with a domain" do
48
+ should "return that domain" do
49
+ assert_equal "test.local", @ga.domain
50
+ end
51
+ end
52
+ context "that has been initialized" do
53
+ should "have an urchin url" do
54
+ assert_equal "http://www.google-analytics.com/__utm.gif", @ga.urchin_url
55
+ end
56
+ end
57
+ context "when included and told that the url needs to be ssl" do
58
+ should "have an https URL" do
59
+ assert_equal "https://www.google-analytics.com/__utm.gif", @ga.urchin_url(true)
60
+ end
61
+ end
62
+ context "creates an image URL that " do
63
+ should "have a utmac equal to the specified analytics id" do
64
+ assert_equal "UA-2202604-2", @ga.utmac
65
+ end
66
+ should "have a utmhn equal to the host name" do
67
+ assert_equal "test.local", @ga.domain
68
+ end
69
+ should "have a utmfl equal to the flash version installed" do
70
+ assert_equal "9.0 r124", @ga.utmfl
71
+ end
72
+ should "have a utmcs equal to the language encoding used for the browser." do
73
+ assert_equal "UTF-8", @ga.utmcs
74
+ end
75
+ should "have a utmdt equal to the Page Title of the page" do
76
+ assert_equal "This is the page title", @ga.utmdt
77
+ end
78
+ should "have a utmje equal to 1" do
79
+ assert_equal "1", @ga.utmje
80
+ end
81
+ should "have a utmn equal to a random number that is generated to prevent caching" do
82
+ assert @ga.utmn != @ga.utmn
83
+ end
84
+ should "have a utmsr equal to the screen resolution" do
85
+ assert_equal "0x0", @ga.utmsr
86
+ end
87
+ should "have a utmsc equal to the screen color depth" do
88
+ assert_equal "0-bit", @ga.utmsc
89
+ end
90
+ should "have a utmul equal to the browser language" do
91
+ assert_equal "en-us", @ga.utmul
92
+ end
93
+ should "have a utmwv equal to the tracking code version" do
94
+ assert_equal "4.3", @ga.utmwv
95
+ end
96
+ should "have a utmr equal to the referring page" do
97
+ assert_equal "-", @ga.utmr
98
+ end
99
+ should "have a utmcr set to 1" do
100
+ assert_equal "1", @ga.utmcr
101
+ end
102
+ end
103
+ end
104
+ context "Rails Analyics" do
105
+ setup do
106
+ @ga2 = AnalyticsGoo::GoogleAnalyticsAdapter.new(:analytics_id => "UA-3536616-5",:domain => "demo.mobilediscovery.com")
107
+ @ga2.expects(:utmcc_cookie).times(2).returns(10000001)
108
+ @ga2.expects(:utmcc_random).returns(1147483647)
109
+ @now = Time.now.to_i
110
+ @ga2.expects(:utmcc_time).times(4).returns(@now)
111
+ end
112
+ should "return a well formed utmcc cookie" do
113
+ assert_equal "__utma%3D10000001.1147483647.#{@now}.#{@now}.#{@now}.10%3B%2B__utmz%3D10000001.#{@now}.1.1.utmcsr%3D(direct)%7Cutmccn%3D(direct)%7Cutmcmd%3D(none)%3B", @ga2.utmcc
114
+ end
115
+ end
116
+ # TODO: mock this test. Currently it actually does do a request. Was using this to verify that it actually was working.
117
+ context "An analytics tracking event" do
118
+ setup do
119
+ @ga3 = AnalyticsGoo::GoogleAnalyticsAdapter.new(:analytics_id => "UA-3536616-5",:domain => "demo.mobilediscovery.com")
120
+ end
121
+ context "makes a request for an image on the google analytics server" do
122
+ setup do
123
+ @resp = @ga3.track_page_view("/testFoo/myPage.html")
124
+ end
125
+ should "be a valid response" do
126
+ assert @resp.is_a?(Net::HTTPOK)
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
4
+ require 'active_support/test_case'
5
+ gem 'thoughtbot-shoulda', ">= 2.10.1"
6
+ require 'shoulda'
7
+ require 'mocha'
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eyestreet-analytics_goo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rob Christie
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-06 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: robchristie@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.markdown
24
+ files:
25
+ - .gitignore
26
+ - CHANGELOG
27
+ - MIT-LICENSE
28
+ - README.markdown
29
+ - Rakefile
30
+ - TODO
31
+ - VERSION
32
+ - analytics_goo.gemspec
33
+ - autotest/CHANGELOG
34
+ - autotest/LICENSE
35
+ - autotest/README.rdoc
36
+ - autotest/discover.rb
37
+ - autotest/railsplugin.rb
38
+ - autotest/railsplugin_rspec.rb
39
+ - init.rb
40
+ - lib/analytics_goo.rb
41
+ - lib/analytics_goo/google_analytics_adapter.rb
42
+ - rails/init.rb
43
+ - tasks/analytics_goo.rake
44
+ - test/analytics_goo_test.rb
45
+ - test/test_helper.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/eyestreet/analytics_goo
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --charset=UTF-8
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.2.0
69
+ signing_key:
70
+ specification_version: 2
71
+ summary: TODO
72
+ test_files:
73
+ - test/analytics_goo_test.rb
74
+ - test/test_helper.rb