Class: AmplitudeExperiment::ExposureService
- Inherits:
-
Object
- Object
- AmplitudeExperiment::ExposureService
- Defined in:
- lib/experiment/local/exposure/exposure_service.rb
Overview
ExposureService
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(amplitude, exposure_filter) ⇒ ExposureService
constructor
A new instance of ExposureService.
- #track(exposure) ⇒ Object
Constructor Details
#initialize(amplitude, exposure_filter) ⇒ ExposureService
Returns a new instance of ExposureService.
5 6 7 8 |
# File 'lib/experiment/local/exposure/exposure_service.rb', line 5 def initialize(amplitude, exposure_filter) @amplitude = amplitude @exposure_filter = exposure_filter end |
Class Method Details
.to_exposure_events(exposure, ttl_millis) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 |
# File 'lib/experiment/local/exposure/exposure_service.rb', line 19 def self.to_exposure_events(exposure, ttl_millis) events = [] canonicalized = exposure.canonicalize exposure.results.each do |flag_key, variant| track_exposure = variant. ? variant..fetch('trackExposure', true) : true next unless track_exposure # Skip default variant exposures is_default = variant. ? variant..fetch('default', false) : false next if is_default # Determine user properties to set and unset. set_props = {} unset_props = {} flag_type = variant.['flagType'] if variant. if flag_type != 'mutual-exclusion-group' if variant.key set_props["[Experiment] #{flag_key}"] = variant.key elsif variant.value set_props["[Experiment] #{flag_key}"] = variant.value end end # Build event properties. event_properties = {} event_properties['[Experiment] Flag Key'] = flag_key if variant.key event_properties['[Experiment] Variant'] = variant.key elsif variant.value event_properties['[Experiment] Variant'] = variant.value end event_properties['metadata'] = variant. if variant. # Build event. event = AmplitudeAnalytics::BaseEvent.new( '[Experiment] Exposure', user_id: exposure.user.user_id, device_id: exposure.user.device_id, event_properties: event_properties, user_properties: { '$set' => set_props, '$unset' => unset_props }, insert_id: "#{exposure.user.user_id} #{exposure.user.device_id} #{AmplitudeExperiment.hash_code("#{flag_key} #{canonicalized}")} #{exposure. / ttl_millis}" ) event.groups = exposure.user.groups if exposure.user.groups events << event end events end |
Instance Method Details
#track(exposure) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/experiment/local/exposure/exposure_service.rb', line 10 def track(exposure) return unless @exposure_filter.should_track(exposure) events = ExposureService.to_exposure_events(exposure, @exposure_filter.ttl_millis) events.each do |event| @amplitude.track(event) end end |