ab-experiments-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +26 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +92 -0
  5. data/Rakefile +34 -0
  6. data/ab_experiments_rails.gemspec +23 -0
  7. data/app/views/ab_experiments_rails/_example.html.erb +17 -0
  8. data/app/views/ab_experiments_rails/_header_content.html.erb +52 -0
  9. data/lib/ab_experiments_rails.rb +6 -0
  10. data/lib/ab_experiments_rails/settings.rb +35 -0
  11. data/lib/ab_experiments_rails/version.rb +3 -0
  12. data/lib/generators/ab_experiments_rails_generator.rb +9 -0
  13. data/lib/tasks/ab_experiments_rails_tasks.rake +4 -0
  14. data/test/ab_experiments_rails_test.rb +7 -0
  15. data/test/dummy/README.rdoc +28 -0
  16. data/test/dummy/Rakefile +6 -0
  17. data/test/dummy/app/assets/javascripts/application.js +14 -0
  18. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  19. data/test/dummy/app/controllers/application_controller.rb +5 -0
  20. data/test/dummy/app/controllers/pages_controller.rb +4 -0
  21. data/test/dummy/app/helpers/application_helper.rb +2 -0
  22. data/test/dummy/app/views/ab_experiments_rails/_header_content.html.erb +52 -0
  23. data/test/dummy/app/views/layouts/application.html.erb +17 -0
  24. data/test/dummy/app/views/pages/landing_page.html.erb +23 -0
  25. data/test/dummy/bin/bundle +3 -0
  26. data/test/dummy/bin/rails +4 -0
  27. data/test/dummy/bin/rake +4 -0
  28. data/test/dummy/bin/setup +29 -0
  29. data/test/dummy/config.ru +4 -0
  30. data/test/dummy/config/application.rb +26 -0
  31. data/test/dummy/config/boot.rb +5 -0
  32. data/test/dummy/config/database.yml +25 -0
  33. data/test/dummy/config/environment.rb +5 -0
  34. data/test/dummy/config/environments/development.rb +41 -0
  35. data/test/dummy/config/environments/production.rb +79 -0
  36. data/test/dummy/config/environments/test.rb +42 -0
  37. data/test/dummy/config/initializers/assets.rb +11 -0
  38. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  39. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  40. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  41. data/test/dummy/config/initializers/inflections.rb +16 -0
  42. data/test/dummy/config/initializers/mime_types.rb +4 -0
  43. data/test/dummy/config/initializers/session_store.rb +3 -0
  44. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  45. data/test/dummy/config/locales/en.yml +23 -0
  46. data/test/dummy/config/routes.rb +57 -0
  47. data/test/dummy/config/secrets.yml +22 -0
  48. data/test/dummy/db/development.sqlite3 +0 -0
  49. data/test/dummy/db/schema.rb +16 -0
  50. data/test/dummy/db/test.sqlite3 +0 -0
  51. data/test/dummy/public/404.html +67 -0
  52. data/test/dummy/public/422.html +67 -0
  53. data/test/dummy/public/500.html +66 -0
  54. data/test/dummy/public/favicon.ico +0 -0
  55. data/test/dummy/public/js_mocks/cx_api_mock.js +7 -0
  56. data/test/dummy/public/js_mocks/cx_api_mock_variant.js +7 -0
  57. data/test/landing_page_test.rb +52 -0
  58. data/test/test_helper.rb +29 -0
  59. metadata +189 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 88e2708c8ce521f4e13827e5e0a2867d7588ca6f
4
+ data.tar.gz: b2d2bec56ea3e8ce3a95a1d99548e0d82098680f
5
+ SHA512:
6
+ metadata.gz: 2cf30639a2b23579e2d716511fbad119f2ffdff62725303ba39e7472409959c43d6ec58ee06edc56bf278c234a8ca2ef2cb0f32de150681eac20b9ad158e8407
7
+ data.tar.gz: fbcaf5f00622ab95f81babd62f95019338de2687c82b84ed6ae29b535cfade604810b4b5f8e296d6d609be2fbb3693add1dccba32acd49b4ab2745b54edea296
data/Gemfile ADDED
@@ -0,0 +1,26 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Declare your gem's dependencies in ab_experiments_rails.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ # To use a debugger
14
+ # gem 'byebug', group: [:development, :test]
15
+
16
+ group :development, :test do
17
+ gem 'jquery-rails', '~> 4.0.3'
18
+ end
19
+
20
+ group :test do
21
+ gem 'capybara', '2.12.0'
22
+ gem 'minitest-rails-capybara'
23
+ gem 'pry'
24
+ gem 'selenium-webdriver', '2.53.4'
25
+ gem 'mocha'
26
+ end
@@ -0,0 +1,20 @@
1
+ Copyright 2017
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,92 @@
1
+ The ab-experiments-rails is wrap for [google ab experiments](https://developers.google.com/analytics/solutions/experiments-client-side). Work with partials instead of javascript fighting.
2
+
3
+ ## Instalation
4
+ Add gem to your gemfile.
5
+ ```
6
+ gem 'ab-experiments-rails', git: 'https://github.com/hlidacky/ab-experiments-rails'
7
+ ```
8
+ Run bundle.
9
+ ```
10
+ bundle
11
+ ```
12
+ Run generator.
13
+ ```
14
+ rails generate ab_experiments_rails
15
+ ```
16
+ Put this content into head tag before google analytics load. (usually application layout file)
17
+ ```
18
+ <%# load before google analytics %>
19
+ <%= yield :google_ab %>
20
+ ```
21
+
22
+ ## Create your first ab test
23
+
24
+ Let's apply ab testing on file like this app/views/pages/landing_page.html.erb
25
+
26
+ ```html
27
+ <h1>Welcome and buy our services</h1>
28
+ <button>
29
+ Registration...
30
+ </button>
31
+ ```
32
+
33
+ Copy and paste this file app/views/ab_experiments_rails/_example.html.erb
34
+ into landing_page.html.erb and modify it like this
35
+
36
+
37
+ ```ruby
38
+ <%
39
+ settings = AbExperimentsRails::Settings.new({
40
+ test_name: :landing_page_test, # your choice
41
+ experiment_id: 'EXPERIMENT_ID', # copy/paste from google ab experiments
42
+ experiment_enabled: true, # on/off your experiment. display only original content when false
43
+ show_delay: 0.3 # flickering prevention if google choose variant. you can increase or decrease this number (seconds)
44
+ })
45
+ %>
46
+ <div class="<%= "#{settings.original_class}" %> google_ab_show_later">
47
+ <h1>Welcome and buy our services</h1>
48
+ <button>
49
+ Registration...
50
+ </button>
51
+ </div>
52
+ <% if settings.experiment_enabled? %>
53
+ <div class="<%= "#{settings.variant_class} "%> google_ab_hidden">
54
+ <h1>Welcome and buy our services for half prices</h1>
55
+ <button>
56
+ Registration...
57
+ </button>
58
+ </div>
59
+ <%= render "ab_experiments_rails/header_content", settings: settings %>
60
+ <% end %>
61
+ ```
62
+
63
+ Don't forget setup experiment id from your google analytics.
64
+ Your ab test is done!
65
+
66
+ ## How to force display test variant
67
+
68
+ You can pass parameter into url to display test variant.
69
+
70
+ If url of your page is
71
+ ```
72
+ www.example.com/landing_page
73
+ ```
74
+
75
+ You can force display original
76
+ ```
77
+ www.example.com/landing_page?landing_page_test_ab=0
78
+ ```
79
+ or variant
80
+ ```
81
+ www.example.com/landing_page?landing_page_test_ab=1
82
+ ```
83
+ This is useful for development
84
+
85
+
86
+
87
+
88
+
89
+
90
+
91
+
92
+
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'AbExperimentsRails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,23 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ require "ab_experiments_rails"
4
+
5
+ # Describe your gem and declare its dependencies:
6
+ Gem::Specification.new do |s|
7
+ s.name = "ab-experiments-rails"
8
+ s.version = AbExperimentsRails::VERSION
9
+ s.authors = ["Ondrej Mudroncik"]
10
+ s.email = ["gems@hlidacky.cz"]
11
+ s.homepage = "https://github.com/hlidacky/ab-experiments-rails"
12
+ s.summary = "Google ab experiments wrap"
13
+ s.description = "The ab-experiments-rails is wrap for google ab experiments. Work with partials instead of javascript fighting."
14
+ s.license = "MIT"
15
+
16
+ s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md", "Gemfile", "ab_experiments_rails.gemspec"]
17
+ s.test_files = Dir["test/**/*"]
18
+
19
+ s.add_dependency "rails", "~> 4.2.7.1"
20
+
21
+ s.add_development_dependency "bundler"
22
+ s.add_development_dependency "sqlite3"
23
+ end
@@ -0,0 +1,17 @@
1
+ <%
2
+ settings = AbExperimentsRails::Settings.new({
3
+ test_name: :landing_page_test, # your choice
4
+ experiment_id: 'EXPERIMENT_ID', # copy/paste from google ab experiments
5
+ experiment_enabled: true, # on/off your experiment. display only original content when false
6
+ show_delay: 0.3 # flickering prevention if google choose variant. you can increase or decrease this number (seconds)
7
+ })
8
+ %>
9
+ <div class="<%= "#{settings.original_class}" %> google_ab_show_later">
10
+ <% # original content... %>
11
+ </div>
12
+ <% if settings.experiment_enabled? %>
13
+ <div class="<%= "#{settings.variant_class} "%> google_ab_hidden">
14
+ <% # variant content... %>
15
+ </div>
16
+ <%= render "ab_experiments_rails/header_content", settings: settings %>
17
+ <% end %>
@@ -0,0 +1,52 @@
1
+ <%# https://developers.google.com/analytics/solutions/experiments-client-side %>
2
+ <%# The easiest way to do this is to place the default tracking code for either ga.js or analytics.js after the JavaScript that loads the Content Experiments JavaScript client and calls the chooseVariation method. %>
3
+ <%# that means that google_ab needs to after javascript load but before analytics javascript %>
4
+
5
+ <% content_for :google_ab do %>
6
+ <style>
7
+ .google_ab_hidden {
8
+ display: none !important;
9
+ }
10
+ @keyframes google_ab_show_later_animation {
11
+ from {opacity: 0;}
12
+ to {opacity: 1;}
13
+ }
14
+ .google_ab_show_later {
15
+ opacity: 1;
16
+ animation-name: google_ab_show_later_animation;
17
+ animation-duration: <%= "#{settings.show_delay}s" %>;
18
+ animation-timing-function: steps(1);
19
+ -webkit-animation-timing-function: steps(1);
20
+ }
21
+ </style>
22
+
23
+ <script src="<%= settings.url %>"></script>
24
+ <script>
25
+ var chosenVariation = cxApi.chooseVariation();
26
+ </script>
27
+
28
+ <% # Force display original / variant %>
29
+ <% if params["#{settings.force_param_name}".to_sym].present? %>
30
+ <% if params["#{settings.force_param_name}".to_sym].to_s == '1' %>
31
+ <script>chosenVariation = 1</script>
32
+ <% else %>
33
+ <script>chosenVariation = 0</script>
34
+ <% end %>
35
+ <% end %>
36
+
37
+ <script>
38
+ var pageVariations = [
39
+ function() {
40
+ // Original is always visible
41
+ },
42
+ function() {
43
+ $('.<%= settings.original_class %>').hide();
44
+ $('.<%= settings.variant_class %>').removeClass('google_ab_hidden');
45
+ $('.<%= settings.variant_class %>').show();
46
+ }
47
+ ];
48
+ $(document).on('ready', function () {
49
+ pageVariations[chosenVariation].call();
50
+ });
51
+ </script>
52
+ <% end %>
@@ -0,0 +1,6 @@
1
+ # Maintain your gem's version:
2
+ require "ab_experiments_rails/version"
3
+ require 'ab_experiments_rails/settings'
4
+
5
+ module AbExperimentsRails
6
+ end
@@ -0,0 +1,35 @@
1
+ module AbExperimentsRails
2
+ class Settings
3
+ attr_accessor :show_delay
4
+ attr_accessor :test_name
5
+ attr_accessor :experiment_id
6
+ attr_accessor :experiment_enabled
7
+
8
+ def initialize(options = {})
9
+ self.show_delay = options[:show_delay]
10
+ self.test_name = options[:test_name]
11
+ self.experiment_id = options[:experiment_id]
12
+ self.experiment_enabled = options[:experiment_enabled]
13
+ end
14
+
15
+ def url
16
+ "//www.google-analytics.com/cx/api.js?experiment=#{experiment_id}"
17
+ end
18
+
19
+ def original_class
20
+ "#{test_name}_original"
21
+ end
22
+
23
+ def variant_class
24
+ "#{test_name}_variant"
25
+ end
26
+
27
+ def experiment_enabled?
28
+ experiment_enabled == true
29
+ end
30
+
31
+ def force_param_name
32
+ "#{test_name}_ab"
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module AbExperimentsRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ class AbExperimentsRailsGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('..', __FILE__)
3
+
4
+ def copy_header
5
+ copy_file '../../app/views/ab_experiments_rails/_header_content.html.erb', 'app/views/ab_experiments_rails/_header_content.html.erb'
6
+ copy_file '../../app/views/ab_experiments_rails/_example.html.erb', 'app/views/ab_experiments_rails/_example.html.erb'
7
+ end
8
+
9
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :ab_experiments_rails do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class AbExperimentsRailsTest < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, AbExperimentsRails
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,14 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require jquery
14
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,4 @@
1
+ class PagesController < ApplicationController
2
+ def landing_page
3
+ end
4
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,52 @@
1
+ <%# https://developers.google.com/analytics/solutions/experiments-client-side %>
2
+ <%# The easiest way to do this is to place the default tracking code for either ga.js or analytics.js after the JavaScript that loads the Content Experiments JavaScript client and calls the chooseVariation method. %>
3
+ <%# that means that google_ab needs to after javascript load but before analytics javascript %>
4
+
5
+ <% content_for :google_ab do %>
6
+ <style>
7
+ .google_ab_hidden {
8
+ display: none !important;
9
+ }
10
+ @keyframes google_ab_show_later_animation {
11
+ from {opacity: 0;}
12
+ to {opacity: 1;}
13
+ }
14
+ .google_ab_show_later {
15
+ opacity: 1;
16
+ animation-name: google_ab_show_later_animation;
17
+ animation-duration: <%= "#{settings.show_delay}s" %>;
18
+ animation-timing-function: steps(1);
19
+ -webkit-animation-timing-function: steps(1);
20
+ }
21
+ </style>
22
+
23
+ <script src="<%= settings.url %>"></script>
24
+ <script>
25
+ var chosenVariation = cxApi.chooseVariation();
26
+ </script>
27
+
28
+ <% # Force display original / variant %>
29
+ <% if params["#{settings.force_param_name}".to_sym].present? %>
30
+ <% if params["#{settings.force_param_name}".to_sym].to_s == '1' %>
31
+ <script>chosenVariation = 1</script>
32
+ <% else %>
33
+ <script>chosenVariation = 0</script>
34
+ <% end %>
35
+ <% end %>
36
+
37
+ <script>
38
+ var pageVariations = [
39
+ function() {
40
+ // Original is always visible
41
+ },
42
+ function() {
43
+ $('.<%= settings.original_class %>').hide();
44
+ $('.<%= settings.variant_class %>').removeClass('google_ab_hidden');
45
+ $('.<%= settings.variant_class %>').show();
46
+ }
47
+ ];
48
+ $(document).on('ready', function () {
49
+ pageVariations[chosenVariation].call();
50
+ });
51
+ </script>
52
+ <% end %>