Module: RailsOnboarding::AbTestable

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/rails_onboarding/ab_testable.rb

Overview

Concern for A/B testing capabilities on User models Provides methods to assign users to test variants and track their performance

Examples:

Include in your User model

class User < ApplicationRecord
  include RailsOnboarding::Onboardable
  include RailsOnboarding::AbTestable
end

Configure A/B tests

RailsOnboarding.configure do |config|
  config.ab_tests = {
    onboarding_flow: {
      variants: ['original', 'simplified', 'gamified'],
      weights: [50, 25, 25], # Percentage distribution
      enabled: true
    }
  }
end

Instance Method Summary collapse

Instance Method Details

#ab_test_variant(test_name) ⇒ String?

Get the variant for a specific A/B test

Examples:

user.ab_test_variant(:onboarding_flow)
# => "simplified"

Parameters:

  • test_name (String, Symbol)

    Name of the A/B test

Returns:

  • (String, nil)

    The assigned variant name or nil if not assigned



43
44
45
46
47
# File 'app/models/concerns/rails_onboarding/ab_testable.rb', line 43

def ab_test_variant(test_name)
  return nil unless ab_test_assignments.is_a?(Hash)

  ab_test_assignments[test_name.to_s]
end

#all_ab_test_assignmentsHash

Get all A/B test assignments for this user

Returns:

  • (Hash)

    Hash of test_name => variant_name



79
80
81
# File 'app/models/concerns/rails_onboarding/ab_testable.rb', line 79

def all_ab_test_assignments
  ab_test_assignments || {}
end

#assign_variant(test_name, variant_name) ⇒ Boolean

Assign a specific variant to the user (useful for manual assignment)

Examples:

user.assign_variant(:onboarding_flow, :gamified)

Parameters:

  • test_name (String, Symbol)

    Name of the A/B test

  • variant_name (String, Symbol)

    Name of the variant to assign

Returns:

  • (Boolean)

    True if assignment was successful



70
71
72
73
74
# File 'app/models/concerns/rails_onboarding/ab_testable.rb', line 70

def assign_variant(test_name, variant_name)
  self.ab_test_assignments ||= {}
  self.ab_test_assignments[test_name.to_s] = variant_name.to_s
  save if persisted?
end

#in_variant?(test_name, variant_name) ⇒ Boolean

Check if user is in a specific variant

Examples:

user.in_variant?(:onboarding_flow, :simplified)
# => true

Parameters:

  • test_name (String, Symbol)

    Name of the A/B test

  • variant_name (String, Symbol)

    Name of the variant to check

Returns:

  • (Boolean)

    True if user is in the specified variant



58
59
60
# File 'app/models/concerns/rails_onboarding/ab_testable.rb', line 58

def in_variant?(test_name, variant_name)
  ab_test_variant(test_name) == variant_name.to_s
end

#track_ab_conversion(test_name, event_name, metadata = {}) ⇒ Object

Track a conversion for an A/B test

Examples:

user.track_ab_conversion(:onboarding_flow, :completed, { time_spent: 300 })

Parameters:

  • test_name (String, Symbol)

    Name of the A/B test

  • event_name (String, Symbol)

    Name of the conversion event

  • metadata (Hash) (defaults to: {})

    Additional metadata to track



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/concerns/rails_onboarding/ab_testable.rb', line 91

def track_ab_conversion(test_name, event_name,  = {})
  variant = ab_test_variant(test_name)
  return unless variant

  # Track using analytics system if available
  if respond_to?(:track_analytics_event)
    track_analytics_event(
      "ab_test_conversion",
      {
        test_name: test_name.to_s,
        variant: variant,
        event_name: event_name.to_s
      }.merge()
    )
  end
end