From 7a0a0d5b92779edd11966c29389ff210812009a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrik=20Mokr=C3=BD?= Date: Sat, 26 Feb 2022 10:47:23 +0100 Subject: [PATCH] Initial commit --- README.md | 8 ++ composer.json | 51 ++++++++ resources/config/facebook-pixel.php | 13 ++ resources/views/body.blade.php | 1 + resources/views/head.blade.php | 23 ++++ resources/views/script.blade.php | 2 + src/LaravelFacebookConversionApi.php | 117 ++++++++++++++++++ src/LaravelFacebookConversionApiFacade.php | 17 +++ ...elFacebookConversionApiServiceProvider.php | 49 ++++++++ src/ScriptViewCreator.php | 38 ++++++ 10 files changed, 319 insertions(+) create mode 100755 README.md create mode 100755 composer.json create mode 100644 resources/config/facebook-pixel.php create mode 100644 resources/views/body.blade.php create mode 100644 resources/views/head.blade.php create mode 100644 resources/views/script.blade.php create mode 100644 src/LaravelFacebookConversionApi.php create mode 100644 src/LaravelFacebookConversionApiFacade.php create mode 100644 src/LaravelFacebookConversionApiServiceProvider.php create mode 100644 src/ScriptViewCreator.php diff --git a/README.md b/README.md new file mode 100755 index 0000000..6d06af4 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# Facebook Conversion API integration for Laravel + +[![Latest Version on Packagist](https://img.shields.io/packagist/v/patrikmokry/laravel-facebook-conversion-api.svg?style=flat-square)](https://packagist.org/packages/patrikmokry/laravel-facebook-conversion-api) +[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) +[![Quality Score](https://img.shields.io/scrutinizer/g/patrikmokry/laravel-facebook-conversion-api.svg?style=flat-square)](https://scrutinizer-ci.com/g/patrikmokry/laravel-facebook-conversion-api) +[![Total Downloads](https://img.shields.io/packagist/dt/patrikmokry/laravel-facebook-conversion-api.svg?style=flat-square)](https://packagist.org/packages/patrikmokry/laravel-facebook-conversion-api) + +TODO: Documentation \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100755 index 0000000..c366eaa --- /dev/null +++ b/composer.json @@ -0,0 +1,51 @@ +{ + "name": "patrikmokry/laravel-facebook-conversion-api", + "type": "library", + "description": "Facebook Conversion API integration for Laravel", + "keywords": [ + "laravel-facebook-conversion", + "laravel", + "facebook pixel" + ], + "license": "MIT", + "authors": [ + { + "name": "Patrik MokrĂ˝", + "email": "patrik.m583@gmail.com", + "role": "Developer" + } + ], + "require": { + "php": "~7.1|^8.0" + }, + "require-dev": { + "phpunit/phpunit": ">=7.0", + "squizlabs/php_codesniffer": "^3.0" + }, + "autoload": { + "psr-4": { + "WebLAgence\\LaravelFacebookPixel\\": "src" + } + }, + "scripts": { + "test": "phpunit", + "check-style": "phpcs src tests", + "fix-style": "phpcbf src tests" + }, + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + }, + "laravel": { + "providers": [ + "MokryPatrik\\LaravelFacebookConversionApi\\LaravelFacebookConversionApiServiceProvider" + ], + "aliases": { + "LaravelFacebookPixel": "WebLAgence\\LaravelFacebookPixel\\LaravelFacebookPixelFacade" + } + } + }, + "config": { + "sort-packages": true + } +} diff --git a/resources/config/facebook-pixel.php b/resources/config/facebook-pixel.php new file mode 100644 index 0000000..4e63ce2 --- /dev/null +++ b/resources/config/facebook-pixel.php @@ -0,0 +1,13 @@ + [], + + /* + * Enable or disable script rendering. Useful for local development. + */ + 'enabled' => true, +]; \ No newline at end of file diff --git a/resources/views/body.blade.php b/resources/views/body.blade.php new file mode 100644 index 0000000..71eef9e --- /dev/null +++ b/resources/views/body.blade.php @@ -0,0 +1 @@ +{!! LaravelFacebookPixel::bodyContent() !!} diff --git a/resources/views/head.blade.php b/resources/views/head.blade.php new file mode 100644 index 0000000..0a9b2f6 --- /dev/null +++ b/resources/views/head.blade.php @@ -0,0 +1,23 @@ +@if($enabled) + + + @foreach($id AS $i) + + @endforeach + +@endif \ No newline at end of file diff --git a/resources/views/script.blade.php b/resources/views/script.blade.php new file mode 100644 index 0000000..716da9c --- /dev/null +++ b/resources/views/script.blade.php @@ -0,0 +1,2 @@ +@include('facebook-pixel::head') +@include('facebook-pixel::body') \ No newline at end of file diff --git a/src/LaravelFacebookConversionApi.php b/src/LaravelFacebookConversionApi.php new file mode 100644 index 0000000..1223928 --- /dev/null +++ b/src/LaravelFacebookConversionApi.php @@ -0,0 +1,117 @@ +id = $id; + $this->enabled = true; + } + + /** + * Return the Facebook Pixel id. + * + * @return string + */ + public function getId(): string + { + return $this->id; + } + + /** + * @param $id + * @return $this + */ + public function setId($id): LaravelFacebookConversionApi + { + $this->id = $id; + + return $this; + } + + /** + * Enable Facebook Pixel scripts rendering. + */ + public function enable(): void + { + $this->enabled = true; + } + + /** + * Disable Facebook Pixel scripts rendering. + */ + public function disable(): void + { + $this->enabled = false; + } + + /** + * @return bool + */ + public function isEnabled(): bool + { + return $this->enabled; + } + + /** + * @throws JsonException + * + * @return string + */ + public function bodyContent(): string + { + $facebookPixelSession = session()->pull('facebookPixelSession', []); + if (count($facebookPixelSession) < 1) { + return ''; + } + + $pixelCode = ''; + foreach ($facebookPixelSession as $key => $facebookPixel) { + $params = json_encode($facebookPixel["parameters"], JSON_THROW_ON_ERROR); + $pixelCode .= "fbq('track', '" . $facebookPixel["name"] . "', " . $params . ");"; + } + session()->forget('facebookPixelSession'); + return ""; + } + + /** + * @param string $eventName + * @param array $parameters + */ + public function createEvent(string $eventName, array $parameters = []): void + { + $facebookPixelSession = session('facebookPixelSession'); + $facebookPixelSession = !$facebookPixelSession ? [] : $facebookPixelSession; + $facebookPixel = [ + "name" => $eventName, + "parameters" => $parameters, + ]; + $facebookPixelSession[] = $facebookPixel; + session(['facebookPixelSession' => $facebookPixelSession]); + } +} diff --git a/src/LaravelFacebookConversionApiFacade.php b/src/LaravelFacebookConversionApiFacade.php new file mode 100644 index 0000000..36fb870 --- /dev/null +++ b/src/LaravelFacebookConversionApiFacade.php @@ -0,0 +1,17 @@ +loadViewsFrom(__DIR__ . '/../resources/views', 'facebook-pixel'); + + $this->publishes([ + __DIR__.'/../resources/config/facebook-pixel.php' => config_path('facebook-pixel.php'), + ], 'config'); + + $this->app['view']->creator( + ['facebook-pixel::head', 'facebook-pixel::body'], + ScriptViewCreator::class + ); + } + + /** + * Register the service provider. + */ + public function register(): void + { + $this->mergeConfigFrom(__DIR__ . '/../resources/config/facebook-pixel.php', 'facebook-pixel'); + + $id = config('facebook-pixel.facebook_pixel_ids'); + + $laravelFacebookPixel = new LaravelFacebookConversionApi($id); + + if (config('facebook-pixel.enabled') === false) { + $laravelFacebookPixel->disable(); + } + + $this->app->instance(LaravelFacebookConversionApi::class, $laravelFacebookPixel); + $this->app->alias(LaravelFacebookConversionApi::class, 'facebook-pixel'); + } +} \ No newline at end of file diff --git a/src/ScriptViewCreator.php b/src/ScriptViewCreator.php new file mode 100644 index 0000000..ba3a4f0 --- /dev/null +++ b/src/ScriptViewCreator.php @@ -0,0 +1,38 @@ +laravelFacebookPixel = $laravelFacebookPixel; + } + + /** + * @param View $view + */ + public function create(View $view): void + { + $view + ->with('enabled', $this->laravelFacebookPixel->isEnabled()) + ->with('id', $this->laravelFacebookPixel->getId()) + ; + } +} \ No newline at end of file