109 lines
3.3 KiB
JavaScript
109 lines
3.3 KiB
JavaScript
const { fb_accessToken } = require("../config")
|
|
|
|
function postAddToCart() {
|
|
const request = context.request()
|
|
const headers = request.header("User-Agent")
|
|
const ipAddress = request.clientIp()
|
|
|
|
const eventPayload = {
|
|
data: [
|
|
{
|
|
event_name: "AddToCart",
|
|
event_time: Math.floor(Date.now() / 1000),
|
|
event_source_url: "https://www.binkrassdufass.de",
|
|
action_source: "website",
|
|
user_data: {
|
|
client_user_agent: headers,
|
|
client_ip_address: ipAddress,
|
|
},
|
|
},
|
|
],
|
|
}
|
|
const res = context.http.fetch(
|
|
"https://graph.facebook.com/v16.0/1117933239951751/events?access_token=" + fb_accessToken,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(eventPayload),
|
|
}
|
|
)
|
|
console.log(JSON.stringify(res), "CARTADD")
|
|
const response = res.body.json()
|
|
console.log(JSON.stringify(response))
|
|
}
|
|
|
|
function postUserRegister() {
|
|
const request = context.request()
|
|
const headers = request.header("User-Agent")
|
|
const ipAddress = request.clientIp()
|
|
const eventPayload = {
|
|
data: [
|
|
{
|
|
event_name: "CompleteRegistration",
|
|
event_time: Math.floor(Date.now() / 1000),
|
|
event_source_url: "https://www.binkrassdufass.de",
|
|
action_source: "website",
|
|
user_data: {
|
|
client_user_agent: headers,
|
|
client_ip_address: ipAddress,
|
|
},
|
|
},
|
|
],
|
|
}
|
|
const res = context.http.fetch(
|
|
"https://graph.facebook.com/v16.0/1117933239951751/events?access_token=" + fb_accessToken,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(eventPayload),
|
|
}
|
|
)
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {string} total
|
|
*/
|
|
function postPurchase(total) {
|
|
const eventPayload = {
|
|
data: [
|
|
{
|
|
event_name: "Purchase",
|
|
event_time: Math.floor(Date.now() / 1000),
|
|
event_source_url: "https://www.binkrassdufass.de",
|
|
action_source: "website",
|
|
user_data: {
|
|
// dummy data bc inside webhook we don't have access to user data
|
|
client_user_agent:
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
|
|
client_ip_address: "192.168.1.1", // random IP address
|
|
},
|
|
custom_data: {
|
|
currency: "EUR",
|
|
value: total,
|
|
},
|
|
},
|
|
],
|
|
}
|
|
const res = context.http.fetch(
|
|
"https://graph.facebook.com/v12.0/1117933239951751/events?access_token=" + fb_accessToken,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(eventPayload),
|
|
}
|
|
)
|
|
}
|
|
|
|
module.exports = {
|
|
postAddToCart,
|
|
postUserRegister,
|
|
postPurchase,
|
|
}
|