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

#ab_test(name, &block) ⇒ Object

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 %>

221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/vanity/frameworks/rails.rb', line 221

def ab_test(name, &block)
  current_request = respond_to?(:request) ? request : nil
  value = Vanity.ab_test(name, current_request)

  if block
    content = capture(value, &block)
    if defined?(block_called_from_erb?) && block_called_from_erb?(block)
      concat(content)
    else
      content
    end
  else
    value
  end
end

#vanity_experimentsObject

Return a copy of the active experiments on a page

Examples:

Render some info about each active experiment in development mode

<% if Rails.env.development? %>
  <% vanity_experiments.each do |name, alternative| %>
    <span>Participating in <%= name %>, seeing <%= alternative %>:<%= alternative.value %> </span>
  <% end %>
<% end %>

Push experiment values into javascript for use there

<% experiments = vanity_experiments %>
<% unless experiments.empty? %>
  <script>
    <% experiments.each do |name, alternative| %>
      myAbTests.<%= name.to_s.camelize(:lower) %> = '<%= alternative.value %>';
    <% end %>
  </script>
<% end %>

290
291
292
293
294
295
296
297
298
# File 'lib/vanity/frameworks/rails.rb', line 290

def vanity_experiments
  edit_safe_experiments = {}

  Vanity.context.vanity_active_experiments.each do |name, alternative|
    edit_safe_experiments[name] = alternative.clone
  end

  edit_safe_experiments
end

#vanity_h(text) ⇒ Object


257
258
259
# File 'lib/vanity/frameworks/rails.rb', line 257

def vanity_h(text)
  h(text)
end

#vanity_html_safe(text) ⇒ Object


261
262
263
264
265
266
267
# File 'lib/vanity/frameworks/rails.rb', line 261

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

#vanity_jsObject


249
250
251
252
253
254
255
# File 'lib/vanity/frameworks/rails.rb', line 249

def vanity_js
  return if Vanity.context.vanity_active_experiments.nil? || Vanity.context.vanity_active_experiments.empty?

  javascript_tag do
    render file: Vanity.template("_vanity.js.erb"), formats: [:js]
  end
end

#vanity_simple_format(text, html_options = {}) ⇒ Object


269
270
271
# File 'lib/vanity/frameworks/rails.rb', line 269

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

#vanity_track_url_for(identity, metric, options = {}) ⇒ Object

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


238
239
240
241
# File 'lib/vanity/frameworks/rails.rb', line 238

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

#vanity_tracking_image(identity, metric, options = {}) ⇒ Object

Generate url with the fingerprint for the current Vanity experiment


244
245
246
247
# File 'lib/vanity/frameworks/rails.rb', line 244

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