Created
February 6, 2025 16:18
-
-
Save RiquelmyMelara/588a12a6b5f1150306cff97be1f501c6 to your computer and use it in GitHub Desktop.
Facebook CAPI
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
async function sendFacebookConversionEvent(order) { | |
const facebookURL = `https://graph.facebook.com/v17.0/${FACEBOOK_PIXEL_ID}/events`; | |
const email = order.Email; | |
if(!email) { | |
console.error("No email found for order:", order.ID); | |
return | |
} | |
const eventData = { | |
data: [ | |
{ | |
event_name: "Purchase", | |
event_time: Math.floor(Date.now() / 1000), | |
user_data: { | |
em: hashEmail(email), | |
}, | |
custom_data: { | |
currency: "USD", | |
value: order.TotalAmount, | |
order_id: order.ID, | |
}, | |
event_source_url: "https://www.5boysapparel.com", | |
action_source: "website", | |
}, | |
], | |
};); | |
try { | |
const response = await axios.post(facebookURL, eventData, { | |
headers: { | |
Authorization: `Bearer ${FACEBOOK_ACCESS_TOKEN}`, | |
"Content-Type": "application/json", | |
}, | |
}); | |
return response.data; | |
} catch (error) { | |
console.error(`Error sending Facebook conversion event for order ${order.ID}:`, error.response ? error.response.data : error.message); | |
throw error; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment