Good morning
for the past few days I’ve been working non stop on trying to implement the serverside Facebook Conversion API to circumvent the iOS 14 limitations on the FBpixel.
I’m no backend developer so this thing has been driving me close to desperation, the API documentation is awful, but in the end i managed to pull off a working example “in the void” using a cURL request and now i have to somehow implement it over to MODX.
What i need is the gist of a acouple snippets that can be called in 2 different situations:
- on page load which should be no problem, just drop the [[Snippet]] somewhere in the page
- as a formit hook here things get trickier because i would like to also pass some values from the form itself.
At the moment i dont need anything overcomplicated and i don’t mind having a separate snippet for every “event”, i just need them to work.
Here’s what i got down so far:
First of all i prepare the API variables, i already stored these in the modx settings
$fbapi_token = $modx->getOption('fbapi_token');
$fbapi_pixelID = $modx->getOption('fbapi_pixelID');
$fbapi_url = 'https://graph.facebook.com/v9.0/' . $fbapi_pixelID . '/events?access_token=' . $fbapi_token;
Then i get some global variables, feel free to point me if there’s a better way to retrieve these.
$ip = $_SERVER['REMOTE_ADDR']; // the user ip
$agent = $_SERVER['HTTP_USER_AGENT']; // the user agent
$time = date_create()->getTimestamp(); // a timestamp
$fbp = $_COOKIE[_fbp]; // the value of the facebook cookie used for deduplication
$id = $modx->resource->get('id'); // gather the current page ID
$url = $modx->makeUrl($id, '', '', 'full'); // generates the current page url
Question: the url from where the requests starts may have parameters but the $url variable doesn’t carry them. How do i do it?
After this i prepare the payload:
$data = array(
'data' => array(
0 => array(
'event_name' => 'EventName',
'event_source_url' => $url,
'event_time' => $timestamp,
'action_source' => 'website',
'user_data' => array(
'client_ip_address' => $ip,
'client_user_agent' => $agent,
'fbp' => $fbp,
),
'custom_data' => array(
'PAGE ID' => $id,
'FOO' => 'BAR'
),
)
),
'test_event_code' => 'XYZ', // for development test only
);
And convert it to JSON for the API to read it
$payload = json_encode($data);
At this point i need to send this payload to the API… In a pure php file this works but not on MODX:
$ch = curl_init($fbapi_url); // cURL init
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); // cURL payload
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); // cURL JSON header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // cURL return
$result = curl_exec($ch); // cURL execution
curl_close($ch); // cURL close
Question: I understand there’s a modREST method to do POST requests. Should i use that? I have no idea how to set it up so i need a few directions.
I’ve tried to implement it like this:
$client = $modx->getService('rest', 'rest.modRest');
$client->setOption('header', true);
$client->setOption('format','JSON');
$response = $client->post($fbapi_url, $data);
if (property_exists($response->responseInfo, 'scalar')) {
$code = $response->responseInfo->scalar;
}
echo $code;
but if i try to pass the json encoded $payload i get a 500 error on the page and if i pass it with the raw array $data i get returmed a 400 error on the request. I need to send the data as a JSON string.
The main focus right now is having this thing to work and properly send the request…
Next step: When used as a formit hook i’d love to pass into the data array a few values of the form itself. Let’s say:
'user_data' => array(
'em' => THE VALUE OF THE MAIL FIELD in SHA256 hash,
'ln' => THE VALUE OF THE NAME FIELD in SHA256 hash
),
How do i do that?