PHP code example of esign / laravel-conversions-api
1. Go to this page and download the library: Download esign/laravel-conversions-api 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/ */
esign / laravel-conversions-api example snippets
return [
/**
* The access token used by the Conversions API.
*/
'access_token' => env('CONVERSIONS_API_ACCESS_TOKEN'),
/**
* The pixel ID used by the Conversions API.
*/
'pixel_id' => env('CONVERSIONS_API_PIXEL_ID'),
/**
* The Google Tag Manager container ID used in case you're deduplicating
* events through Google Tag Manager instead of Facebook Pixel directly.
* Should look something like "GTM-XXXXXX".
*/
'gtm_id' => env('GOOGLE_TAG_MANAGER_ID'),
/**
* The Conversions API comes with a nice way to test your events.
* You may use this config variable to set your test code.
*/
'test_code' => env('CONVERSIONS_API_TEST_CODE'),
];
use Esign\ConversionsApi\Facades\ConversionsApi;
use FacebookAds\Object\ServerSide\UserData;
use FacebookAds\Object\ServerSide\Event;
ConversionsApi::addEvent(
(new Event())->setEventName('PageView')->setEventId('abc')
);
ConversionsApi::setEvents([
(new Event())->setEventName('PageView')->setEventId('abc'),
(new Event())->setEventName('Purchase')->setEventId('xyz'),
]);
ConversionsApi::getEvents();
ConversionsApi::clearEvents();
use Esign\ConversionsApi\Facades\ConversionsApi;
ConversionsApi::sendEvents();
use Esign\ConversionsApi\Facades\ConversionsApi;
use FacebookAds\Object\ServerSide\ActionSource;
use FacebookAds\Object\ServerSide\Event;
class PurchaseEvent extends Event
{
public static function create(): static
{
return (new static())
->setActionSource(ActionSource::WEBSITE)
->setEventName('Purchase')
->setEventTime(time())
->setEventSourceUrl(request()->fullUrl())
->setEventId((string) Str::uuid())
->setUserData(ConversionsApi::getUserData());
}
}
namespace App\Http\Middleware;
use Esign\ConversionsApi\Facades\ConversionsApi;
use Esign\ConversionsApi\Objects\DefaultUserData;
class InitializeFacebookUserData
{
public function handle(Request $request, Closure $next): Response
{
ConversionsApi::setUserData(
DefaultUserData::create()
->setEmail($request->user()?->email)
);
return $next($request);
}
}
use Esign\ConversionsApi\Facades\ConversionsApi;
use FacebookAds\Object\ServerSide\Event;
ConversionsApi::addEvent(
(new Event())->setUserData(ConversionsApi::getUserData())
);
ConversionsApi::setUserData(
(new UserData())->setEmail('[email protected]')
);
use Esign\ConversionsApi\Contracts\MapsToFacebookPixel;
use Esign\ConversionsApi\Facades\ConversionsApi;
use FacebookAds\Object\ServerSide\ActionSource;
use FacebookAds\Object\ServerSide\Event;
class PurchaseEvent extends Event implements MapsToFacebookPixel
{
public static function create(): static
{
return (new static())
->setActionSource(ActionSource::WEBSITE)
->setEventName('Purchase')
->setEventTime(time())
->setEventSourceUrl(request()->fullUrl())
->setEventId((string) Str::uuid())
->setUserData(ConversionsApi::getUserData());
}
public function getFacebookPixelEventType(): string
{
return 'track';
}
public function getFacebookPixelEventName(): string
{
return $this->getEventName();
}
public function getFacebookPixelCustomData(): array
{
$customData = $this->getCustomData();
return array_filter([
'currency' => $customData?->getCurrency(),
'value' => $customData?->getValue(),
]);
}
public function getFacebookPixelEventData(): array
{
return array_filter(['eventID' => $this->getEventId()]);
}
}
use FacebookAds\Object\ServerSide\CustomData;
use Illuminate\Support\Str;
$event = PurchaseEvent::create()->setCustomData(
(new CustomData())->setCurrency('EUR')->setValue(10)
);
ConversionsApi::setUserData(
(new UserData())->setEmail('[email protected]')
);
use Esign\ConversionsApi\Contracts\MapsToDataLayer;
use Esign\ConversionsApi\Facades\ConversionsApi;
use FacebookAds\Object\ServerSide\ActionSource;
use FacebookAds\Object\ServerSide\Event;
class PurchaseEvent extends Event implements MapsToDataLayer
{
public static function create(): static
{
return (new static())
->setActionSource(ActionSource::WEBSITE)
->setEventName('Purchase')
->setEventTime(time())
->setEventSourceUrl(request()->fullUrl())
->setEventId((string) Str::uuid())
->setUserData(ConversionsApi::getUserData());
}
public function getDataLayerArguments(): array
{
$customData = $this->getCustomData();
return [
'event' => 'conversionsApiPurchase',
'conversionsApiPurchaseEventId' => $this->getEventId(),
'conversionsApiPurchaseCurrency' => $customData?->getCurrency(),
'conversionsApiPurchaseValue' => $customData?->getValue(),
];
}
}
use FacebookAds\Object\ServerSide\CustomData;
use Illuminate\Support\Str;
$event = PurchaseEvent::create()->setCustomData(
(new CustomData())->setCurrency('EUR')->setValue(10)
);
namespace App\Http\Middleware;
use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;
class EncryptCookies extends Middleware
{
/**
* The names of the cookies that should not be encrypted.
*
* @var array
*/
protected $except = [
'_fbp',
'_fbc'
];
}