Skip to content

Commit

Permalink
implement facebook conversion api
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrik Mokrý committed Feb 27, 2022
1 parent 7a0a0d5 commit 053dde8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"autoload": {
"psr-4": {
"WebLAgence\\LaravelFacebookPixel\\": "src"
"MokryPatrik\\LaravelFacebookConversionApi\\": "src"
}
},
"scripts": {
Expand All @@ -41,7 +41,7 @@
"MokryPatrik\\LaravelFacebookConversionApi\\LaravelFacebookConversionApiServiceProvider"
],
"aliases": {
"LaravelFacebookPixel": "WebLAgence\\LaravelFacebookPixel\\LaravelFacebookPixelFacade"
"LaravelFacebookPixel": "MokryPatrik\\LaravelFacebookConversionApi\\LaravelFacebookConversionApiFacade"
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions resources/config/facebook-pixel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
*/
'facebook_pixel_ids' => [],

'facebook_conversion_api_access_token' => '',

'test_event_code' => false,

/*
* Enable or disable script rendering. Useful for local development.
*/
Expand Down
86 changes: 82 additions & 4 deletions src/LaravelFacebookConversionApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace MokryPatrik\LaravelFacebookConversionApi;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Traits\Macroable;
use JsonException;

Expand All @@ -14,7 +18,7 @@ class LaravelFacebookConversionApi
use Macroable;

/**
* @var string
* @var array
*/
protected $id;

Expand All @@ -36,9 +40,9 @@ public function __construct($id)
/**
* Return the Facebook Pixel id.
*
* @return string
* @return array
*/
public function getId(): string
public function getId(): array
{
return $this->id;
}
Expand Down Expand Up @@ -93,7 +97,9 @@ public function bodyContent(): string
$pixelCode = '';
foreach ($facebookPixelSession as $key => $facebookPixel) {
$params = json_encode($facebookPixel["parameters"], JSON_THROW_ON_ERROR);
$pixelCode .= "fbq('track', '" . $facebookPixel["name"] . "', " . $params . ");";
$eventId = uniqid($facebookPixel["name"], true);
$pixelCode .= "fbq('track', '" . $facebookPixel["name"] . "', " . $params . ", {eventID: '" . $eventId . "'});";
$this->conversionApiRequest($facebookPixel["name"], $eventId, $facebookPixel["parameters"]);
}
session()->forget('facebookPixelSession');
return "<script>" . $pixelCode . "</script>";
Expand All @@ -114,4 +120,76 @@ public function createEvent(string $eventName, array $parameters = []): void
$facebookPixelSession[] = $facebookPixel;
session(['facebookPixelSession' => $facebookPixelSession]);
}

/**
* @param $eventName
* @param $eventId
* @param $params
*
* @throws GuzzleException
*/
private function conversionApiRequest($eventName, $eventId, $params): void
{
if (isset($params['content_category']) && is_array($params['content_category'])) {
$params['content_category'] = implode(',', $params['content_category']);
}
$data = [
'data' => [
[
'event_name' => $eventName,
'event_id' => $eventId,
'event_time' => time(),
'event_source_url' => Request::url(),
'action_source' => 'website',
'user_data' => $this->getUserData(),
'custom_data' => $params,
]
]
];

if (config('facebook-pixel.test_event_code')) {
$data['test_event_code'] = config('facebook-pixel.test_event_code');
}
$data['access_token'] = config('facebook-pixel.facebook_conversion_api_access_token');

foreach ($this->getId() as $id) {
$endpoint = sprintf(
'https://graph.facebook.com/v13.0/%s/events',
$id
);

try {
$client = new Client();
$client->request(
'POST',
$endpoint,
[
'headers' => ['Content-type' => 'application/json'],
'body' => json_encode($data,JSON_THROW_ON_ERROR)
]
);
} catch (ClientException | JsonException $e) {}
}
}

/**
* @return array
*/
private function getUserData(): array
{
$data = [
'client_ip_address' => Request::ip(),
'client_user_agent' => Request::userAgent(),
];

if (Request::cookie('_fbc')) {
$data['fbc'] = Request::cookie('_fbc');
}

if (Request::cookie('_fbp')) {
$data['fbp'] = Request::cookie('_fbp');
}

return $data;
}
}

0 comments on commit 053dde8

Please sign in to comment.