Last active
April 14, 2021 01:10
-
-
Save webdevsuperfast/b5a721d6539d7818eee2c77706ecf76f to your computer and use it in GitHub Desktop.
FluentForms Server-side Facebook Conversions API
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
<?php | |
/** | |
* Functions | |
* | |
* @package Facebook Conversions API | |
* @since 1.0 | |
* @link https://rotsenacob.com/ | |
* @author Rotsen Mark Acob <rotsenacob.com> | |
* @copyright Copyright (c) 2020, Rotsen Mark Acob | |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License | |
* | |
*/ | |
define( 'PIXEL_ID', 'FB_PIXEL_ID_HERE' ); // Facebook Pixel ID | |
define( 'ACCESS_TOKEN', 'FB_ACCESS_TOKEN' ); // Facebook Access Token | |
define( 'API_VERSION', 'v9.0' ); // Facebook API Version | |
define( 'IP_ADDRESS', $_SERVER['REMOTE_ADDR'] ); | |
define( 'USER_AGENT', $_SERVER['HTTP_USER_AGENT'] ); | |
add_action( 'wp_head', function() { | |
global $post; | |
$endpoint = 'https://graph.facebook.com/' . API_VERSION . '/' . PIXEL_ID . '/events?access_token=' . ACCESS_TOKEN; | |
$data = array( | |
'event_name' => 'PageView', | |
'event_time' => time(), | |
'user_data' => [ | |
'client_user_agent' => USER_AGENT, | |
'client_ip_address' => IP_ADDRESS, | |
'em' => hash( 'sha256', 'john.doe@xyz.us' ) // Your email address | |
], | |
'event_source_url' => get_permalink(), | |
'action_source' => 'website', | |
); | |
wp_safe_remote_post( $endpoint, | |
array( | |
'body' => array( | |
'data' => array( json_encode( | |
$data | |
) ), | |
'test_event_code' => 'TEST12345' // Test Event Code for testing | |
), | |
) | |
); | |
}, 10, 1 ); | |
// Track all submissions from FluentForms | |
add_action( 'fluentform_submission_inserted', function( $entryId, $formData, $form ) { | |
global $post; | |
$endpoint = 'https://graph.facebook.com/' . API_VERSION . '/' . PIXEL_ID . '/events?access_token=' . ACCESS_TOKEN; | |
$data = array( | |
'event_name' => 'Lead', | |
'event_time' => time(), | |
'user_data' => [ | |
'client_user_agent' => USER_AGENT, | |
'client_ip_address' => IP_ADDRESS, | |
], | |
'event_source_url' => get_permalink( intval($formData['__fluent_form_embded_post_id']) ), | |
'action_source' => 'website' | |
); | |
$fname = $formData['names']['first_name']; | |
$lname = $formData['names']['last_name']; | |
$email = $formData['email']; | |
$phone = $formData['phone']; | |
if ( $fname ) : | |
$data['user_data']['fn'] = hash( 'sha256', strtolower( $fname ) ); | |
endif; | |
if ( $lname ) : | |
$data['user_data']['ln'] = hash( 'sha256', strtolower( $lname ) ); | |
endif; | |
if ( $email ) : | |
$data['user_data']['em'] = hash( 'sha256', strtolower( $email ) ); | |
endif; | |
if ( $phone ) : | |
$data['user_data']['ph'] = hash( 'sha256', $phone ); | |
endif; | |
wp_safe_remote_post( $endpoint, array( | |
'body' => array( | |
'data' => array( json_encode( | |
$data | |
) ), | |
'test_event_code' => 'TEST12345' // Test Event Code | |
) | |
) ); | |
}, 20, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment