From 053dde8e9d8cd41dd662bc8793f781877e2fcdb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrik=20Mokr=C3=BD?= Date: Sun, 27 Feb 2022 20:32:41 +0100 Subject: [PATCH] implement facebook conversion api --- composer.json | 4 +- resources/config/facebook-pixel.php | 4 ++ src/LaravelFacebookConversionApi.php | 86 ++++++++++++++++++++++++++-- 3 files changed, 88 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index c366eaa..4e3639f 100755 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ }, "autoload": { "psr-4": { - "WebLAgence\\LaravelFacebookPixel\\": "src" + "MokryPatrik\\LaravelFacebookConversionApi\\": "src" } }, "scripts": { @@ -41,7 +41,7 @@ "MokryPatrik\\LaravelFacebookConversionApi\\LaravelFacebookConversionApiServiceProvider" ], "aliases": { - "LaravelFacebookPixel": "WebLAgence\\LaravelFacebookPixel\\LaravelFacebookPixelFacade" + "LaravelFacebookPixel": "MokryPatrik\\LaravelFacebookConversionApi\\LaravelFacebookConversionApiFacade" } } }, diff --git a/resources/config/facebook-pixel.php b/resources/config/facebook-pixel.php index 4e63ce2..d354478 100644 --- a/resources/config/facebook-pixel.php +++ b/resources/config/facebook-pixel.php @@ -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. */ diff --git a/src/LaravelFacebookConversionApi.php b/src/LaravelFacebookConversionApi.php index 1223928..7bd49ca 100644 --- a/src/LaravelFacebookConversionApi.php +++ b/src/LaravelFacebookConversionApi.php @@ -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; @@ -14,7 +18,7 @@ class LaravelFacebookConversionApi use Macroable; /** - * @var string + * @var array */ protected $id; @@ -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; } @@ -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 ""; @@ -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; + } }