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; |
|
|
|
|
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; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Disable Facebook Pixel scripts rendering. |
66
|
|
|
*/ |
67
|
|
|
public function disable() |
68
|
|
|
{ |
69
|
|
|
$this->enabled = false; |
|
|
|
|
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 headContent() |
103
|
|
|
{ |
104
|
|
|
if ($this->cspNonceCallback) { |
105
|
|
|
$row = "<script nonce='" . call_user_func($this->cspNonceCallback) . "'>"; |
106
|
|
|
} else { |
107
|
|
|
$row = "<script>"; |
108
|
|
|
} |
109
|
|
|
return "<!-- Facebook Pixel Code -->". |
110
|
|
|
$row. |
111
|
|
|
"!function(f,b,e,v,n,t,s) |
112
|
|
|
{if(f.fbq)return;n=f.fbq=function(){n.callMethod? |
113
|
|
|
n.callMethod.apply(n,arguments):n.queue.push(arguments)}; |
114
|
|
|
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0'; |
115
|
|
|
n.queue=[];t=b.createElement(e);t.async=!0; |
116
|
|
|
t.src=v;s=b.getElementsByTagName(e)[0]; |
117
|
|
|
s.parentNode.insertBefore(t,s)}(window, document,'script', |
118
|
|
|
'https://connect.facebook.net/en_US/fbevents.js'); |
119
|
|
|
fbq('init', '" . $this->id . "'); |
120
|
|
|
fbq('track', 'PageView'); |
121
|
|
|
</script> |
122
|
|
|
<noscript><img height='1' width='1' style='display:none' |
123
|
|
|
src='https://www.facebook.com/tr?id=" . $this->id . "&ev=PageView&noscript=1' |
124
|
|
|
/></noscript> |
125
|
|
|
<!-- End Facebook Pixel Code -->"; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function bodyContent() |
132
|
|
|
{ |
133
|
|
|
$facebookPixelSession = session()->pull('facebookPixelSession', []); |
|
|
|
|
134
|
|
|
$pixelCode = ""; |
135
|
|
|
if (count($facebookPixelSession) > 0) { |
136
|
|
|
foreach ($facebookPixelSession as $key => $facebookPixel) { |
137
|
|
|
$pixelCode .= "fbq('track', '" . $facebookPixel["name"] . "', " . json_encode($facebookPixel["parameters"]) . ");"; |
138
|
|
|
}; |
139
|
|
|
session()->forget('facebookPixelSession'); |
140
|
|
|
if($this->cspNonceCallback) { |
141
|
|
|
return "<script nonce='" . call_user_func($this->cspNonceCallback) . "'>" . $pixelCode . "</script>"; |
142
|
|
|
} |
143
|
|
|
return "<script>" . $pixelCode . "</script>"; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return ""; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param $eventName |
151
|
|
|
* @param array $parameters |
152
|
|
|
*/ |
153
|
|
|
public function createEvent($eventName, $parameters = []) |
154
|
|
|
{ |
155
|
|
|
$facebookPixelSession = session('facebookPixelSession'); |
|
|
|
|
156
|
|
|
$facebookPixelSession = !$facebookPixelSession ? [] : $facebookPixelSession; |
157
|
|
|
$facebookPixel = [ |
158
|
|
|
"name" => $eventName, |
159
|
|
|
"parameters" => $parameters, |
160
|
|
|
]; |
161
|
|
|
array_push($facebookPixelSession, $facebookPixel); |
162
|
|
|
session(['facebookPixelSession' => $facebookPixelSession]); |
163
|
|
|
} |
164
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths