field_test 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +103 -0
- data/Rakefile +10 -0
- data/app/models/field_test/membership.rb +9 -0
- data/field_test.gemspec +27 -0
- data/lib/field_test.rb +21 -0
- data/lib/field_test/engine.rb +4 -0
- data/lib/field_test/experiment.rb +94 -0
- data/lib/field_test/helpers.rb +54 -0
- data/lib/field_test/version.rb +3 -0
- data/lib/generators/field_test/install_generator.rb +31 -0
- data/lib/generators/field_test/templates/config.yml +7 -0
- data/lib/generators/field_test/templates/memberships.rb +14 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 21a7309f61348d5b03aa7a6073e2d7493dbc4f9a
|
4
|
+
data.tar.gz: 486affca9a9ade457fa2481925995bdc55b4a954
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b9c4d1fa745aa15f132cf8c74dcfd4f456864601c661139f96bef2b9cd17266b3a8c1dfcaec56d8b7fed21617e25d83c51ead8e73a7d7c59ae0ac057c5959eff
|
7
|
+
data.tar.gz: d971c7c1907456a6af2162738ad4473891b02c17fd4b9b48e316757b596291cc84ee6dee73016f573284113f73a0884f611e20ab2e053af99f4929ada7e0a361
|
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 Andrew Kane
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following 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 OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# Field Test
|
2
|
+
|
3
|
+
:maple_leaf: A/B testing for Rails
|
4
|
+
|
5
|
+
- Designed for web and email
|
6
|
+
- Seamlessly handles the transition from anonymous visitor to logged in user
|
7
|
+
- Results are stored in your database
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application’s Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'field_test'
|
15
|
+
```
|
16
|
+
|
17
|
+
And run:
|
18
|
+
|
19
|
+
```sh
|
20
|
+
rails g field_test:install
|
21
|
+
```
|
22
|
+
|
23
|
+
## Getting Started
|
24
|
+
|
25
|
+
Add an experiment to `config/field_test.yml`.
|
26
|
+
|
27
|
+
```yml
|
28
|
+
experiments:
|
29
|
+
button_color:
|
30
|
+
variants:
|
31
|
+
- control
|
32
|
+
- red
|
33
|
+
- green
|
34
|
+
```
|
35
|
+
|
36
|
+
Refer to it in views, controllers, and mailers.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
button_color = field_test(:button_color)
|
40
|
+
```
|
41
|
+
|
42
|
+
When someone converts, record it with:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
field_test_converted(:button_color)
|
46
|
+
```
|
47
|
+
|
48
|
+
Get the results with:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
experiment = FieldTest::Experiment.find(:button_color)
|
52
|
+
experiment.results
|
53
|
+
```
|
54
|
+
|
55
|
+
When an experiment is over, specify a winner:
|
56
|
+
|
57
|
+
```yml
|
58
|
+
experiments:
|
59
|
+
button_color:
|
60
|
+
winner: red
|
61
|
+
```
|
62
|
+
|
63
|
+
All calls to `field_test` will now return the winner.
|
64
|
+
|
65
|
+
## Features
|
66
|
+
|
67
|
+
You can specify a variant with query parameters to make testing easier
|
68
|
+
|
69
|
+
```
|
70
|
+
http://localhost:3000/?field_test[button_color]=red
|
71
|
+
```
|
72
|
+
|
73
|
+
For mailers, you need to specify a participant:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
field_test(:button_color, participant: "test@example.org")
|
77
|
+
```
|
78
|
+
|
79
|
+
## Funnels
|
80
|
+
|
81
|
+
For advanced funnels, we recommend an analytics platform like [Ahoy](https://github.com/ankane/ahoy) or [Mixpanel](https://mixpanel.com/).
|
82
|
+
|
83
|
+
You can pass experiments and variants as properties.
|
84
|
+
|
85
|
+
## TODO
|
86
|
+
|
87
|
+
- Add confidence to stats
|
88
|
+
- Add [Bayesian confidence](http://www.evanmiller.org/bayesian-ab-testing.html) to results
|
89
|
+
- Exclude bots
|
90
|
+
- User interface
|
91
|
+
|
92
|
+
## History
|
93
|
+
|
94
|
+
View the [changelog](https://github.com/ankane/field_test/blob/master/CHANGELOG.md)
|
95
|
+
|
96
|
+
## Contributing
|
97
|
+
|
98
|
+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
99
|
+
|
100
|
+
- [Report bugs](https://github.com/ankane/field_test/issues)
|
101
|
+
- Fix bugs and [submit pull requests](https://github.com/ankane/field_test/pulls)
|
102
|
+
- Write, clarify, or fix documentation
|
103
|
+
- Suggest or add new features
|
data/Rakefile
ADDED
data/field_test.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "field_test/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "field_test"
|
8
|
+
spec.version = FieldTest::VERSION
|
9
|
+
spec.authors = ["Andrew Kane"]
|
10
|
+
spec.email = ["andrew@chartkick.com"]
|
11
|
+
|
12
|
+
spec.summary = "A/B testing for Rails"
|
13
|
+
spec.homepage = "https://github.com/ankane/field_test"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "railties"
|
23
|
+
spec.add_dependency "activerecord"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler"
|
26
|
+
spec.add_development_dependency "rake"
|
27
|
+
end
|
data/lib/field_test.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "field_test/experiment"
|
2
|
+
require "field_test/engine"
|
3
|
+
require "field_test/helpers"
|
4
|
+
require "field_test/version"
|
5
|
+
|
6
|
+
module FieldTest
|
7
|
+
class Error < StandardError; end
|
8
|
+
class ExperimentNotFound < Error; end
|
9
|
+
end
|
10
|
+
|
11
|
+
ActiveSupport.on_load(:action_controller) do
|
12
|
+
include FieldTest::Helpers
|
13
|
+
end
|
14
|
+
|
15
|
+
ActiveSupport.on_load(:action_view) do
|
16
|
+
include FieldTest::Helpers
|
17
|
+
end
|
18
|
+
|
19
|
+
ActiveSupport.on_load(:action_mailer) do
|
20
|
+
include FieldTest::Helpers
|
21
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
module FieldTest
|
2
|
+
class Experiment
|
3
|
+
attr_reader :id, :variants, :winner
|
4
|
+
|
5
|
+
def initialize(attributes)
|
6
|
+
attributes = attributes.symbolize_keys
|
7
|
+
@id = attributes[:id]
|
8
|
+
@variants = attributes[:variants]
|
9
|
+
@winner = attributes[:winner]
|
10
|
+
end
|
11
|
+
|
12
|
+
def variant(participants, options = {})
|
13
|
+
return winner if winner
|
14
|
+
return variants.first if options[:exclude]
|
15
|
+
|
16
|
+
participants = standardize_participants(participants)
|
17
|
+
membership = membership_for(participants) || FieldTest::Membership.new(experiment: id)
|
18
|
+
|
19
|
+
if options[:variant] && variants.include?(options[:variant])
|
20
|
+
membership.variant = options[:variant]
|
21
|
+
else
|
22
|
+
membership.variant ||= variants.sample
|
23
|
+
end
|
24
|
+
|
25
|
+
# upgrade to preferred participant
|
26
|
+
membership.participant = participants.first
|
27
|
+
|
28
|
+
if membership.changed?
|
29
|
+
begin
|
30
|
+
membership.save!
|
31
|
+
rescue ActiveRecord::RecordNotUnique
|
32
|
+
membership = memberships.find_by(participant: participants.first)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
membership.try(:variant) || variants.first
|
37
|
+
end
|
38
|
+
|
39
|
+
def convert(participants)
|
40
|
+
participants = standardize_participants(participants)
|
41
|
+
membership = membership_for(participants)
|
42
|
+
|
43
|
+
if membership
|
44
|
+
membership.converted = true
|
45
|
+
membership.save! if membership.changed?
|
46
|
+
true
|
47
|
+
else
|
48
|
+
false
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def memberships
|
53
|
+
FieldTest::Membership.where(experiment: id)
|
54
|
+
end
|
55
|
+
|
56
|
+
def results
|
57
|
+
data = memberships.group(:variant).group(:converted).count
|
58
|
+
results = {}
|
59
|
+
variants.each do |variant|
|
60
|
+
converted = data[[variant, true]].to_i
|
61
|
+
participated = converted + data[[variant, false]].to_i
|
62
|
+
results[variant] = {
|
63
|
+
participated: participated,
|
64
|
+
converted: converted,
|
65
|
+
conversion_rate: converted.to_f / participated
|
66
|
+
}
|
67
|
+
end
|
68
|
+
results
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.find(id)
|
72
|
+
# reload in dev
|
73
|
+
@config = nil if Rails.env.development?
|
74
|
+
|
75
|
+
@config ||= YAML.load(ERB.new(File.read("config/field_test.yml")).result)
|
76
|
+
|
77
|
+
settings = @config["experiments"][id.to_s]
|
78
|
+
raise FieldTest::ExperimentNotFound unless settings
|
79
|
+
|
80
|
+
FieldTest::Experiment.new(settings.merge(id: id.to_s))
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def membership_for(participants)
|
86
|
+
memberships = self.memberships.where(participant: participants).index_by(&:participant)
|
87
|
+
participants.map { |part| memberships[part] }.compact.first
|
88
|
+
end
|
89
|
+
|
90
|
+
def standardize_participants(participants)
|
91
|
+
Array(participants).map { |v| v.respond_to?(:model_name) ? "#{v.model_name.name}:#{v.id}" : v.to_s }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module FieldTest
|
2
|
+
module Helpers
|
3
|
+
def field_test(experiment, options = {})
|
4
|
+
exp = FieldTest::Experiment.find(experiment)
|
5
|
+
|
6
|
+
participants = field_test_participants(options)
|
7
|
+
|
8
|
+
if try(:request)
|
9
|
+
if params[:field_test] && params[:field_test][experiment]
|
10
|
+
options[:variant] ||= params[:field_test][experiment]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# cache results for request
|
15
|
+
@field_test_cache ||= {}
|
16
|
+
@field_test_cache[experiment] ||= exp.variant(participants, options)
|
17
|
+
end
|
18
|
+
|
19
|
+
def field_test_converted(experiment, options = {})
|
20
|
+
exp = FieldTest::Experiment.find(experiment)
|
21
|
+
|
22
|
+
participants = field_test_participants(options)
|
23
|
+
|
24
|
+
exp.convert(participants)
|
25
|
+
end
|
26
|
+
|
27
|
+
def field_test_participants(options = {})
|
28
|
+
participants = []
|
29
|
+
|
30
|
+
if options[:participant]
|
31
|
+
participants << options[:participant]
|
32
|
+
else
|
33
|
+
if respond_to?(:current_user, true) && current_user
|
34
|
+
participants << current_user
|
35
|
+
end
|
36
|
+
|
37
|
+
if try(:request)
|
38
|
+
# use cookie
|
39
|
+
cookie_key = "field_test"
|
40
|
+
token = cookies[cookie_key]
|
41
|
+
if participants.empty? && !token
|
42
|
+
token = SecureRandom.uuid
|
43
|
+
cookies[cookie_key] = {value: token, expires: 30.days.from_now}
|
44
|
+
end
|
45
|
+
if token
|
46
|
+
participants << "cookie:#{token.gsub(/[^a-z0-9\-]/i, "")}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
participants
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "rails/generators"
|
2
|
+
require "rails/generators/migration"
|
3
|
+
require "active_record"
|
4
|
+
require "rails/generators/active_record"
|
5
|
+
|
6
|
+
module FieldTest
|
7
|
+
module Generators
|
8
|
+
class InstallGenerator < Rails::Generators::Base
|
9
|
+
include Rails::Generators::Migration
|
10
|
+
source_root File.expand_path("../templates", __FILE__)
|
11
|
+
|
12
|
+
# Implement the required interface for Rails::Generators::Migration.
|
13
|
+
def self.next_migration_number(dirname) #:nodoc:
|
14
|
+
next_migration_number = current_migration_number(dirname) + 1
|
15
|
+
if ::ActiveRecord::Base.timestamped_migrations
|
16
|
+
[Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
|
17
|
+
else
|
18
|
+
"%.3d" % next_migration_number
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def copy_migration
|
23
|
+
migration_template "memberships.rb", "db/migrate/create_field_test_memberships.rb"
|
24
|
+
end
|
25
|
+
|
26
|
+
def copy_config
|
27
|
+
template "config.yml", "config/field_test.yml"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :field_test_memberships do |t|
|
4
|
+
t.string :participant
|
5
|
+
t.string :experiment
|
6
|
+
t.string :variant
|
7
|
+
t.timestamp :created_at
|
8
|
+
t.boolean :converted, default: false
|
9
|
+
end
|
10
|
+
|
11
|
+
add_index :field_test_memberships, [:experiment, :participant], unique: true
|
12
|
+
add_index :field_test_memberships, :participant
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: field_test
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Kane
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- andrew@chartkick.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- CHANGELOG.md
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- app/models/field_test/membership.rb
|
83
|
+
- field_test.gemspec
|
84
|
+
- lib/field_test.rb
|
85
|
+
- lib/field_test/engine.rb
|
86
|
+
- lib/field_test/experiment.rb
|
87
|
+
- lib/field_test/helpers.rb
|
88
|
+
- lib/field_test/version.rb
|
89
|
+
- lib/generators/field_test/install_generator.rb
|
90
|
+
- lib/generators/field_test/templates/config.yml
|
91
|
+
- lib/generators/field_test/templates/memberships.rb
|
92
|
+
homepage: https://github.com/ankane/field_test
|
93
|
+
licenses: []
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.5.1
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: A/B testing for Rails
|
115
|
+
test_files: []
|