Module: Vanity::Rails::Helpers

Defined in:
lib/vanity/frameworks/rails.rb

Overview

Introduces ab_test helper (controllers and views). Similar to the generic ab_test method, with the ability to capture content (applicable to views, see examples).

Instance Method Summary (collapse)

Instance Method Details

- (Object) ab_test(name, &block)

This method returns one of the alternative values in the named A/B test.

Examples:

A/B two alternatives for a page

def index
  if ab_test(:new_page) # true/false test
    render action: "new_page"
  else
    render action: "index"
  end
end

Similar, alternative value is page name

def index
  render action: ab_test(:new_page)
end

A/B test inside ERB template (condition)

<%= if ab_test(:banner) %>100% less complexity!<% end %>

A/B test inside ERB template (value)

<%= ab_test(:greeting) %> <%= current_user.name %>

A/B test inside ERB template (capture)

<% ab_test :features do |count| %>
  <%= count %> features to choose from!
<% end %>


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/vanity/frameworks/rails.rb', line 181

def ab_test(name, &block)
  if Vanity.playground.using_js?
    @_vanity_experiments ||= {}
    @_vanity_experiments[name] ||= Vanity.playground.experiment(name).choose
    value = @_vanity_experiments[name].value
  else
    value = Vanity.playground.experiment(name).choose.value
  end
 
  if block
    content = capture(value, &block)
    block_called_from_erb?(block) ? concat(content) : content
  else
    value
  end
end

- (Object) vanity_h(text)



217
218
219
# File 'lib/vanity/frameworks/rails.rb', line 217

def vanity_h(text)
  h(text)
end

- (Object) vanity_html_safe(text)



221
222
223
224
225
226
227
# File 'lib/vanity/frameworks/rails.rb', line 221

def vanity_html_safe(text)
  if text.respond_to?(:html_safe)
    text.html_safe
  else
    text
  end
end

- (Object) vanity_js



210
211
212
213
214
215
# File 'lib/vanity/frameworks/rails.rb', line 210

def vanity_js
  return if @_vanity_experiments.nil?
  javascript_tag do
    render Vanity.template("vanity.js.erb")
  end
end

- (Object) vanity_simple_format(text, html_options = {})



229
230
231
# File 'lib/vanity/frameworks/rails.rb', line 229

def vanity_simple_format(text, html_options={})
  vanity_html_safe(simple_format(text, html_options))
end

- (Object) vanity_track_url_for(identity, metric, options = {})

Generate url with the identity of the current user and the metric to track on click



199
200
201
202
# File 'lib/vanity/frameworks/rails.rb', line 199

def vanity_track_url_for(identity, metric, options = {})
  options = options.merge(:_identity => identity, :_track => metric)
  url_for(options)
end

- (Object) vanity_tracking_image(identity, metric, options = {})

Generate url with the fingerprint for the current Vanity experiment



205
206
207
208
# File 'lib/vanity/frameworks/rails.rb', line 205

def vanity_tracking_image(identity, metric, options = {})
  options = options.merge(:controller => :vanity, :action => :image, :_identity => identity, :_track => metric)
  image_tag(url_for(options), :width => "1px", :height => "1px", :alt => "")
end