Skip to content

Instantly share code, notes, and snippets.

@LucasGorgal
Created August 12, 2021 20:24
Show Gist options
  • Save LucasGorgal/76b87d1e60a2aa194210e18e03a6710c to your computer and use it in GitHub Desktop.
Save LucasGorgal/76b87d1e60a2aa194210e18e03a6710c to your computer and use it in GitHub Desktop.
<?php
$email = $_POST['email'];
$time = time();
$event_id = hash("sha256", $email .$time);
//FB token
$access_token = 'FACEBOOK ACCESS TOKEN';
//Insert into FB
lead_event('PIXELID', $access_token, $email, $event_id);
//Insert into ActiveCampaign
$contact_id = insert_contact_active($email);
//Change automotion
automation($contact_id);
//Redirect to Thank You page using the paramter eventId to deduplicate events
header('Location: ' ."https://URL.com/inscricao/obrigado?eventID=$event_id");
function lead_event($pixelid, $access_token, $email, $event_id) {
$email_sha = hash("sha256", mb_strtolower($email));
$time = time();
$fbc = $_COOKIE['_fbc'];
$fbp = $_COOKIE['_fbp'];
$user_ip = $_SERVER['REMOTE_ADDR'];
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$event_source_url = 'https://' .$_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI'];
$ch = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://graph.facebook.com/v11.0/'.$pixelid.'/events',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('access_token' => $access_token, 'data' => '[
{
"event_name": "Lead",
"event_time":'.$time.',
"event_id": "'.$event_id.'",
"user_data": {
"em": [
"'.$email_sha.'"
],
"client_ip_address": "'.$ip.'",
"client_user_agent": "'.$user_agent.'",
"fbc": "'.$fbc.'",
"fbp": "'.$fbp.'"
},
"event_source_url": "'.$event_source_url.'",
"action_source": "website"
},
]', "test_event_code" => "TEST82658")
));
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error cURL';
}
curl_close($ch);
}
function insert_contact_active($email) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://CONTA.api-us1.com/api/3/contacts',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"contact": {
"email": "' .$email .'"
}
}',
CURLOPT_HTTPHEADER => array('Api-Token: TOKEN ACTIVE'),
));
$response = curl_exec($curl);
$response = json_decode($response, True);
$id = $response['contact']['id'];
return $id;
}
function automation($contact_id) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://CONTA.api-us1.com/api/3/contactAutomations',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"contactAutomation": {
"contact": "' .$contact_id .'",
"automation": "4"
}
}',
CURLOPT_HTTPHEADER => array(
'Api-Token: TOKEN ACTIVE',
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment