bacchanalytics 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,11 @@
1
+ rvm:
2
+ - 1.8.7-p352
3
+ - 1.9.2-p290
4
+ script: "bundle exec rake"
5
+ gemfile:
6
+ - gemfiles/2.3.gemfile
7
+ - gemfiles/3.0.gemfile
8
+ - gemfiles/3.1.gemfile
9
+ branches:
10
+ only:
11
+ - master
@@ -0,0 +1,14 @@
1
+ appraise "2.3" do
2
+ gem "activesupport", "~> 2.3"
3
+ gem "rack", "~> 1.1.0"
4
+ end
5
+
6
+ appraise "3.0" do
7
+ gem "activesupport", "~> 3.0"
8
+ gem "rack", "~> 1.2.1"
9
+ end
10
+
11
+ appraise "3.1" do
12
+ gem "activesupport", "~> 3.1"
13
+ gem "rack", "~> 1.3.2"
14
+ end
@@ -0,0 +1,53 @@
1
+ 0.1.9
2
+ -----
3
+
4
+ * Build the gem
5
+ * Scope middlewares in Bacchanalytics scope
6
+
7
+ 0.1.8
8
+ -----
9
+
10
+ * Not load google analytics source multiple times (one for each middleware)
11
+ * Added option to skip google analytics source code
12
+
13
+ 0.1.7
14
+ -----
15
+
16
+ * Fixed bug with multiple middlewares (like newrelic 3.2.0) and different response styles (Array or String)
17
+
18
+ 0.1.6
19
+ -----
20
+
21
+ * Added support for multiple tests with the same goal
22
+
23
+ 0.1.5
24
+ -----
25
+
26
+ * Added support for rails 3
27
+
28
+
29
+ 0.1.3
30
+ -----
31
+
32
+ * Added "ignored organic" features
33
+
34
+
35
+ (Unreleased)
36
+ ------------
37
+
38
+ * FIX Bacchanalytics should be able to set the domain name
39
+
40
+ 0.1.2
41
+ -----
42
+
43
+ * Added WebSite Optimizer tracking features
44
+
45
+ 0.1.1
46
+ ----
47
+
48
+ * Basic testing added (rack/test and nokogiri are used for testing).
49
+
50
+ 0.1.0
51
+ ----
52
+
53
+ * Rack middleware that simply inserts the GATC.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [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.
@@ -0,0 +1,94 @@
1
+ # Bacchanalytics
2
+
3
+ Bacchanalytics is a rack middleware that inserts the Asynchronous Google Analytics
4
+ Tracking Code in your application.
5
+
6
+ ## <a name="installation">Installation</a>
7
+ gem install bacchanalytics
8
+
9
+ ## <a name="ci">Continuous Integration</a>
10
+ [![Build Status](https://secure.travis-ci.org/aspgems/bacchanalytics.png)](http://travis-ci.org/aspgems/bacchanalytics)
11
+
12
+ ## Usage in a Rails Application
13
+
14
+ Add the following line in your config/environment.rb:
15
+
16
+ ````ruby
17
+ config.middleware.use "Bacchanalytics::Analytics", :web_property_id => "UA-XXXXX-X"
18
+ ````
19
+
20
+ You have to replace "UA-XXXXX-X" with the Google Analytics property ID of your site.
21
+ You can find more information on the Google Analytics property ID in
22
+ http://code.google.com/apis/analytics/docs/concepts/gaConceptsAccounts.html#accountID
23
+
24
+ You are able to set the domain name of your site.
25
+
26
+ ````ruby
27
+ config.middleware.use "Bacchanalytics::Analytics", :web_property_id => "UA-XXXXX-X"
28
+ :domain_name => ".facturagem.com"
29
+ ````
30
+
31
+
32
+ You can also use "ignored organic" features in your pages with:
33
+
34
+ ````ruby
35
+ config.middleware.use "Bacchanalytics::Analytics", :web_property_id => "UA-XXXXXXX-X",
36
+ :ignored_organic => ["yoursite",
37
+ "yoursite.com",
38
+ "www.yoursite.com",
39
+ "http://www.yoursite.com",
40
+ "http://yoursite.com"]
41
+ ````
42
+
43
+
44
+
45
+ ## Google Website Optimizer
46
+
47
+ In order to use A/B tests, just add the following lines to your config/environment.rb:
48
+
49
+ ````ruby
50
+ config.middleware.use "Bacchanalytics::WebsiteOptimizer", :account_id => 'UA-XXXXXXXX-X',
51
+ :ab => { 'NNNNNNNNNN' => {:a => ["/", "/public/index"],
52
+ :b => "/home2",
53
+ :goal => "/public/signup"}
54
+ }
55
+ ````
56
+
57
+ where 'UA-XXXXXXXX-X' is the ID of the Website Optimizer account and 'NNNNNNNNNN' is the track page id, a key provided in the Website Optimizer javascript. The 'ab' hash contains the A (origin) pages, the B (variations) pages and the goal pages.
58
+
59
+ The embeded javascript looks like:
60
+
61
+ ````javascript
62
+ <!-- Google Website Optimizer Tracking Script -->
63
+ <script type="text/javascript">
64
+ var _gaq = _gaq || [];
65
+ _gaq.push(['gwo._setAccount', 'UA-XXXXXXXX-X']);
66
+ _gaq.push(['gwo._trackPageview', '/3203696384/test']);
67
+ (function() {
68
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
69
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
70
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
71
+ })();
72
+ </script>
73
+ <!-- End of Google Website Optimizer Tracking Script -->
74
+ ````
75
+
76
+ As you can see, the 'NNNNNNNNNN' is the '3203696384' track page ID.
77
+
78
+
79
+ ## Google Adwords Conversions
80
+
81
+ ````ruby
82
+ config.middleware.use "Bacchanalytics::AdwordsConversion", [
83
+ {:id => 'NNNNNNNNNN',
84
+ :label => 'oIiDCJe__QEQqcrF-gM',
85
+ :language => 'es',
86
+ :format => 3,
87
+ :value => 3.5,
88
+ :description => '',
89
+ :pages=>[ "/welcome", "/en/welcome", "/es/welcome", "/ca/welcome", "/eu/welcome"]
90
+ }
91
+ ]
92
+ ````
93
+
94
+ Copyright (c) 2011 ASPgems, released under the MIT license
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'appraisal'
4
+
5
+ require 'rake'
6
+ require 'rake/testtask'
7
+
8
+ desc 'Default: run unit tests.'
9
+ task :default => :test
10
+
11
+ desc 'Test the bacchanalytics gem'
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.libs << 'lib'
14
+ t.libs << 'test'
15
+ t.pattern = 'test/**/*_test.rb'
16
+ t.verbose = true
17
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/bacchanalytics/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["David Pérez", "Paco Guzman", "Javier Vidal", "ASPgems developers"]
6
+ gem.email = ["dperez@aspgems.com", "fjguzman@aspgems.com", "javier.vidal@aspgems.com", "developers@aspgems.com"]
7
+ gem.description = %q{Bacchanalytics is a rack middleware that inserts the Asynchronous Google Analytics
8
+ Tracking Code in your application.}
9
+ gem.summary = %q{Bacchanalytics is a rack middleware that inserts the Asynchronous Google Analytics
10
+ Tracking Code in your application.}
11
+ gem.homepage = "https://github.com/aspgems/bacchanalytics"
12
+
13
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14
+ gem.files = `git ls-files`.split("\n")
15
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ gem.name = "bacchanalytics"
17
+ gem.require_paths = ["lib"]
18
+ gem.version = Bacchanalytics::VERSION
19
+
20
+ gem.add_runtime_dependency "rack", ">= 1.1"
21
+ gem.add_runtime_dependency "activesupport", ">= 2"
22
+
23
+ gem.add_development_dependency "appraisal", "~> 0.3.8"
24
+ gem.add_development_dependency "rack-test", "~> 0.6"
25
+ gem.add_development_dependency "nokogiri", "~> 1.5"
26
+ gem.add_development_dependency "rake", "~> 0.9"
27
+ end
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rack", "~> 1.1.0"
6
+ gem "activesupport", "~> 2.3"
7
+
8
+ gemspec :path=>"../"
@@ -0,0 +1,31 @@
1
+ PATH
2
+ remote: /home/pacoguzman/dev/github_aspgems/bacchanalytics
3
+ specs:
4
+ bacchanalytics (0.1.9)
5
+ activesupport (>= 2)
6
+ rack (>= 1.1)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activesupport (2.3.14)
12
+ appraisal (0.3.8)
13
+ bundler
14
+ rake
15
+ nokogiri (1.5.0)
16
+ rack (1.1.2)
17
+ rack-test (0.6.1)
18
+ rack (>= 1.0)
19
+ rake (0.9.2.2)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ activesupport (~> 2.3)
26
+ appraisal (~> 0.3.8)
27
+ bacchanalytics!
28
+ nokogiri (~> 1.5)
29
+ rack (~> 1.1.0)
30
+ rack-test (~> 0.6)
31
+ rake (~> 0.9)
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rack", "~> 1.2.1"
6
+ gem "activesupport", "~> 3.0"
7
+
8
+ gemspec :path=>"../"
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: /home/pacoguzman/dev/github_aspgems/bacchanalytics
3
+ specs:
4
+ bacchanalytics (0.1.9)
5
+ activesupport (>= 2)
6
+ rack (>= 1.1)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activesupport (3.1.1)
12
+ multi_json (~> 1.0)
13
+ appraisal (0.3.8)
14
+ bundler
15
+ rake
16
+ multi_json (1.0.3)
17
+ nokogiri (1.5.0)
18
+ rack (1.2.4)
19
+ rack-test (0.6.1)
20
+ rack (>= 1.0)
21
+ rake (0.9.2.2)
22
+
23
+ PLATFORMS
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ activesupport (~> 3.0)
28
+ appraisal (~> 0.3.8)
29
+ bacchanalytics!
30
+ nokogiri (~> 1.5)
31
+ rack (~> 1.2.1)
32
+ rack-test (~> 0.6)
33
+ rake (~> 0.9)
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rack", "~> 1.3.2"
6
+ gem "activesupport", "~> 3.1"
7
+
8
+ gemspec :path=>"../"
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: /home/pacoguzman/dev/github_aspgems/bacchanalytics
3
+ specs:
4
+ bacchanalytics (0.1.9)
5
+ activesupport (>= 2)
6
+ rack (>= 1.1)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activesupport (3.1.1)
12
+ multi_json (~> 1.0)
13
+ appraisal (0.3.8)
14
+ bundler
15
+ rake
16
+ multi_json (1.0.3)
17
+ nokogiri (1.5.0)
18
+ rack (1.3.5)
19
+ rack-test (0.6.1)
20
+ rack (>= 1.0)
21
+ rake (0.9.2.2)
22
+
23
+ PLATFORMS
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ activesupport (~> 3.1)
28
+ appraisal (~> 0.3.8)
29
+ bacchanalytics!
30
+ nokogiri (~> 1.5)
31
+ rack (~> 1.3.2)
32
+ rack-test (~> 0.6)
33
+ rake (~> 0.9)
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "bacchanalytics"
@@ -0,0 +1 @@
1
+ require "bacchanalytics"
@@ -0,0 +1,5 @@
1
+ require "bacchanalytics/version"
2
+ require "bacchanalytics/google_analytics"
3
+ require "bacchanalytics/analytics"
4
+ require "bacchanalytics/adwords_conversion"
5
+ require "bacchanalytics/website_optimizer"
@@ -0,0 +1,95 @@
1
+ module Bacchanalytics
2
+
3
+ module AdwordsConversionTrackingCode
4
+
5
+ # Construct the Adwords conversion tracking code.
6
+ def adwords_tracking_code(page, conversions=[])
7
+ return if page.blank?
8
+
9
+ tracking_code = ""
10
+ conversions.each do |conversion|
11
+ begin
12
+ next unless valid_conversion?(conversion)
13
+
14
+ # Check the requested page, to include the A, B or goal tracking code.
15
+ if conversion[:pages].include?(page)
16
+ cid = conversion[:id]
17
+ label = conversion[:label]
18
+
19
+ description = conversion[:description] || 'adwords conversion'
20
+ language = conversion[:language] || 'en'
21
+ format = conversion[:format] || 3
22
+ value = conversion[:value] || 0
23
+
24
+ tracking_code = conversion_code(cid, label, language, format, value, description)
25
+ break
26
+ end
27
+ rescue
28
+ tracking_code = ""
29
+ end
30
+ end
31
+ tracking_code
32
+ end
33
+
34
+ private
35
+
36
+ def valid_conversion?(conversion)
37
+ !(conversion[:id].blank? || conversion[:label].blank? || conversion[:pages].empty?)
38
+ end
39
+
40
+ def conversion_code(cid, label, language, format, value, description)
41
+ <<-SCRIPT
42
+ <!-- Google Code for #{description} Conversion Page -->
43
+ <script type="text/javascript">
44
+ /* <![CDATA[ */
45
+ var google_conversion_id = #{cid};
46
+ var google_conversion_language = "#{language}";
47
+ var google_conversion_format = "#{format}";
48
+ var google_conversion_color = "ffffff";
49
+ var google_conversion_label = "#{label}";
50
+ var google_conversion_value = 0;
51
+ if (#{value}) {
52
+ google_conversion_value = #{value};
53
+ }
54
+ /* ]]> */
55
+ </script>
56
+ <script type="text/javascript" src="https://www.googleadservices.com/pagead/conversion.js">
57
+ </script>
58
+ <noscript>
59
+ <div style="display:inline;">
60
+ <img height="1" width="1" style="border-style:none;" alt="" src="https://www.googleadservices.com/pagead/conversion/#{cid}/?value=#{value}&amp;label=#{label}&amp;guid=ON&amp;script=0"/>
61
+ </div>
62
+ </noscript>
63
+ SCRIPT
64
+ end
65
+ end
66
+
67
+
68
+ class AdwordsConversion
69
+ include GoogleAnalytics::Base
70
+ include AdwordsConversionTrackingCode
71
+
72
+ def initialize(app, conversions = [])
73
+ @app = app
74
+ @conversions = conversions
75
+ end
76
+
77
+ def call(env)
78
+ status, headers, response = @app.call(env)
79
+
80
+ if should_instrument?(headers) && (source = response_source(response))
81
+ page = env['REQUEST_URI']
82
+ page.gsub!(/\?.*/, '') if page #remove url parameters
83
+
84
+ tracking_code = adwords_tracking_code(page, @conversions)
85
+ return [status, headers, response] if tracking_code.to_s == ""
86
+
87
+ new_body = source.sub /<\/[bB][oO][dY][yY]\s*>/, "#{tracking_code}\n</body>"
88
+ headers["Content-Length"] = new_body.length.to_s
89
+ Rack::Response.new(new_body, status, headers).finish
90
+ else
91
+ [status, headers, response]
92
+ end
93
+ end
94
+ end
95
+ end