Class: TrackFbConversionWorker

Inherits:
ApplicationWorker show all
Defined in:
app/workers/track_fb_conversion_worker.rb

Constant Summary collapse

VALID_SOURCES =
'email,website,app,phone_call,chat,physical_store,system_generated,other'.split(',').freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationWorker

unlimited_retry

Class Method Details

.reservation_to_event_id(reservation) ⇒ Object



10
11
12
# File 'app/workers/track_fb_conversion_worker.rb', line 10

def self.reservation_to_event_id(reservation)
  "reservation-#{reservation.id}"
end

Instance Method Details

#generate_user_data(reservation, params) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'app/workers/track_fb_conversion_worker.rb', line 66

def generate_user_data(reservation, params)
  FacebookAds::ServerSide::UserData.new(
    emails: [reservation.email],
    phones: [reservation.phone],
    client_ip_address: params.fetch(:ip_address, nil),
    client_user_agent: params.fetch(:user_agent, nil),
    fbc: find_or_generate_fbc(params.fetch(:fbc, nil)),
    fbp: find_or_generate_fbp(params.fetch(:fbp, nil)),
  )
end

#perform(reservation_id, action, params) ⇒ Object

Parameters:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/workers/track_fb_conversion_worker.rb', line 24

def perform(reservation_id, action, params)
  return if params.blank?
  return if AdminSetting.fb_activate_server_conversion_api.to_s == 'false'

  self.reservation = Reservation.fetch reservation_id

  return if reservation.property.blank? && reservation.package.blank?

  case action.to_s.to_sym
  when :purchase
    purchase(reservation, params)
  else
    raise NotImplementedError, "#{action} is not implemented yet"
  end
end

#purchase(reservation, params) ⇒ Object



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
# File 'app/workers/track_fb_conversion_worker.rb', line 40

def purchase(reservation, params)
  params = normalize_params(params)
  user_data = generate_user_data(reservation, params)

  custom_data = generate_custom_data(reservation)

  event_time = params.fetch(:event_time) { Time.zone.now.to_i }
  event = FacebookAds::ServerSide::Event.new(
    event_name: 'Purchase',
    event_time: event_time,
    event_id: TrackFbConversionWorker.reservation_to_event_id(reservation),
    user_data: user_data,
    custom_data: custom_data,
    event_source_url: params.fetch(:event_source_url, nil),
    action_source: to_action_source(params.fetch(:action_source, nil)),
  )

  request = FacebookAds::ServerSide::EventRequest.new(
    test_event_code: AdminSetting.fb_test_event_code,
    pixel_id: pixel_id,
    events: [event],
  )

  request.execute
end