google_analytics_tools 0.2

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/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require "rubygems"
2
+ require "pathname"
3
+ require "rake"
4
+ require "rake/testtask"
5
+
6
+ task :default => [ :test ]
7
+ Rake::TestTask.new do |t|
8
+ t.libs << "test"
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ t.verbose = true
11
+ end
12
+
13
+ # Gem
14
+ require "rake/gempackagetask"
15
+ require "lib/google_analytics_tools/version"
16
+
17
+ NAME = "google_analytics_tools"
18
+ SUMMARY = "A clean interface to generating the Google Analytics _gaq"
19
+ GEM_VERSION = GoogleAnalyticsTools::VERSION
20
+
21
+ spec = Gem::Specification.new do |s|
22
+ s.name = NAME
23
+ s.summary = s.description = SUMMARY
24
+ s.author = "Scott Bauer"
25
+ s.homepage = "http://rdoc.info/projects/Bauerpauer/google_analytics_tools"
26
+ s.version = GEM_VERSION
27
+ s.platform = Gem::Platform::RUBY
28
+ s.require_path = 'lib'
29
+ s.files = %w(Rakefile) + Dir.glob("lib/**/*")
30
+
31
+ s.add_dependency "json"
32
+ end
33
+
34
+ Rake::GemPackageTask.new(spec) do |pkg|
35
+ pkg.gem_spec = spec
36
+ end
37
+
38
+ desc "Install #{NAME} as a gem"
39
+ task :install => [:repackage] do
40
+ sh %{gem install pkg/#{NAME}-#{GEM_VERSION}}
41
+ end
42
+
43
+ task :version do
44
+ puts GEM_VERSION
45
+ end
46
+
47
+ spec_file = ".gemspec"
48
+ desc "Create #{spec_file}"
49
+ task :gemspec do
50
+ File.open(spec_file, "w") do |file|
51
+ file.puts spec.to_ruby
52
+ end
53
+ end
@@ -0,0 +1,44 @@
1
+ module GoogleAnalyticsTools
2
+
3
+ class AsyncTrackingQueue
4
+
5
+ def initialize
6
+ @events = []
7
+ end
8
+
9
+ def <<(event)
10
+ push(event)
11
+ end
12
+
13
+ def push(event, tracker_id = nil)
14
+ @events << renderer_for_event(event, tracker_id)
15
+ end
16
+
17
+ def to_s
18
+ <<-JAVASCRIPT
19
+ <script type="text/javascript">
20
+ var _gaq = _gaq || [];
21
+ #{@events.map { |event| event.to_s }.join("\n")}
22
+ (function() {
23
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
24
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
25
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
26
+ })();
27
+ </script>
28
+ JAVASCRIPT
29
+ end
30
+
31
+ private
32
+
33
+ def renderer_for_event(event, tracker_id)
34
+ case event
35
+ when Event then EventRenderer.new(event, tracker_id)
36
+ when EventCollection then EventCollectionRenderer.new(event, tracker_id)
37
+ end
38
+ end
39
+
40
+ end
41
+ end
42
+
43
+ GAT = GoogleAnalyticsTools
44
+ GAQ = GAT::AsyncTrackingQueue
@@ -0,0 +1,14 @@
1
+ class GAQ
2
+
3
+ class Event
4
+
5
+ attr_reader :name, :params
6
+
7
+ def initialize(name, *params)
8
+ @name = name
9
+ @params = params
10
+ end
11
+
12
+ end
13
+
14
+ end
@@ -0,0 +1,29 @@
1
+ class GAQ
2
+
3
+ class EventCollection
4
+
5
+ include Enumerable
6
+
7
+ class InvalidEventError < StandardError
8
+ def initialize(non_event)
9
+ super("GAQ::EventCollection#<< expects instances of GAQ::Event, you passed #{non_event}")
10
+ end
11
+ end
12
+
13
+ def initialize
14
+ @events = []
15
+ end
16
+
17
+ def <<(event)
18
+ raise InvalidEventError.new(event) unless event.is_a?(GAQ::Event)
19
+
20
+ @events << event
21
+ end
22
+
23
+ def each
24
+ @events.each { |e| yield e }
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,16 @@
1
+ class GAQ
2
+
3
+ class EventCollectionRenderer
4
+
5
+ def initialize(event_collection, tracker_id)
6
+ @event_collection = event_collection
7
+ @tracker_id = tracker_id
8
+ end
9
+
10
+ def to_s
11
+ @event_collection.map { |event| EventRenderer.new(event, @tracker_id).to_s }.join("\n")
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,16 @@
1
+ class GAQ
2
+
3
+ class EventRenderer
4
+
5
+ def initialize(event, tracker_id)
6
+ @event = event
7
+ @tracker_id = tracker_id
8
+ end
9
+
10
+ def to_s
11
+ "_gaq.push(#{[@tracker_id ? "#{@tracker_id}.#{@event.name}" : @event.name, *@event.params].to_json});"
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,89 @@
1
+ module GAQ::Events
2
+
3
+ class SetAccount < ::GAQ::Event
4
+
5
+ def initialize(account_id)
6
+ super('_setAccount', account_id)
7
+ end
8
+
9
+ end
10
+
11
+ class SetDomainName < ::GAQ::Event
12
+
13
+ def initialize(domain_name)
14
+ super('_setDomainName', domain_name)
15
+ end
16
+
17
+ end
18
+
19
+ class TrackPageview < ::GAQ::Event
20
+
21
+ def initialize
22
+ super('_trackPageview')
23
+ end
24
+
25
+ end
26
+
27
+ class TrackEvent < ::GAQ::Event
28
+
29
+ def initialize(category, action, label = nil, value = nil)
30
+ if label || value
31
+ super('_trackEvent', category.to_s, action.to_s, label ? label.to_s : nil, value ? value.to_i : nil)
32
+ else
33
+ super('_trackEvent', category.to_s, action.to_s)
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+ module ECommerce
40
+
41
+ class AddTransaction < ::GAQ::Event
42
+
43
+ # _gaq.push(['_addTrans',
44
+ # '1234', // order ID - required
45
+ # 'Acme Clothing', // affiliation or store name
46
+ # '11.99', // total - required
47
+ # '1.29', // tax
48
+ # '5', // shipping
49
+ # 'San Jose', // city
50
+ # 'California', // state or province
51
+ # 'USA' // country
52
+ # ]);
53
+
54
+ def initialize(order_id, store_name, total, tax, shipping, city, state_or_province, country)
55
+ super('_addTrans', order_id.to_s, store_name.to_s, total.to_s, tax.to_s, shipping.to_s, city.to_s, state_or_province.to_s, country.to_s)
56
+ end
57
+
58
+ end
59
+
60
+ class AddItem < ::GAQ::Event
61
+
62
+ # _gaq.push(['_addItem',
63
+ # '1234', // order ID - required
64
+ # 'DD44', // SKU/code - required
65
+ # 'T-Shirt', // product name
66
+ # 'Green Medium', // category or variation
67
+ # '11.99', // unit price - required
68
+ # '1' // quantity - required
69
+ # ]);
70
+
71
+ def initialize(order_id, product_id, product_name, product_variation, unit_price, quantity)
72
+ super('_addItem', order_id.to_s, product_id.to_s, product_name.to_s, product_variation.to_s, unit_price.to_s, quantity.to_s)
73
+ end
74
+
75
+ end
76
+
77
+ class TrackTransaction < ::GAQ::Event
78
+
79
+ # _gaq.push(['_trackTrans']); //submits transaction to the Analytics servers
80
+
81
+ def initialize
82
+ super('_trackTrans')
83
+ end
84
+
85
+ end
86
+
87
+ end
88
+
89
+ end
@@ -0,0 +1,3 @@
1
+ module GoogleAnalyticsTools
2
+ VERSION = "0.2"
3
+ end
@@ -0,0 +1,11 @@
1
+ require "json"
2
+ require "pathname"
3
+
4
+ $:.unshift(Pathname(__FILE__).dirname.expand_path)
5
+
6
+ require "google_analytics_tools/tracking/async_tracking_queue"
7
+ require "google_analytics_tools/tracking/events/event"
8
+ require "google_analytics_tools/tracking/events/event_collection"
9
+ require "google_analytics_tools/tracking/events/event_renderer"
10
+ require "google_analytics_tools/tracking/events/event_collection_renderer"
11
+ require "google_analytics_tools/tracking/events/events"
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_analytics_tools
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ version: "0.2"
10
+ platform: ruby
11
+ authors:
12
+ - Scott Bauer
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-03 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: json
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: A clean interface to generating the Google Analytics _gaq
35
+ email:
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - Rakefile
44
+ - lib/google_analytics_tools/tracking/async_tracking_queue.rb
45
+ - lib/google_analytics_tools/tracking/events/event.rb
46
+ - lib/google_analytics_tools/tracking/events/event_collection.rb
47
+ - lib/google_analytics_tools/tracking/events/event_collection_renderer.rb
48
+ - lib/google_analytics_tools/tracking/events/event_renderer.rb
49
+ - lib/google_analytics_tools/tracking/events/events.rb
50
+ - lib/google_analytics_tools/version.rb
51
+ - lib/google_analytics_tools.rb
52
+ has_rdoc: true
53
+ homepage: http://rdoc.info/projects/Bauerpauer/google_analytics_tools
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_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
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.3.7
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: A clean interface to generating the Google Analytics _gaq
86
+ test_files: []
87
+