|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# |
| 4 | +# Copyright 2019, Optimizely and contributors |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +require_relative 'entity/event_batch' |
| 19 | +require_relative 'entity/conversion_event' |
| 20 | +require_relative 'entity/decision' |
| 21 | +require_relative 'entity/impression_event' |
| 22 | +require_relative 'entity/snapshot' |
| 23 | +require_relative 'entity/snapshot_event' |
| 24 | +require_relative 'entity/visitor' |
| 25 | +require 'optimizely/helpers/validator' |
| 26 | +module Optimizely |
| 27 | + class EventFactory |
| 28 | + # EventFactory builds LogEvent objects from a given user_event. |
| 29 | + class << self |
| 30 | + CUSTOM_ATTRIBUTE_FEATURE_TYPE = 'custom' |
| 31 | + ENDPOINT = 'https://logx.optimizely.com/v1/events' |
| 32 | + POST_HEADERS = {'Content-Type' => 'application/json'}.freeze |
| 33 | + ACTIVATE_EVENT_KEY = 'campaign_activated' |
| 34 | + |
| 35 | + def create_log_event(user_events, logger) |
| 36 | + @logger = logger |
| 37 | + builder = Optimizely::EventBatch::Builder.new |
| 38 | + |
| 39 | + user_events = [user_events] unless user_events.is_a? Array |
| 40 | + |
| 41 | + visitors = [] |
| 42 | + user_context = nil |
| 43 | + user_events.each do |user_event| |
| 44 | + if user_event.is_a? Optimizely::ImpressionEvent |
| 45 | + visitor = create_impression_event_visitor(user_event) |
| 46 | + visitors.push(visitor) |
| 47 | + elsif user_event.is_a? Optimizely::ConversionEvent |
| 48 | + visitor = create_conversion_event_visitor(user_event) |
| 49 | + visitors.push(visitor) |
| 50 | + else |
| 51 | + @logger.log(Logger::WARN, 'invalid UserEvent added in a list.') |
| 52 | + next |
| 53 | + end |
| 54 | + user_context = user_event.event_context |
| 55 | + end |
| 56 | + |
| 57 | + return nil if visitors.empty? |
| 58 | + |
| 59 | + builder.with_account_id(user_context[:account_id]) |
| 60 | + builder.with_project_id(user_context[:project_id]) |
| 61 | + builder.with_client_version(user_context[:client_version]) |
| 62 | + builder.with_revision(user_context[:revision]) |
| 63 | + builder.with_client_name(user_context[:client_name]) |
| 64 | + builder.with_anonymize_ip(user_context[:anonymize_ip]) |
| 65 | + builder.with_enrich_decisions(true) |
| 66 | + |
| 67 | + builder.with_visitors(visitors) |
| 68 | + event_batch = builder.build |
| 69 | + Event.new(:post, ENDPOINT, event_batch.as_json, POST_HEADERS) |
| 70 | + end |
| 71 | + |
| 72 | + def build_attribute_list(user_attributes, project_config) |
| 73 | + visitor_attributes = [] |
| 74 | + user_attributes&.keys&.each do |attribute_key| |
| 75 | + # Omit attribute values that are not supported by the log endpoint. |
| 76 | + attribute_value = user_attributes[attribute_key] |
| 77 | + next unless Helpers::Validator.attribute_valid?(attribute_key, attribute_value) |
| 78 | + |
| 79 | + attribute_id = project_config.get_attribute_id attribute_key |
| 80 | + next if attribute_id.nil? |
| 81 | + |
| 82 | + visitor_attributes.push( |
| 83 | + entity_id: attribute_id, |
| 84 | + key: attribute_key, |
| 85 | + type: CUSTOM_ATTRIBUTE_FEATURE_TYPE, |
| 86 | + value: attribute_value |
| 87 | + ) |
| 88 | + end |
| 89 | + |
| 90 | + return unless Helpers::Validator.boolean? project_config.bot_filtering |
| 91 | + |
| 92 | + # Append Bot Filtering Attribute |
| 93 | + visitor_attributes.push( |
| 94 | + entity_id: Optimizely::Helpers::Constants::CONTROL_ATTRIBUTES['BOT_FILTERING'], |
| 95 | + key: Optimizely::Helpers::Constants::CONTROL_ATTRIBUTES['BOT_FILTERING'], |
| 96 | + type: CUSTOM_ATTRIBUTE_FEATURE_TYPE, |
| 97 | + value: project_config.bot_filtering |
| 98 | + ) |
| 99 | + end |
| 100 | + |
| 101 | + private |
| 102 | + |
| 103 | + def create_impression_event_visitor(impression_event) |
| 104 | + decision = Optimizely::Decision.new( |
| 105 | + campaign_id: impression_event.experiment_layer_id, |
| 106 | + experiment_id: impression_event.experiment_id, |
| 107 | + variation_id: impression_event.variation_id |
| 108 | + ) |
| 109 | + |
| 110 | + snapshot_event = Optimizely::SnapshotEvent.new( |
| 111 | + entity_id: impression_event.experiment_layer_id, |
| 112 | + timestamp: impression_event.timestamp, |
| 113 | + uuid: impression_event.uuid, |
| 114 | + key: ACTIVATE_EVENT_KEY |
| 115 | + ) |
| 116 | + |
| 117 | + snapshot = Optimizely::Snapshot.new( |
| 118 | + events: [snapshot_event.as_json], |
| 119 | + decisions: [decision.as_json] |
| 120 | + ) |
| 121 | + |
| 122 | + visitor = Optimizely::Visitor.new( |
| 123 | + snapshots: [snapshot.as_json], |
| 124 | + visitor_id: impression_event.user_id, |
| 125 | + attributes: impression_event.visitor_attributes |
| 126 | + ) |
| 127 | + visitor.as_json |
| 128 | + end |
| 129 | + |
| 130 | + def create_conversion_event_visitor(conversion_event) |
| 131 | + revenue_value = Helpers::EventTagUtils.get_revenue_value(conversion_event.tags, @logger) |
| 132 | + numeric_value = Helpers::EventTagUtils.get_numeric_value(conversion_event.tags, @logger) |
| 133 | + snapshot_event = Optimizely::SnapshotEvent.new( |
| 134 | + entity_id: conversion_event.event['id'], |
| 135 | + timestamp: conversion_event.timestamp, |
| 136 | + uuid: conversion_event.uuid, |
| 137 | + key: conversion_event.event['key'], |
| 138 | + revenue: revenue_value, |
| 139 | + value: numeric_value, |
| 140 | + tags: conversion_event.tags |
| 141 | + ) |
| 142 | + |
| 143 | + snapshot = Optimizely::Snapshot.new(events: [snapshot_event.as_json]) |
| 144 | + |
| 145 | + visitor = Optimizely::Visitor.new( |
| 146 | + snapshots: [snapshot.as_json], |
| 147 | + visitor_id: conversion_event.user_id, |
| 148 | + attributes: conversion_event.visitor_attributes |
| 149 | + ) |
| 150 | + visitor.as_json |
| 151 | + end |
| 152 | + end |
| 153 | + end |
| 154 | +end |
0 commit comments