Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
event.php
1<?php
2
4
5
7
9{
10 private $container = [];
11
12 // action source values : https://developers.facebook.com/docs/marketing-api/conversions-api/parameters/server-event#action-source
13 public const ACTION_SOURCE_EMAIL = 'email';
14 public const ACTION_SOURCE_WEBSITE = 'website';
15 public const ACTION_SOURCE_APP = 'app';
16 public const ACTION_SOURCE_PHONE_CALL = 'phone_call';
17 public const ACTION_SOURCE_CHAT = 'chat';
18 public const ACTION_SOURCE_PHYSICAL_STORE = 'physical_store';
19 public const ACTION_SOURCE_SYSTEM_GENERATED = 'system_generated';
20 public const ACTION_SOURCE_OTHER = 'other';
21
22 // events description: https://developers.facebook.com/docs/facebook-pixel/reference#standard-events
23 public const EVENT_ADD_PAYMENT = 'AddPaymentInfo';
24 public const EVENT_ADD_TO_CART = 'AddToCart';
25 public const EVENT_ADD_TO_WISH_LIST = 'AddToWishlist';
26 public const EVENT_COMPLETE_REGISTRATION = 'CompleteRegistration';
27 public const EVENT_CONTACT = 'Contact';
28 public const EVENT_DONATE = 'CustomizeProduct';
29 public const EVENT_FIND_LOCATION = 'FindLocation';
30 public const EVENT_INITIATE_CHECKOUT = 'InitiateCheckout';
31 public const EVENT_LEAD = 'Lead';
32 public const EVENT_PAGE_VIEW = 'PageView';
33 public const EVENT_PURCHASE = 'Purchase';
34 public const EVENT_SEARCH = 'Search';
35 public const EVENT_START_TRIAL = 'StartTrial';
36 public const EVENT_SUBMIT_APPLICATION = 'SubmitApplication';
37 public const EVENT_SUBSCRIBE = 'Subscribe';
38 public const EVENT_VIEW_CONTENT = 'ViewContent';
39
40 private function setParameter(string $key,$value)
41 {
42 $this->container[$key] = $value;
43 }
44
45 private function getParameter(string $key)
46 {
47 return array_key_exists($key,$this->container)? $this->container[$key] : null;
48 }
49
50 public static function getEventTypeList()
51 {
52 return [
53 static::EVENT_ADD_PAYMENT,
54 static::EVENT_ADD_TO_CART,
55 static::EVENT_ADD_TO_WISH_LIST,
56 static::EVENT_COMPLETE_REGISTRATION,
57 static::EVENT_CONTACT,
58 static::EVENT_DONATE,
59 static::EVENT_FIND_LOCATION,
60 static::EVENT_INITIATE_CHECKOUT,
61 static::EVENT_LEAD,
62 static::EVENT_PAGE_VIEW,
63 static::EVENT_SEARCH,
64 static::EVENT_START_TRIAL,
65 static::EVENT_SUBMIT_APPLICATION,
66 static::EVENT_SUBSCRIBE,
67 static::EVENT_VIEW_CONTENT,
68 static::EVENT_PURCHASE
69 ];
70 }
75 public static function getActionSourceList()
76 {
77 return [
78 static::ACTION_SOURCE_EMAIL,
79 static::ACTION_SOURCE_WEBSITE,
80 static::ACTION_SOURCE_APP,
81 static::ACTION_SOURCE_PHONE_CALL,
82 static::ACTION_SOURCE_CHAT,
83 static::ACTION_SOURCE_PHYSICAL_STORE,
84 static::ACTION_SOURCE_SYSTEM_GENERATED,
85 static::ACTION_SOURCE_OTHER
86 ];
87 }
88
94 public function __construct(?array $params = null)
95 {
96 if ($params && !empty($params))
97 {
98 if (array_key_exists('action_source',$params) && is_string($params['action_source']))
99 {
100 $this->setActionSource($params['action_source']);
101 }
102 if (array_key_exists('event_time',$params) && is_int($params['event_time']))
103 {
104 $this->setTime($params['event_time']);
105 }
106 if (array_key_exists('opt_out',$params) && is_bool($params['opt_out']))
107 {
108 $this->setDynamicAdsOption($params['opt_out']);
109 }
110 if (array_key_exists('event_name',$params) && is_string($params['event_name']))
111 {
112 $this->setEventType($params['event_name']);
113 }
114 if (array_key_exists('event_source_url',$params) && is_string($params['event_source_url']))
115 {
116 $this->setSource($params['event_source_url']);
117 }
118 if (array_key_exists('user_data',$params))
119 {
120 if ($params['user_data'] instanceof UserData)
121 {
122 $this->setUserData($params['user_data']);
123
124 } elseif (is_array($params['user_data']))
125 {
126 $this->setUserData(new UserData($params['user_data']));
127 }
128 }
129 if (array_key_exists('custom_data',$params))
130 {
131 if ($params['custom_data'] instanceof CustomData)
132 {
133 $this->setCustomData($params['custom_data']);
134
135 } elseif (is_array($params['custom_data']))
136 {
137 $this->setCustomData(new CustomData($params['custom_data']));
138 }
139 }
140 }
141
142 if (!$this->getParameter('event_time'))
143 {
144 $this->setParameter('event_time',time());
145 }
146
147 }
148
155 public function setActionSource(?string $action = self::ACTION_SOURCE_WEBSITE)
156 {
157 if(in_array($action,static::getActionSourceList()))
158 {
159 $this->setParameter('action_source',$action);
160 }
161 return $this;
162 }
163
170 public function setTime(?int $timeStamp)
171 {
172
173 if(is_int($timeStamp))
174 {
175 $this->setParameter('event_time',$timeStamp);
176 }
177 return $this;
178 }
185 public function setDynamicAdsOption(?bool $option = false)
186 {
187 $this->setParameter('opt_out',$option);
188 return $this;
189 }
190
197 public function setEventType(string $type)
198 {
199 //if(in_array($type,static::getEventTypeList()))
200 {
201 $this->setParameter('event_name',$type);
202 }
203 return $this;
204 }
205
212 public function setSource(string $source)
213 {
214 if(preg_match('%^((https://)|(www\.)|(http://))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i',$source))
215 {
216 $this->setParameter('event_source_url',$source);
217 }
218 return $this;
219 }
220
227 public function setUserData(UserData $userData)
228 {
229 $this->setParameter('user_data',$userData);
230 return $this;
231 }
232
239 public function setCustomData(CustomData $data)
240 {
241 $this->setParameter('custom_data',$data);
242 return $this;
243 }
244
248 public function validate() : bool
249 {
250 [$userData, $customData] = [$this->getParameter('user_data'), $this->getParameter('custom_data')];
251 if (
252 $this->getParameter('event_name')
253 && $userData instanceof UserData
254 && $userData->validate()
255 && $customData instanceof CustomData
256 && $customData->validate()
257 && $this->getParameter('event_time')
258 && $this->getParameter('event_time') + 604800 > time()
259 )
260 {
261 $result = true;
262 if ($this->getParameter('event_name') === static::EVENT_PURCHASE)
263 {
264 $result = $result && is_set($customData->getValue()) && is_set($customData->getCurrency());
265 }
266 if ($this->getParameter('action_source') === static::ACTION_SOURCE_WEBSITE)
267 {
268 $result = is_set($this->getParameter('event_source_url'));
269 }
270 return $result;
271 }
272 return false;
273 }
274
278 public function prepareData() : array
279 {
280 [$userData, $customData] = [$this->getParameter('user_data'), $this->getParameter('custom_data')];
281 return array_filter([
282 'action_source' => $this->getParameter('action_source'),
283 'custom_data' => $customData->toArray(),
284 'user_data' => $userData->toArray(),
285 'event_source_url' => $this->getParameter('event_source_url'),
286 'event_name' => $this->getParameter('event_name'),
287 'opt_out' => $this->getParameter('opt_out'),
288 'event_time' => $this->getParameter('event_time')
289 ]);
290 }
291}
setDynamicAdsOption(?bool $option=false)
Definition event.php:185
setUserData(UserData $userData)
Definition event.php:227
__construct(?array $params=null)
Definition event.php:94
setActionSource(?string $action=self::ACTION_SOURCE_WEBSITE)
Definition event.php:155
setCustomData(CustomData $data)
Definition event.php:239