-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Patrik Mokrý
committed
Feb 26, 2022
0 parents
commit 7a0a0d5
Showing
10 changed files
with
319 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
return [ | ||
/* | ||
* Use this variable instead of `facebook_pixel_id` if you need to use multiple facebook pixels | ||
*/ | ||
'facebook_pixel_ids' => [], | ||
|
||
/* | ||
* Enable or disable script rendering. Useful for local development. | ||
*/ | ||
'enabled' => true, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{!! LaravelFacebookPixel::bodyContent() !!} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
@if($enabled) | ||
<!-- Facebook Pixel Code --> | ||
<script> | ||
!function(f,b,e,v,n,t,s) | ||
{if(f.fbq)return;n=f.fbq=function(){n.callMethod? | ||
n.callMethod.apply(n,arguments):n.queue.push(arguments)}; | ||
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0'; | ||
n.queue=[];t=b.createElement(e);t.async=!0; | ||
t.src=v;s=b.getElementsByTagName(e)[0]; | ||
s.parentNode.insertBefore(t,s)}(window, document,'script', | ||
'https://connect.facebook.net/en_US/fbevents.js'); | ||
@foreach($id AS $i) | ||
fbq('init', '{{ $i }}'); | ||
@endforeach | ||
fbq('track', 'PageView'); | ||
</script> | ||
@foreach($id AS $i) | ||
<noscript> | ||
<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id={{ $i }}&ev=PageView&noscript=1"/> | ||
</noscript> | ||
@endforeach | ||
<!-- End Facebook Pixel Code --> | ||
@endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@include('facebook-pixel::head') | ||
@include('facebook-pixel::body') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace MokryPatrik\LaravelFacebookConversionApi; | ||
|
||
use Illuminate\Support\Traits\Macroable; | ||
use JsonException; | ||
|
||
/** | ||
* Class LaravelFacebookPixel | ||
* @package MokryPatrik\LaravelFacebookConversionApi | ||
*/ | ||
class LaravelFacebookConversionApi | ||
{ | ||
use Macroable; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $id; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $enabled; | ||
|
||
/** | ||
* LaravelFacebookPixel constructor. | ||
* @param $id | ||
*/ | ||
public function __construct($id) | ||
{ | ||
$this->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 "<script>" . $pixelCode . "</script>"; | ||
} | ||
|
||
/** | ||
* @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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace MokryPatrik\LaravelFacebookConversionApi; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
/** | ||
* Class LaravelFacebookPixelFacade | ||
* @package MokryPatrik\LaravelFacebookConversionApi | ||
*/ | ||
class LaravelFacebookConversionApiFacade extends Facade | ||
{ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return 'facebook-pixel'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace MokryPatrik\LaravelFacebookConversionApi; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
use MokryPatrik\LaravelFacebookConversionApi\LaravelFacebookConversionApi; | ||
|
||
/** | ||
* Class LaravelFacebookPixelServiceProvider | ||
* @package MokryPatrik\LaravelFacebookConversionApi | ||
*/ | ||
class LaravelFacebookConversionApiServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Bootstrap the application events. | ||
*/ | ||
public function boot() | ||
{ | ||
$this->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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace MokryPatrik\LaravelFacebookConversionApi; | ||
|
||
use Illuminate\View\View; | ||
|
||
/** | ||
* Class ScriptViewCreator | ||
* @package MokryPatrik\LaravelFacebookConversionApi | ||
*/ | ||
class ScriptViewCreator | ||
{ | ||
/** | ||
* @var LaravelFacebookConversionApi | ||
*/ | ||
protected $laravelFacebookPixel; | ||
|
||
/** | ||
* ScriptViewCreator constructor. | ||
* | ||
* @param LaravelFacebookConversionApi $laravelFacebookPixel | ||
*/ | ||
public function __construct(LaravelFacebookConversionApi $laravelFacebookPixel) | ||
{ | ||
$this->laravelFacebookPixel = $laravelFacebookPixel; | ||
} | ||
|
||
/** | ||
* @param View $view | ||
*/ | ||
public function create(View $view): void | ||
{ | ||
$view | ||
->with('enabled', $this->laravelFacebookPixel->isEnabled()) | ||
->with('id', $this->laravelFacebookPixel->getId()) | ||
; | ||
} | ||
} |