Class: Optimizely::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/optimizely.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datafile, event_dispatcher = nil, logger = nil, error_handler = nil) ⇒ Project

Returns a new instance of Project.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/optimizely.rb', line 21

def initialize(datafile, event_dispatcher = nil, logger = nil, error_handler = nil)
  # Constructor for Projects.
  #
  # datafile - JSON string representing the project.
  # event_dispatcher - Provides a dispatch_event method which if given a URL and params sends a request to it.
  # logger - Optional param which provides a log method to log messages. By default nothing would be logged.
  # error_handler - Optional param which provides a handle_error method to handle exceptions.
  #                 By default all exceptions will be suppressed.

  @logger = logger || NoOpLogger.new
  @error_handler = error_handler || NoOpErrorHandler.new
  @event_dispatcher = event_dispatcher || EventDispatcher.new
  validate_inputs(datafile)

  @config = ProjectConfig.new(datafile, @logger, @error_handler)
  @bucketer = Bucketer.new(@config)
  @event_builder = EventBuilder.new(@config, @bucketer)
end

Instance Attribute Details

#bucketerObject

Returns the value of attribute bucketer.



15
16
17
# File 'lib/optimizely.rb', line 15

def bucketer
  @bucketer
end

#configObject

Returns the value of attribute config.



14
15
16
# File 'lib/optimizely.rb', line 14

def config
  @config
end

#error_handlerObject

Returns the value of attribute error_handler.



19
20
21
# File 'lib/optimizely.rb', line 19

def error_handler
  @error_handler
end

#event_builderObject

Returns the value of attribute event_builder.



16
17
18
# File 'lib/optimizely.rb', line 16

def event_builder
  @event_builder
end

#event_dispatcherObject

Returns the value of attribute event_dispatcher.



17
18
19
# File 'lib/optimizely.rb', line 17

def event_dispatcher
  @event_dispatcher
end

#loggerObject

Returns the value of attribute logger.



18
19
20
# File 'lib/optimizely.rb', line 18

def logger
  @logger
end

Instance Method Details

#activate(experiment_key, user_id, attributes = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/optimizely.rb', line 40

def activate(experiment_key, user_id, attributes = nil)
  # Buckets visitor and sends impression event to Optimizely.
  #
  # experiment_key - Experiment which needs to be activated.
  # user_id - String ID for user.
  # attributes - Hash representing user attributes and values to be recorded.
  #
  # Returns variation key representing the variation the user will be bucketed in.
  # Returns nil if experiment is not Running or if user is not in experiment.

  if attributes && !attributes_valid?(attributes)
    @logger.log(Logger::INFO, "Not activating user '#{user_id}'.")
    return nil
  end

  unless preconditions_valid?(experiment_key, user_id, attributes)
    @logger.log(Logger::INFO, "Not activating user '#{user_id}'.")
    return nil
  end

  variation_id = @bucketer.bucket(experiment_key, user_id)

  if not variation_id
    @logger.log(Logger::INFO, "Not activating user '#{user_id}'.")
    return nil
  end

  # Create and dispatch impression event
  impression_event = @event_builder.create_impression_event(experiment_key, variation_id, user_id, attributes)
  @logger.log(Logger::INFO,
              'Dispatching impression event to URL %s with params %s.' % [impression_event.url,
                                                                          impression_event.params])
  @event_dispatcher.dispatch_event(impression_event.url, impression_event.params)

  @config.get_variation_key_from_id(experiment_key, variation_id)
end

#get_variation(experiment_key, user_id, attributes = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/optimizely.rb', line 77

def get_variation(experiment_key, user_id, attributes = nil)
  # Gets variation where visitor will be bucketed.
  #
  # experiment_key - Experiment for which visitor variation needs to be determined.
  # user_id - String ID for user.
  # attributes - Hash representing user attributes.
  #
  # Returns variation key where visitor will be bucketed.
  # Returns nil if experiment is not Running or if user is not in experiment.

  if attributes && !attributes_valid?(attributes)
    @logger.log(Logger::INFO, "Not activating user '#{user_id}.")
    return nil
  end

  unless preconditions_valid?(experiment_key, user_id, attributes)
    @logger.log(Logger::INFO, "Not activating user '#{user_id}.")
    return nil
  end

  variation_id = @bucketer.bucket(experiment_key, user_id)
  @config.get_variation_key_from_id(experiment_key, variation_id)
end

#track(event_key, user_id, attributes = nil, event_value = nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/optimizely.rb', line 101

def track(event_key, user_id, attributes = nil, event_value = nil)
  # Send conversion event to Optimizely.
  #
  # event_key - Goal key representing the event which needs to be recorded.
  # user_id - String ID for user.
  # attributes - Hash representing visitor attributes and values which need to be recorded.
  # event_value - Value associated with the event. Can be used to represent revenue in cents.

  # Create and dispatch conversion event

  return nil if attributes && !attributes_valid?(attributes)

  experiment_ids = @config.get_experiment_ids_for_goal(event_key)
  if experiment_ids.empty?
    @config.logger.log(Logger::INFO, "Not tracking user '#{user_id}'.")
    return nil
  end

  # Filter out experiments that are not running or that do not include the user in audience conditions
  valid_experiment_keys = []
  experiment_ids.each do |experiment_id|
    experiment_key = @config.experiment_id_map[experiment_id]['key']
    unless preconditions_valid?(experiment_key, user_id, attributes)
      @config.logger.log(Logger::INFO, "Not tracking user '#{user_id}' for experiment '#{experiment_key}'.")
      next
    end
    valid_experiment_keys.push(experiment_key)
  end

  conversion_event = @event_builder.create_conversion_event(event_key, user_id, attributes,
                                                            event_value, valid_experiment_keys)
  @logger.log(Logger::INFO,
              'Dispatching conversion event to URL %s with params %s.' % [conversion_event.url,
                                                                          conversion_event.params])
  @event_dispatcher.dispatch_event(conversion_event.url, conversion_event.params)
end