Class: MixpanelTesting::MixpanelProvider
- Inherits:
-
Object
- Object
- MixpanelTesting::MixpanelProvider
- Defined in:
- lib/mixpaneltesting/mixpanel.rb
Instance Method Summary collapse
- #event_complex_query(event, extra_query) ⇒ Object
- #events_segmentation(events) ⇒ Object
-
#initialize(session_id) ⇒ MixpanelProvider
constructor
A new instance of MixpanelProvider.
- #validate_complex_query(event, extra_query, expected) ⇒ Object
- #validate_events(expected_results) ⇒ Object
Constructor Details
#initialize(session_id) ⇒ MixpanelProvider
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/mixpaneltesting/mixpanel.rb', line 10 def initialize(session_id) @log = Logger.new(STDOUT) @log.info "Login at Mixpanel: #{Settings.mixpanel_api_key}" @client = Mixpanel::Client.new( api_key: Settings.mixpanel_api_key, api_secret: Settings.mixpanel_api_secret ) @today = Date.today.strftime("%Y-%m-%d") @yesterday = (Date.today - 1).strftime("%Y-%m-%d") @session_id = session_id puts "" end |
Instance Method Details
#event_complex_query(event, extra_query) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/mixpaneltesting/mixpanel.rb', line 94 def event_complex_query(event, extra_query) # Arguments: # event: The event to validate dates. @log.debug "Request to mixpanel: #{event}" response = @client.request( 'segmentation', event: event, type: 'general', unit: 'day', from_date: @yesterday, to_date: @today, where: "properties[\"mp_session_id\"] == \"#{@session_id}\" and (#{extra_query})" ) @log.debug JSON.pretty_generate(response) response['data']['values'][event][@today] end |
#events_segmentation(events) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/mixpaneltesting/mixpanel.rb', line 75 def events_segmentation(events) # Arguments: # events: is a list of event names. @log.debug "Request to mixpanel: #{events}" Hash[events.map { |event| response = @client.request( 'segmentation', event: event, type: 'general', unit: 'day', from_date: @yesterday, to_date: @today, where: "properties[\"mp_session_id\"] == \"#{@session_id}\"" ) @log.debug JSON.pretty_generate(response) [event, response['data']['values'][event][@today]] }] end |
#validate_complex_query(event, extra_query, expected) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mixpaneltesting/mixpanel.rb', line 52 def validate_complex_query(event, extra_query, expected) # Arguments: # event: The event name to use in the query # extra_query: String with params for mixpanel query. # expected: The integer result to be expected # Return: # true: if succesfull. # false: if doesn't. Some info messages can go to stdout with this state. correct = false (1..10).each { received = event_complex_query(event, extra_query) @log.info "\"#{event}\": expected value #{expected} received #{received}" unless received == expected correct = received == expected break if correct puts "Retrying mixpanel queries in two seconds" sleep(3) } correct end |
#validate_events(expected_results) ⇒ Object
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 |
# File 'lib/mixpaneltesting/mixpanel.rb', line 23 def validate_events(expected_results) # Arguments: # expected_results: is a hash of event names (string) related with # expected result # Return: # true: if succesfull. # false: if doesn't. Some info messages can go to stdout with this state. correct = false (1..10).each { mixpanel_result = events_segmentation expected_results.keys result = expected_results.each { |event, expected_value| if expected_value != mixpanel_result[event] @log.info "\"#{event}\": expected value #{expected_value} received #{mixpanel_result[event]}" break end } correct = !result.nil? break if correct puts "Retrying mixpanel queries in two seconds" sleep(3) } correct end |