PHP code example of bkfdev / laravel-facebook-pixel
1. Go to this page and download the library: Download bkfdev/laravel-facebook-pixel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bkfdev / laravel-facebook-pixel example snippets
return [
/*
* The Facebook Pixel id, should be a code that looks something like "XXXXXXXXXXXXXXXX".
*/
'facebook_pixel_id' => env('FACEBOOK_PIXEL_ID', ''),
/*
* The key under which data is saved to the session with flash.
*/
'sessionKey' => env('FACEBOOK_PIXEL_SESSION_KEY', config('app.name').'_facebookPixel'),
/*
* To use the Conversions API, you need an access token. For Documentation please see: https://developers.facebook.com/docs/marketing-api/conversions-api/get-started
*/
'token' => env('FACEBOOK_PIXEL_TOKEN', ''), //Only if you plan using Conversions API for server events
/*
* Enable or disable script rendering. Useful for local development.
*/
'enabled' => env('FACEBOOK_PIXEL_ENABLED', false),
/*
* This is used to test server events
*/
'test_event_code' => env('FACEBOOK_TEST_EVENT_CODE')
];
// CheckoutController.php
use Bkfdev\FacebookPixel\Facades\FacebookPixel;
public function index()
{
FacebookPixel::track('Purchase', ['currency' => 'USD', 'value' => 30.00]);
return view('thank-you');
}
//For example your order id
FacebookPixel::track('Purchase', ['currency' => 'USD', 'value' => 30.00], '123456');
// ContactController.php
use Bkfdev\FacebookPixel\Facades\FacebookPixel;
public function postContact()
{
// Do contact form stuff...
FacebookPixel::flashEvent('Lead', [
'content_name' => 'Auto Insurance',
'content_category' => 'Quote',
'value' => 400.00,
'currency' => 'USD'
]);
return redirect()->action('ContactController@getContact');
}
use Bkfdev\FacebookPixel\Facades\FacebookPixel;
// Retrieve your Pixel id
$id = FacebookPixel::pixelId();
// Set Pixel id on the fly
FacebookPixel::setPixelId('XXXXXXXX');
// Check whether script rendering is enabled
$enabled = FacebookPixel::isEnabled(); // true|false
// Enable and disable script rendering on the fly
FacebookPixel::enable();
FacebookPixel::disable();
// Add event to the event layer (automatically renders right before the pixel script). Setting new values merges them with the previous ones.
FacebookPixel::track('eventName', ['attribute' => 'value']);
FacebookPixel::track('eventName', ['attribute' => 'value'], 'event_id'); //with an event id
FacebookPixel::track('eventName'); //without properties
FacebookPixel::track('eventName', [], 'event_id'); //with an event id but without properties
// Flash event for the next request. Setting new values merges them with the previous ones.
FacebookPixel::flashEvent('eventName', ['attribute' => 'value']);
FacebookPixel::flashEvent('eventName', ['attribute' => 'value'], 'event_id'); //with an event id
FacebookPixel::flashEvent('eventName'); //without properties
//Clear the event layer.
FacebookPixel::clear();
use Bkfdev\FacebookPixel\Facades\FacebookPixel;
// In your controller
FacebookPixel::trackCustom('CUSTOM-EVENT-NAME', ['custom_parameter' => 'ABC', 'value' => 10.00, 'currency' => 'USD']);
//With an event ID
FacebookPixel::trackCustom('CUSTOM-EVENT-NAME', ['custom_parameter' => 'ABC', 'value' => 10.00, 'currency' => 'USD'], 'EVENT_ID');
use Bkfdev\FacebookPixel\Facades\FacebookPixel;
//t) {
FacebookPixel::track('Purchase', [
'currency' => 'EUR',
'value' => $product->price
]);
});
//in your controller
FacebookPixel::purchase($product);
use Bkfdev\FacebookPixel\Facades\FacebookPixel;
use FacebookAds\Object\ServerSide\Content;
use FacebookAds\Object\ServerSide\CustomData;
use FacebookAds\Object\ServerSide\DeliveryCategory;
use FacebookAds\Object\ServerSide\UserData;
//Prepare User Data first.
// By default, the IP Address, User Agent and fbc/fbp cookies are added.
$user_data = FacebookPixel::userData()->setEmail('[email protected]')->setPhone('12345678901');
$content = (new Content())
->setProductId('product123')
->setQuantity(1)
->setDeliveryCategory(DeliveryCategory::HOME_DELIVERY);
$custom_data = (new CustomData())
->setContents(array($content))
->setCurrency('usd')
->setValue(123.45);
$eventId = uniqid('prefix_');
//send request
FacebookPixel::send('Purchase', $eventId ,$custom_data, $user_data);