weblagence /
laravel-facebook-pixel
| 1 | <?php |
||
| 2 | |||
| 3 | namespace WebLAgence\LaravelFacebookPixel; |
||
| 4 | |||
| 5 | use Illuminate\Support\Traits\Macroable; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Class LaravelFacebookPixel |
||
| 9 | * @package WebLAgence\LaravelFacebookPixel |
||
| 10 | */ |
||
| 11 | class LaravelFacebookPixel |
||
| 12 | { |
||
| 13 | use Macroable; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $id; |
||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $cspNonceCallback; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * LaravelFacebookPixel constructor. |
||
| 26 | * @param $id |
||
| 27 | */ |
||
| 28 | public function __construct($id) |
||
| 29 | { |
||
| 30 | $this->id = $id; |
||
| 31 | $this->enabled = true; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 32 | $this->cspNonceCallback = ''; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Return the Facebook Pixel id. |
||
| 37 | * |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | public function id() |
||
| 41 | { |
||
| 42 | return $this->id; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param $id |
||
| 47 | * @return $this |
||
| 48 | */ |
||
| 49 | public function setId($id) |
||
| 50 | { |
||
| 51 | $this->id = $id; |
||
| 52 | |||
| 53 | return $this; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Enable Facebook Pixel scripts rendering. |
||
| 58 | */ |
||
| 59 | public function enable() |
||
| 60 | { |
||
| 61 | $this->enabled = true; |
||
|
0 ignored issues
–
show
|
|||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Disable Facebook Pixel scripts rendering. |
||
| 66 | */ |
||
| 67 | public function disable() |
||
| 68 | { |
||
| 69 | $this->enabled = false; |
||
|
0 ignored issues
–
show
|
|||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return bool |
||
| 74 | */ |
||
| 75 | public function isEnabled() |
||
| 76 | { |
||
| 77 | return $this->enabled; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | public function getCspNonceCallback() |
||
| 84 | { |
||
| 85 | return $this->cspNonceCallback; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $callback |
||
| 90 | */ |
||
| 91 | public function addCspNonce($callback) |
||
| 92 | { |
||
| 93 | if (!function_exists($callback)) { |
||
| 94 | return; |
||
| 95 | } |
||
| 96 | $this->cspNonceCallback = $callback; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function bodyContent() |
||
| 103 | { |
||
| 104 | $facebookPixelSession = session()->pull('facebookPixelSession', []); |
||
| 105 | $pixelCode = ""; |
||
| 106 | if (count($facebookPixelSession) > 0) { |
||
| 107 | foreach ($facebookPixelSession as $key => $facebookPixel) { |
||
| 108 | $pixelCode .= "fbq('track', '" . $facebookPixel["name"] . "', " . json_encode($facebookPixel["parameters"]) . ");"; |
||
| 109 | }; |
||
| 110 | session()->forget('facebookPixelSession'); |
||
| 111 | if($this->cspNonceCallback) { |
||
| 112 | return "<script nonce='" . call_user_func($this->cspNonceCallback) . "'>" . $pixelCode . "</script>"; |
||
| 113 | } |
||
| 114 | return "<script>" . $pixelCode . "</script>"; |
||
| 115 | } |
||
| 116 | |||
| 117 | return ""; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param $eventName |
||
| 122 | * @param array $parameters |
||
| 123 | */ |
||
| 124 | public function createEvent($eventName, $parameters = []) |
||
| 125 | { |
||
| 126 | $facebookPixelSession = session('facebookPixelSession'); |
||
| 127 | $facebookPixelSession = !$facebookPixelSession ? [] : $facebookPixelSession; |
||
| 128 | $facebookPixel = [ |
||
| 129 | "name" => $eventName, |
||
| 130 | "parameters" => $parameters, |
||
| 131 | ]; |
||
| 132 | array_push($facebookPixelSession, $facebookPixel); |
||
| 133 | session(['facebookPixelSession' => $facebookPixelSession]); |
||
| 134 | } |
||
| 135 | } |