Class: ForestLiana::MixpanelLastEventsGetter

Inherits:
IntegrationBaseGetter show all
Defined in:
app/services/forest_liana/mixpanel_last_events_getter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ MixpanelLastEventsGetter

Returns a new instance of MixpanelLastEventsGetter.


5
6
7
8
9
10
# File 'app/services/forest_liana/mixpanel_last_events_getter.rb', line 5

def initialize(params)
  @params = params
  api_secret = ForestLiana.integrations[:mixpanel][:api_secret]
  @custom_properties = ForestLiana.integrations[:mixpanel][:custom_properties]
  @mixpanel = Mixpanel::Client.new(api_secret: api_secret)
end

Instance Attribute Details

#recordObject

Returns the value of attribute record.


3
4
5
# File 'app/services/forest_liana/mixpanel_last_events_getter.rb', line 3

def record
  @record
end

Instance Method Details

#countObject


83
84
85
# File 'app/services/forest_liana/mixpanel_last_events_getter.rb', line 83

def count
  @records.count
end

#perform(field_name, field_value) ⇒ Object


12
13
14
15
16
17
18
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
# File 'app/services/forest_liana/mixpanel_last_events_getter.rb', line 12

def perform(field_name, field_value)
  result = @mixpanel.request(
    'jql',
    script: "function main() {
      return People().filter(function (user) {
        return user.properties.$email == '#{field_value}';
      });
    }"
  )

  if result.length == 0
    @records = []
    return
  end

  from_date = (DateTime.now - 60.days).strftime("%Y-%m-%d")
  to_date = DateTime.now.strftime("%Y-%m-%d")
  distinct_id = result[0]['distinct_id']

  result = @mixpanel.request(
    'stream/query',
    from_date: from_date,
    to_date: to_date,
    distinct_ids: [distinct_id],
    limit: 100
  )

  if result['status'] != 'ok'
    FOREST_LOGGER.error "Cannot retrieve the Mixpanel last events"
    @records = []
    return
  end

  if result.length == 0
    @records = []
    return
  end

  @records = process_result(result['results']['events'])
end

#process_result(events) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/services/forest_liana/mixpanel_last_events_getter.rb', line 53

def process_result(events)
  events.reverse.map { |event|
    properties = event['properties']

    new_event = {
      'id' => SecureRandom.uuid,
      'event' => event['event'],
      'city' => properties['$city'],
      'region' => properties['$region'],
      'timezone' => properties['$timezone'],
      'os' => properties['$os'],
      'osVersion' => properties['$os_version'],
      'country' => properties['mp_country_code'],
      'browser' => properties['browser'],
    }

    time = properties['time'].to_s
    new_event['date'] = DateTime.strptime(time,'%s').strftime("%Y-%m-%dT%H:%M:%S%z")

    custom_attributes = event['properties'].select { |key, _| @custom_properties.include? key }
    new_event = new_event.merge(custom_attributes)

    ForestLiana::MixpanelEvent.new(new_event)
  }
end

#recordsObject


79
80
81
# File 'app/services/forest_liana/mixpanel_last_events_getter.rb', line 79

def records
  @records
end