How to implement Google Analytics content experiment in Rails

166 views Asked by At

Google Analytics content experiments require using an original URL and a variation URL. In Rails, does this mean we need to create a new route and controller action? Or, can we use the same action to render different views based on URL parameters?

1

There are 1 answers

0
Vladan Markovic On

I believe you can just route multiple paths to same action like:

  match 'test_ctrl/test_action_1' => 'test_ctrl#test_action', as: :test_action_1
  match 'test_ctrl/test_action_2' => 'test_ctrl#test_action', as: :test_action_2
  match 'test_ctrl/test_action_3' => 'test_ctrl#test_action', as: :test_action_3

Then inside TestCtrl controller you can check request data and render specific view depending of request url with:

def test_action
  if request.path_info == 'test_ctrl/test_action_1'
    render "/test_action/1"
  elsif request.path_info == 'test_ctrl/test_action_2'
    render "/test_action/2"
  elsif request.path_info == 'test_ctrl/test_action_3'
    render "/test_action/3"
  end
end

And just create views 1,2 and 3 in views/test_action/