diff --git a/lib/optimizely/config/datafile_project_config.rb b/lib/optimizely/config/datafile_project_config.rb index 1f03171d..14e5b168 100644 --- a/lib/optimizely/config/datafile_project_config.rb +++ b/lib/optimizely/config/datafile_project_config.rb @@ -32,7 +32,7 @@ class DatafileProjectConfig < ProjectConfig :group_id_map, :rollout_id_map, :rollout_experiment_id_map, :variation_id_map, :variation_id_to_variable_usage_map, :variation_key_map, :variation_id_map_by_experiment_id, :variation_key_map_by_experiment_id, :flag_variation_map, :integration_key_map, :integrations, - :public_key_for_odp, :host_for_odp, :all_segments + :public_key_for_odp, :host_for_odp, :all_segments, :region # Boolean - denotes if Optimizely should remove the last block of visitors' IP address before storing event data attr_reader :anonymize_ip @@ -68,6 +68,10 @@ def initialize(datafile, logger, error_handler) @rollouts = config.fetch('rollouts', []) @send_flag_decisions = config.fetch('sendFlagDecisions', false) @integrations = config.fetch('integrations', []) + @region = config.fetch('region', 'US') + + # Default to US region if not specified + @region = 'US' if @region.nil? || @region.empty? # Json type is represented in datafile as a subtype of string for the sake of backwards compatibility. # Converting it to a first-class json type while creating Project Config diff --git a/lib/optimizely/event/entity/event_batch.rb b/lib/optimizely/event/entity/event_batch.rb index 70aca415..d49d85a8 100644 --- a/lib/optimizely/event/entity/event_batch.rb +++ b/lib/optimizely/event/entity/event_batch.rb @@ -18,7 +18,7 @@ module Optimizely class EventBatch attr_accessor :account_id, :project_id, :revision, :client_name, :client_version, - :anonymize_ip, :enrich_decisions, :visitors + :anonymize_ip, :enrich_decisions, :visitors, :region def as_json { @@ -29,13 +29,14 @@ def as_json client_version: @client_version, anonymize_ip: @anonymize_ip, enrich_decisions: @enrich_decisions, - visitors: @visitors + visitors: @visitors, + region: @region } end class Builder attr_reader :account_id, :project_id, :revision, :client_name, :client_version, - :anonymize_ip, :enrich_decisions, :visitors + :anonymize_ip, :enrich_decisions, :visitors, :region def build event_batch = EventBatch.new @@ -47,6 +48,7 @@ def build event_batch.anonymize_ip = @anonymize_ip event_batch.enrich_decisions = @enrich_decisions event_batch.visitors = @visitors + event_batch.region = @region || 'US' event_batch end @@ -62,6 +64,10 @@ def with_revision(revision) @revision = revision end + def with_region(region) + @region = region || 'US' + end + def with_client_name(client_name) @client_name = client_name end diff --git a/lib/optimizely/event/entity/event_context.rb b/lib/optimizely/event/entity/event_context.rb index 65f8f18e..f26b30a2 100644 --- a/lib/optimizely/event/entity/event_context.rb +++ b/lib/optimizely/event/entity/event_context.rb @@ -26,7 +26,8 @@ def initialize( anonymize_ip:, revision:, client_name:, - client_version: + client_version:, + region: ) @account_id = account_id @project_id = project_id @@ -34,6 +35,7 @@ def initialize( @revision = revision @client_name = client_name @client_version = client_version + @region = region end def as_json @@ -43,7 +45,8 @@ def as_json anonymize_ip: @anonymize_ip, revision: @revision, client_name: @client_name, - client_version: @client_version + client_version: @client_version, + region: @region } end end diff --git a/lib/optimizely/event/event_factory.rb b/lib/optimizely/event/event_factory.rb index 9ac8a937..b85b8951 100644 --- a/lib/optimizely/event/event_factory.rb +++ b/lib/optimizely/event/event_factory.rb @@ -28,7 +28,10 @@ class EventFactory # EventFactory builds LogEvent objects from a given user_event. class << self CUSTOM_ATTRIBUTE_FEATURE_TYPE = 'custom' - ENDPOINT = 'https://logx.optimizely.com/v1/events' + ENDPOINTS = { + US: 'https://logx.optimizely.com/v1/events', + EU: 'https://eu.logx.optimizely.com/v1/events' + }.freeze POST_HEADERS = {'Content-Type' => 'application/json'}.freeze ACTIVATE_EVENT_KEY = 'campaign_activated' @@ -64,10 +67,14 @@ def create_log_event(user_events, logger) builder.with_client_name(user_context[:client_name]) builder.with_anonymize_ip(user_context[:anonymize_ip]) builder.with_enrich_decisions(true) + builder.with_region(user_context[:region]) builder.with_visitors(visitors) event_batch = builder.build - Event.new(:post, ENDPOINT, event_batch.as_json, POST_HEADERS) + + endpoint = ENDPOINTS[user_context[:region].to_s.upcase.to_sym] || ENDPOINTS[:US] + + Event.new(:post, endpoint, event_batch.as_json, POST_HEADERS) end def build_attribute_list(user_attributes, project_config) diff --git a/lib/optimizely/event/user_event_factory.rb b/lib/optimizely/event/user_event_factory.rb index f7852341..872a70b8 100644 --- a/lib/optimizely/event/user_event_factory.rb +++ b/lib/optimizely/event/user_event_factory.rb @@ -33,6 +33,7 @@ def self.create_impression_event(project_config, experiment, variation_id, metad # # Returns Event encapsulating the impression event. event_context = Optimizely::EventContext.new( + region: project_config.region, account_id: project_config.account_id, project_id: project_config.project_id, anonymize_ip: project_config.anonymize_ip, @@ -67,6 +68,7 @@ def self.create_conversion_event(project_config, event, user_id, user_attributes # Returns Event encapsulating the conversion event. event_context = Optimizely::EventContext.new( + region: project_config.region, account_id: project_config.account_id, project_id: project_config.project_id, anonymize_ip: project_config.anonymize_ip, diff --git a/lib/optimizely/event_builder.rb b/lib/optimizely/event_builder.rb index 4c743cc3..a5ee82a9 100644 --- a/lib/optimizely/event_builder.rb +++ b/lib/optimizely/event_builder.rb @@ -101,13 +101,17 @@ def get_common_params(project_config, user_id, attributes) revision: project_config.revision, client_name: CLIENT_ENGINE, enrich_decisions: true, - client_version: VERSION + client_version: VERSION, + region: project_config.region || 'US' } end end class EventBuilder < BaseEventBuilder - ENDPOINT = 'https://logx.optimizely.com/v1/events' + ENDPOINTS = { + US: 'https://logx.optimizely.com/v1/events', + EU: 'https://eu.logx.optimizely.com/v1/events' + }.freeze POST_HEADERS = {'Content-Type' => 'application/json'}.freeze ACTIVATE_EVENT_KEY = 'campaign_activated' @@ -122,11 +126,14 @@ def create_impression_event(project_config, experiment, variation_id, user_id, a # # Returns +Event+ encapsulating the impression event. + region = project_config.region || 'US' event_params = get_common_params(project_config, user_id, attributes) impression_params = get_impression_params(project_config, experiment, variation_id) event_params[:visitors][0][:snapshots].push(impression_params) - Event.new(:post, ENDPOINT, event_params, POST_HEADERS) + endpoint = ENDPOINTS[region.to_s.upcase.to_sym] + + Event.new(:post, endpoint, event_params, POST_HEADERS) end def create_conversion_event(project_config, event, user_id, attributes, event_tags) @@ -140,11 +147,14 @@ def create_conversion_event(project_config, event, user_id, attributes, event_ta # # Returns +Event+ encapsulating the conversion event. + region = project_config.region || 'US' event_params = get_common_params(project_config, user_id, attributes) conversion_params = get_conversion_params(event, event_tags) event_params[:visitors][0][:snapshots] = [conversion_params] - Event.new(:post, ENDPOINT, event_params, POST_HEADERS) + endpoint = ENDPOINTS[region.to_s.upcase.to_sym] + + Event.new(:post, endpoint, event_params, POST_HEADERS) end private diff --git a/lib/optimizely/project_config.rb b/lib/optimizely/project_config.rb index 43e86441..b5094b62 100644 --- a/lib/optimizely/project_config.rb +++ b/lib/optimizely/project_config.rb @@ -62,6 +62,8 @@ def host_for_odp; end def all_segments; end + def region; end + def experiment_running?(experiment); end def get_experiment_from_key(experiment_key); end diff --git a/spec/config/datafile_project_config_spec.rb b/spec/config/datafile_project_config_spec.rb index 362141d6..74c264b3 100644 --- a/spec/config/datafile_project_config_spec.rb +++ b/spec/config/datafile_project_config_spec.rb @@ -57,6 +57,7 @@ expect(project_config.sdk_key).to eq(config_body['sdkKey']) expect(project_config.environment_key).to eq(config_body['environmentKey']) expect(project_config.send_flag_decisions).to eq(config_body['sendFlagDecisions']) + expect(project_config.region).to eq(config_body['region']) expected_attribute_key_map = { 'browser_type' => config_body['attributes'][0], @@ -756,6 +757,23 @@ expect(project_config.rollout_experiment_id_map).to eq(expected_rollout_experiment_id_map) end + it 'should use US region when no region is specified in datafile' do + project_config = Optimizely::DatafileProjectConfig.new(config_body_JSON, logger, error_handler) + expect(project_config.region).to eq('US') + end + + it 'should parse region specified in datafile correctly' do + project_config_us = Optimizely::DatafileProjectConfig.new(config_body_JSON, logger, error_handler) + expect(project_config_us.region).to eq('US') + + config_body_eu = config_body.dup + config_body_eu['region'] = 'EU' + config_body_json = JSON.dump(config_body_eu) + project_config_eu = Optimizely::DatafileProjectConfig.new(config_body_json, logger, error_handler) + + expect(project_config_eu.region).to eq('EU') + end + it 'should initialize properties correctly upon creating project with typed audience dict' do project_config = Optimizely::DatafileProjectConfig.new(JSON.dump(OptimizelySpec::CONFIG_DICT_WITH_TYPED_AUDIENCES), logger, error_handler) config_body = OptimizelySpec::CONFIG_DICT_WITH_TYPED_AUDIENCES diff --git a/spec/event/event_entities_spec.rb b/spec/event/event_entities_spec.rb index 7d747ed2..47be87dc 100644 --- a/spec/event/event_entities_spec.rb +++ b/spec/event/event_entities_spec.rb @@ -69,7 +69,8 @@ revision: '42', client_name: Optimizely::CLIENT_ENGINE, enrich_decisions: true, - client_version: Optimizely::VERSION + client_version: Optimizely::VERSION, + region: 'US' } @expected_conversion_payload = { @@ -103,7 +104,8 @@ revision: '42', client_name: Optimizely::CLIENT_ENGINE, enrich_decisions: true, - client_version: Optimizely::VERSION + client_version: Optimizely::VERSION, + region: 'US' } end diff --git a/spec/event/event_factory_spec.rb b/spec/event/event_factory_spec.rb index b92661be..dbf4b175 100644 --- a/spec/event/event_factory_spec.rb +++ b/spec/event/event_factory_spec.rb @@ -34,7 +34,10 @@ allow(Time).to receive(:now).and_return(time_now) allow(SecureRandom).to receive(:uuid).and_return('a68cf1ad-0393-4e18-af87-efe8f01a7c9c') - @expected_endpoint = 'https://logx.optimizely.com/v1/events' + @expected_endpoints = { + US: 'https://logx.optimizely.com/v1/events', + EU: 'https://eu.logx.optimizely.com/v1/events' + } @expected_impression_params = { account_id: '12001', project_id: '111001', @@ -70,7 +73,8 @@ revision: '42', client_name: Optimizely::CLIENT_ENGINE, enrich_decisions: true, - client_version: Optimizely::VERSION + client_version: Optimizely::VERSION, + region: 'US' } @expected_conversion_params = { account_id: '12001', @@ -96,7 +100,8 @@ revision: '42', client_name: Optimizely::CLIENT_ENGINE, enrich_decisions: true, - client_version: Optimizely::VERSION + client_version: Optimizely::VERSION, + region: 'US' } end @@ -111,7 +116,7 @@ impression_event = Optimizely::UserEventFactory.create_impression_event(project_config, experiment, '111128', metadata, 'test_user', nil) log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -134,7 +139,7 @@ 'browser_type' => 'firefox') log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -184,7 +189,7 @@ impression_event = Optimizely::UserEventFactory.create_impression_event(project_config, experiment, '111128', metadata, 'test_user', attributes) log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -225,7 +230,7 @@ impression_event = Optimizely::UserEventFactory.create_impression_event(project_config, experiment, '111128', metadata, 'test_user', attributes) log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -248,7 +253,7 @@ 'browser_type' => false) log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -270,7 +275,7 @@ impression_event = Optimizely::UserEventFactory.create_impression_event(project_config, experiment, '111128', metadata, 'test_user', 'browser_type' => 0) log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -286,7 +291,7 @@ invalid_attribute: 'sorry_not_sorry') log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -294,7 +299,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, nil) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -309,7 +314,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', {'browser_type' => 'firefox'}, nil) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -322,7 +327,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -335,7 +340,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -347,7 +352,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -359,7 +364,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -375,7 +380,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -390,7 +395,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -404,7 +409,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -418,7 +423,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -432,7 +437,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -447,7 +452,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -461,7 +466,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', nil, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -487,7 +492,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', {'browser_type' => 'firefox'}, event_tags) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -520,7 +525,7 @@ impression_event = Optimizely::UserEventFactory.create_impression_event(project_config, experiment, '111128', metadata, 'test_user', user_attributes) log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -556,7 +561,7 @@ impression_event = Optimizely::UserEventFactory.create_impression_event(project_config, experiment, '111128', metadata, 'test_user', user_attributes) log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -594,7 +599,7 @@ ) log_event = Optimizely::EventFactory.create_log_event(impression_event, spy_logger) expect(log_event.params).to eq(@expected_impression_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -620,7 +625,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', user_attributes, nil) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -642,7 +647,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', user_attributes, nil) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end @@ -671,7 +676,7 @@ conversion_event = Optimizely::UserEventFactory.create_conversion_event(project_config, event, 'test_user', user_attributes, nil) log_event = Optimizely::EventFactory.create_log_event(conversion_event, spy_logger) expect(log_event.params).to eq(@expected_conversion_params) - expect(log_event.url).to eq(@expected_endpoint) + expect(log_event.url).to eq(@expected_endpoints[:US]) expect(log_event.http_verb).to eq(:post) end end diff --git a/spec/event/forwarding_event_processor_spec.rb b/spec/event/forwarding_event_processor_spec.rb index f58d0e90..582bac30 100644 --- a/spec/event/forwarding_event_processor_spec.rb +++ b/spec/event/forwarding_event_processor_spec.rb @@ -64,7 +64,8 @@ revision: '42', client_name: Optimizely::CLIENT_ENGINE, enrich_decisions: true, - client_version: Optimizely::VERSION + client_version: Optimizely::VERSION, + region: 'US' } end diff --git a/spec/event/user_event_factory_spec.rb b/spec/event/user_event_factory_spec.rb index f9876c23..f1ed533e 100644 --- a/spec/event/user_event_factory_spec.rb +++ b/spec/event/user_event_factory_spec.rb @@ -46,6 +46,7 @@ expect(impression_event.event_context[:project_id]).to eq(project_config.project_id) expect(impression_event.event_context[:revision]).to eq(project_config.revision) expect(impression_event.event_context[:anonymize_ip]).to eq(project_config.anonymize_ip) + expect(impression_event.event_context[:region]).to eq(project_config.region) expect(impression_event.bot_filtering).to eq(project_config.bot_filtering) expect(impression_event.experiment_id).to eq(experiment['id']) expect(impression_event.variation_id).to eq('111128') @@ -79,6 +80,7 @@ expect(impression_event.event_context[:project_id]).to eq(project_config.project_id) expect(impression_event.event_context[:revision]).to eq(project_config.revision) expect(impression_event.event_context[:anonymize_ip]).to eq(project_config.anonymize_ip) + expect(impression_event.event_context[:region]).to eq(project_config.region) expect(impression_event.bot_filtering).to eq(project_config.bot_filtering) expect(impression_event.experiment_id).to eq(experiment['id']) expect(impression_event.variation_id).to eq('111128') @@ -108,6 +110,7 @@ expect(conversion_event.event_context[:project_id]).to eq(project_config.project_id) expect(conversion_event.event_context[:revision]).to eq(project_config.revision) expect(conversion_event.event_context[:anonymize_ip]).to eq(project_config.anonymize_ip) + expect(conversion_event.event_context[:region]).to eq(project_config.region) expect(conversion_event.event['key']).to eq(event['key']) expect(conversion_event.bot_filtering).to eq(project_config.bot_filtering) expect(conversion_event.user_id).to eq('test_user') @@ -141,6 +144,7 @@ expect(conversion_event.event_context[:project_id]).to eq(project_config.project_id) expect(conversion_event.event_context[:revision]).to eq(project_config.revision) expect(conversion_event.event_context[:anonymize_ip]).to eq(project_config.anonymize_ip) + expect(conversion_event.event_context[:region]).to eq(project_config.region) expect(conversion_event.event['key']).to eq(event['key']) expect(conversion_event.bot_filtering).to eq(project_config.bot_filtering) expect(conversion_event.user_id).to eq('test_user') diff --git a/spec/event_builder_spec.rb b/spec/event_builder_spec.rb index 4201c579..62cf5ba1 100644 --- a/spec/event_builder_spec.rb +++ b/spec/event_builder_spec.rb @@ -39,7 +39,10 @@ allow(Time).to receive(:now).and_return(time_now) allow(SecureRandom).to receive(:uuid).and_return('a68cf1ad-0393-4e18-af87-efe8f01a7c9c') - @expected_endpoint = 'https://logx.optimizely.com/v1/events' + @expected_endpoints = { + US: 'https://logx.optimizely.com/v1/events', + EU: 'https://eu.logx.optimizely.com/v1/events' + } @expected_impression_params = { account_id: '12001', project_id: '111001', @@ -69,7 +72,8 @@ revision: '42', client_name: Optimizely::CLIENT_ENGINE, enrich_decisions: true, - client_version: Optimizely::VERSION + client_version: Optimizely::VERSION, + region: 'US' } @expected_conversion_params = { account_id: '12001', @@ -95,7 +99,8 @@ revision: '42', client_name: Optimizely::CLIENT_ENGINE, enrich_decisions: true, - client_version: Optimizely::VERSION + client_version: Optimizely::VERSION, + region: 'US' } end @@ -103,7 +108,7 @@ experiment = config.get_experiment_from_key('test_experiment') impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', nil) expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -119,7 +124,7 @@ impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', 'browser_type' => 'firefox') expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -161,7 +166,7 @@ } impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', attributes) expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -194,7 +199,7 @@ } impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', attributes) expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -210,7 +215,7 @@ impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', 'browser_type' => false) expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -225,7 +230,7 @@ experiment = config.get_experiment_from_key('test_experiment') impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', 'browser_type' => 0) expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -234,14 +239,14 @@ impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', invalid_attribute: 'sorry_not_sorry') expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end it 'should create a valid Event when create_conversion_event is called' do conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, nil) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -255,7 +260,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', {'browser_type' => 'firefox'}, nil) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -267,7 +272,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -279,7 +284,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -290,7 +295,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -301,7 +306,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -316,7 +321,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -330,7 +335,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -343,7 +348,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -356,7 +361,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -369,7 +374,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -383,7 +388,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -396,7 +401,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', nil, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -421,7 +426,7 @@ conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', {'browser_type' => 'firefox'}, event_tags) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -447,7 +452,7 @@ experiment = config.get_experiment_from_key('test_experiment') impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', user_attributes) expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -476,7 +481,7 @@ expect(config.send(:bot_filtering)).to eq(true) impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', user_attributes) expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -505,7 +510,7 @@ allow(config).to receive(:bot_filtering).and_return(false) impression_event = @event_builder.create_impression_event(config, experiment, '111128', 'test_user', user_attributes) expect(impression_event.params).to eq(@expected_impression_params) - expect(impression_event.url).to eq(@expected_endpoint) + expect(impression_event.url).to eq(@expected_endpoints[:US]) expect(impression_event.http_verb).to eq(:post) end @@ -530,7 +535,7 @@ } conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', user_attributes, nil) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -551,7 +556,7 @@ } conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', user_attributes, nil) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end @@ -579,7 +584,7 @@ allow(config).to receive(:bot_filtering).and_return(false) conversion_event = @event_builder.create_conversion_event(config, @event, 'test_user', user_attributes, nil) expect(conversion_event.params).to eq(@expected_conversion_params) - expect(conversion_event.url).to eq(@expected_endpoint) + expect(conversion_event.url).to eq(@expected_endpoints[:US]) expect(conversion_event.http_verb).to eq(:post) end end diff --git a/spec/optimizely_user_context_spec.rb b/spec/optimizely_user_context_spec.rb index 515068c0..b47550bc 100644 --- a/spec/optimizely_user_context_spec.rb +++ b/spec/optimizely_user_context_spec.rb @@ -224,7 +224,8 @@ type: 'custom', value: true }] - }] + }], + region: 'US' } stub_request(:post, impression_log_url) expect(forced_decision_project_instance.notification_center).to receive(:send_notifications) @@ -323,7 +324,8 @@ type: 'custom', value: true }] - }] + }], + region: 'US' } expect(forced_decision_project_instance.notification_center).to receive(:send_notifications) @@ -442,7 +444,8 @@ type: 'custom', value: true }] - }] + }], + region: 'US' } stub_request(:post, impression_log_url) expect(forced_decision_project_instance.notification_center).to receive(:send_notifications) diff --git a/spec/project_spec.rb b/spec/project_spec.rb index f857a5ce..e14c77ba 100644 --- a/spec/project_spec.rb +++ b/spec/project_spec.rb @@ -274,7 +274,8 @@ class InvalidErrorHandler; end # rubocop:disable Lint/ConstantDefinitionInBlock revision: '42', client_name: Optimizely::CLIENT_ENGINE, enrich_decisions: true, - client_version: Optimizely::VERSION + client_version: Optimizely::VERSION, + region: 'US' } end @@ -393,7 +394,8 @@ class InvalidErrorHandler; end # rubocop:disable Lint/ConstantDefinitionInBlock revision: '3', client_name: Optimizely::CLIENT_ENGINE, enrich_decisions: true, - client_version: Optimizely::VERSION + client_version: Optimizely::VERSION, + region: 'US' } end @@ -1079,7 +1081,8 @@ def callback(_args); end revision: '42', client_name: Optimizely::CLIENT_ENGINE, enrich_decisions: true, - client_version: Optimizely::VERSION + client_version: Optimizely::VERSION, + region: 'US' } end @@ -1251,7 +1254,8 @@ def callback(_args); end revision: '3', client_name: Optimizely::CLIENT_ENGINE, enrich_decisions: true, - client_version: Optimizely::VERSION + client_version: Optimizely::VERSION, + region: 'US' } end after(:example) do @@ -1615,7 +1619,8 @@ def callback(_args); end revision: '42', client_name: Optimizely::CLIENT_ENGINE, enrich_decisions: true, - client_version: Optimizely::VERSION + client_version: Optimizely::VERSION, + region: 'US' } end @@ -3867,7 +3872,8 @@ def callback(_args); end type: 'custom', value: true }] - }] + }], + region: 'US' } expect(project_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, impression_log_url, expected_params, post_headers)) end @@ -4024,7 +4030,8 @@ def callback(_args); end type: 'custom', value: true }] - }] + }], + region: 'US' } expect(project_instance.event_dispatcher).to have_received(:dispatch_event).with(Optimizely::Event.new(:post, impression_log_url, expected_params, post_headers)) end diff --git a/spec/spec_params.rb b/spec/spec_params.rb index e43ce3cc..8735218f 100644 --- a/spec/spec_params.rb +++ b/spec/spec_params.rb @@ -19,6 +19,7 @@ module OptimizelySpec VALID_CONFIG_BODY = { + 'region' => 'US', 'accountId' => '12001', 'projectId' => '111001', 'anonymizeIP' => false,