Class: Optimizely::EventFactory

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

Constant Summary collapse

CUSTOM_ATTRIBUTE_FEATURE_TYPE =
'custom'
ENDPOINT =
'https://logx.optimizely.com/v1/events'
POST_HEADERS =
{'Content-Type' => 'application/json'}.freeze
ACTIVATE_EVENT_KEY =
'campaign_activated'

Class Method Summary collapse

Class Method Details

.build_attribute_list(user_attributes, project_config) ⇒ Object



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

def build_attribute_list(user_attributes, project_config)
  visitor_attributes = []
  user_attributes&.each_key do |attribute_key|
    # Omit attribute values that are not supported by the log endpoint.
    attribute_value = user_attributes[attribute_key]
    next unless Helpers::Validator.attribute_valid?(attribute_key, attribute_value)

    attribute_id = project_config.get_attribute_id attribute_key
    next if attribute_id.nil?

    visitor_attributes.push(
      entity_id: attribute_id,
      key: attribute_key,
      type: CUSTOM_ATTRIBUTE_FEATURE_TYPE,
      value: attribute_value
    )
  end

  return visitor_attributes unless Helpers::Validator.boolean? project_config.bot_filtering

  # Append Bot Filtering Attribute
  visitor_attributes.push(
    entity_id: Optimizely::Helpers::Constants::CONTROL_ATTRIBUTES['BOT_FILTERING'],
    key: Optimizely::Helpers::Constants::CONTROL_ATTRIBUTES['BOT_FILTERING'],
    type: CUSTOM_ATTRIBUTE_FEATURE_TYPE,
    value: project_config.bot_filtering
  )
end

.create_log_event(user_events, logger) ⇒ Object



35
36
37
38
39
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
# File 'lib/optimizely/event/event_factory.rb', line 35

def create_log_event(user_events, logger)
  @logger = logger
  builder = Optimizely::EventBatch::Builder.new

  user_events = [user_events] unless user_events.is_a? Array

  visitors = []
  user_context = nil
  user_events.each do |user_event|
    case user_event
    when Optimizely::ImpressionEvent
      visitor = create_impression_event_visitor(user_event)
      visitors.push(visitor)
    when Optimizely::ConversionEvent
      visitor = create_conversion_event_visitor(user_event)
      visitors.push(visitor)
    else
      @logger.log(Logger::WARN, 'invalid UserEvent added in a list.')
      next
    end
    user_context = user_event.event_context
  end

  return nil if visitors.empty?

  builder.(user_context[:account_id])
  builder.with_project_id(user_context[:project_id])
  builder.with_client_version(user_context[:client_version])
  builder.with_revision(user_context[:revision])
  builder.with_client_name(user_context[:client_name])
  builder.with_anonymize_ip(user_context[:anonymize_ip])
  builder.with_enrich_decisions(true)

  builder.with_visitors(visitors)
  event_batch = builder.build
  Event.new(:post, ENDPOINT, event_batch.as_json, POST_HEADERS)
end