Class: SpreeKlaviyo::AnalyticsEventHandler

Inherits:
Spree::BaseAnalyticsEventHandler
  • Object
show all
Defined in:
app/models/spree_klaviyo/analytics_event_handler.rb

Instance Method Summary collapse

Instance Method Details

#clientObject



3
4
5
# File 'app/models/spree_klaviyo/analytics_event_handler.rb', line 3

def client
  @client ||= store.integrations.active.find_by(type: 'Spree::Integrations::Klaviyo')
end

#handle_event(event_name, properties) ⇒ Object



7
8
9
10
11
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
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/spree_klaviyo/analytics_event_handler.rb', line 7

def handle_event(event_name, properties)
  return if client.blank?

  email = user&.email.presence

  record = case event_name
           when 'product_viewed'
             properties[:product]
           when 'product_list_viewed'
             properties[:taxon]
           when 'product_searched'
             properties[:query]
           when 'product_added', 'product_removed'
             email ||= properties[:line_item].order.email
             properties[:line_item].order
           when 'payment_info_entered'
             email ||= properties[:order].email
             properties[:order]
           when 'coupon_entered', 'coupon_removed'
             email ||= properties[:order].email
             properties[:order]
           when 'coupon_applied'
             email ||= properties[:order].email
             properties[:order]
           when 'coupon_denied'
             email ||= properties[:order].email
             properties[:order]
           when 'checkout_started'
             email ||= properties[:order].email
             properties[:order]
           when 'checkout_email_entered'
             email = properties[:email]
             properties[:order]
           when 'checkout_step_viewed', 'checkout_step_completed'
             email ||= properties[:order].email
             properties[:order]
           when 'order_completed'
             email ||= properties[:order].email
             properties[:order]
           when 'subscribed_to_newsletter'
             email ||= properties[:email]
             SpreeKlaviyo::SubscribeJob.perform_later(client.id, email, user&.id)
             nil
           when 'unsubscribed_from_newsletter'
             email ||= properties[:email]
             SpreeKlaviyo::UnsubscribeJob.perform_later(client.id, email, user&.id)
             nil
           end

  return if email.blank? && identity_hash[:visitor_id].blank?

  resource_type = record.nil? ? nil : (record.is_a?(String) ? 'String' : record.class.name)
  resource_id = record.is_a?(String) ? record : record&.id

  SpreeKlaviyo::AnalyticsEventJob.perform_later(client.id, event_human_name(event_name), resource_type, resource_id, email, identity_hash[:visitor_id])
end