Last active
July 6, 2022 15:45
-
-
Save cdrani/08e4dfcc916b0d6198fcf15f79f5bcd7 to your computer and use it in GitHub Desktop.
Facebook Business
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Facebook | |
class EventHandler | |
def call(record:, request_params:, type:) | |
@record = record | |
@request_params = request_params | |
@facebook_pixel_id = Rails.configuration.analytics_facebook_pixel_id | |
send_event(type) | |
end | |
private | |
def send_event(type) | |
method = "create_#{type.to_s}_event" | |
event = send(method) | |
fb_request(event).execute | |
end | |
def user_data | |
object = @record.patient || @record | |
FacebookAds::ServerSide::UserData.new( | |
email: object.email, | |
phone: object.phone, | |
external_id: object.id, | |
client_ip_address: @request_params[:remote_ip], | |
client_user_agent: @request_params[:user_agent] | |
) | |
end | |
def fb_request_event(event) | |
FacebookAds::ServerSide::EventRequest.new( | |
pixel_id: @facebook_pixel_id, | |
events: [event], | |
test_event_code: ("TESTCODEHERE" unless Rails.env.production?) | |
) | |
end | |
end | |
class AppointmentEvent < EventHandler | |
def create_appointment_created_event | |
FacebookAds::ServerSide::Event.new( | |
event_name: 'Purchase', | |
event_time: Time.current.to_i, | |
user_data: user_data, | |
custom_data: appointment_custom_data | |
) | |
end | |
def appointment_custom_data | |
FacebookAds::ServerSide::CustomData.new( | |
contents: appointment_cart_contents, | |
currency: 'usd', | |
value: @record.total_cost, | |
delivery_category: 'home_delivery', | |
content_type: 'product', | |
custom_properties: appointment_custom_properties | |
) | |
end | |
def serialized_appointment | |
@serialized_appointment ||= AppointmentAnalyticsSerializer.new(@record).to_hash | |
end | |
def create_cart_content | |
lambda { |cart_content| | |
FacebookAds::ServerSide::Content.new( | |
product_id: cart_content[:package][:package_id], | |
title: cart_content[:package][:title], | |
category: cart_content[:package][:product_line] | |
) | |
} | |
end | |
def appointment_cart_contents | |
serialized_appointment[:cart_contents].map(&create_cart_content) | |
end | |
def appointment_custom_properties | |
{ appointment_id: serialized_appointment[:appointment_id], | |
patient_id: serialized_appointment[:patient_id], | |
locaion: serialized_appointment[:location], | |
asap: serialized_appointment[:asap], | |
requested_datetime: serialized_appointment[:requested_datetime], | |
cart_contents: serialized_appointment[:cart_contents] } | |
end | |
end | |
class PaymentEvent< EventHandler | |
def create_user_adds_card_event | |
FacebookAds::ServerSide::Event.new( | |
event_name: 'UserAddsCard', | |
event_time: Time.current.to_i, | |
user_data: user_data, | |
custom_data: serialized_patient_data | |
) | |
end | |
def serialized_patient | |
@serialized_patient ||= PatientAnalyticsSerializer.new(@record).to_hash | |
end | |
def serialized_patient_data | |
FacebookAds::ServerSide::CustomData.new(custom_properties: serialized_patient) | |
end | |
end | |
end | |
# Usage Example | |
request_params = { remote_ip: remote_ip, user_agent: request.user_agent } | |
FaceBook::PaymentEvent.new.call(record: patient, request_params: request_params, type: :user_adds_card) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment