vanityはRailsのABテスト用ライブラリ。導入が一番簡単っぽい。
元々はテキストとか画像をのABテストを行うためのもののようだが、
chankoという限定公開用のライブラリを使って、機能単位でもABテストができるようにしてみる。
まずはGemfileに
- gem 'chanko', :git => 'git://github.com/cookpad/chanko.git'
- gem "vanity"
gem 'chanko', :git => 'git://github.com/cookpad/chanko.git'
gem "vanity"
を記述して、
# bundle install
それぞれに必要な初期設定をする。
# rails generate chanko:install
# rails generate vanity
# rake db:migrate
config/vanity.ymlを作成
- development:
- adapter: active_record
- active_record_adapter: mysql2
- host: localhost
- database: DBNAME
- user: USERNAME
- password: PASSWORD
development:
adapter: active_record
active_record_adapter: mysql2
host: localhost
database: DBNAME
user: USERNAME
password: PASSWORD
development.rbに以下を記述
- Vanity.playground.collecting = true
Vanity.playground.collecting = true
vanityのDashboard用コントローラを作成
- class VanityController < ApplicationController
- include Vanity::Rails::Dashboard
- end
class VanityController < ApplicationController
include Vanity::Rails::Dashboard
end
ルーティングを設定
- match '/vanity(/:action(/:id(.:format)))', :controller=>:vanity
match '/vanity(/:action(/:id(.:format)))', :controller=>:vanity
測定用のユーザアイデンティティを設定。
- class ApplicationController < ActionController::Base
- use_vanity :current_user
- end
class ApplicationController < ActionController::Base
use_vanity :current_user
end
Railsのルートディレクトリ下にテストと測定用のファイルを作成
#mkdir -p experiments/metrics
postした回数測定用のファイルを作成
experiments/metrics/post.rb
- metric "Post" do
- description "Postした回数"
- end
metric "Post" do
description "Postした回数"
end
コントローラの好きな場所に、
track! :post
を入力すれば、その場所がよばれた回数を測定できるようになります。
- class PostController < ApplicationController
- def create
- track! :post
-
- end
- end
- end
class PostController < ApplicationController
def create
track! :post
# ...投稿する処理
end
end
end
テスト用ファイルを作成
experiments/post_labels.rb
- ab_test "Post labels" do
- description "テスト"
- alternatives true, false
- metrics :post
- end
ab_test "Post labels" do
description "テスト"
alternatives true, false
metrics :post
end
次にchanko用設定
# rails generate chanko sample
- module Sample
- include Chanko::Unit
-
- active_if do |context, options|
- ab_test(:price_options)
- end
- scope(:controller) do
- function(:controller_show) do
-
- end
- end
- scope(:view) do
- function(:view_show) do
- render :partial => "/show"
- end
- end
- end
module Sample
include Chanko::Unit
active_if do |context, options|
ab_test(:price_options)
end
scope(:controller) do
function(:controller_show) do
# controller code here
end
end
scope(:view) do
function(:view_show) do
render :partial => "/show"
end
end
end
で、好きな場所にinvoke(:sample, :controller_show) をおいたり、
invoke(:sample, :show)
を置けばOK。
chankoの詳しい使い方は
こちらを参考に。
どちらが優位かなんてのは、Vanityが表示してくるので、
評価しやすいはず。
production環境でうまく動かなかったので、
そのときはredisによる接続をためしてみるといいかも。
- production:
- adapter: redis
- host: localhost
production:
adapter: redis
host: localhost
参考:
http://eccyan.hatenablog.com/entry/2011/12/08/223603
https://github.com/assaf/vanity
http://webandy.com/articles/a-b-testing-with-vanity