Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
pixelfb.php
1<?php
3
4use \Bitrix\Seo\BusinessSuite\ExtensionFacade;
5use \Bitrix\Main\Loader;
6use \Bitrix\Main\Localization\Loc;
7use \Bitrix\Main\Config\Option;
8use \Bitrix\Landing\Field;
9use \Bitrix\Landing\Manager;
10
11Loc::loadMessages(__FILE__);
12
14{
19 protected function getMap()
20 {
21 $helpUrl = \Bitrix\Landing\Help::getHelpUrl('PIXEL');
22 return array(
23 'USE' => new Field\Checkbox('USE', array(
24 'title' => Loc::getMessage('LANDING_HOOK_PIXEL_FB_USE')
25 )),
26 'COUNTER' => new Field\Text('COUNTER', array(
27 'title' => Loc::getMessage('LANDING_HOOK_PIXEL_FB_COUNTER'),
28 'placeholder' => Loc::getMessage('LANDING_HOOK_PIXEL_FB_PLACEHOLDER2'),
29 'help' => $helpUrl
30 ? '<a href="' . $helpUrl . '" target="_blank">' .
31 Loc::getMessage('LANDING_HOOK_PIXEL_FB_HELP') .
32 '</a>'
33 : ''
34 ))
35 );
36 }
37
42 public function enabledInEditMode()
43 {
44 return false;
45 }
46
51 public function enabled()
52 {
53 if ($this->issetCustomExec())
54 {
55 return true;
56 }
57
58 if (!$this->isPage() && Manager::isB24())
59 {
60 return true;
61 }
62
63 return $this->fields['USE']->getValue() == 'Y';
64 }
65
70 private static function getBusinessPixelFromSeo(): ?string
71 {
72 if (\Bitrix\Main\Loader::includeModule('seo'))
73 {
74 $businessSuite = ExtensionFacade::getInstance();
75 if ($businessSuite->isInstalled())
76 {
77 return $businessSuite->getCurrentInstalls()->getPixel();
78 }
79 }
80
81 return null;
82 }
83
88 private function getBusinessPixel(): ?string
89 {
90 $pixelStored = Manager::getOption('business_pixel');
91 if ($pixelStored === null)
92 {
93 $pixelStored = $this->getBusinessPixelFromSeo();
94 Manager::setOption('business_pixel', $pixelStored);
95 }
96
97 return $pixelStored;
98 }
99
104 public static function changeBusinessPixel(): void
105 {
106 if (self::getBusinessPixelFromSeo() !== Manager::getOption('business_pixel'))
107 {
108 Option::delete('landing', ['name' => 'business_pixel']);
110 }
111 }
112
117 public function exec()
118 {
119 if ($this->execCustom())
120 {
121 return;
122 }
123
124 $zone = '';
125 if (Loader::includeModule('bitrix24'))
126 {
127 $zone = \CBitrix24::getPortalZone();
128 }
129 elseif (file_exists($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/lang/ru")
130 && !file_exists($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/lang/ua"))
131 {
132 $zone = 'ru';
133 }
134 if ($zone === 'ru')
135 {
136 return;
137 }
138
139 $counter = null;
140 $businessPixel = $this->getBusinessPixel();
141
142 if ($this->fields['USE']->getValue() === 'Y')
143 {
144 $counter = \htmlspecialcharsbx(trim($this->fields['COUNTER']));
145 $counter = \CUtil::jsEscape($counter);
146 }
147
148 if (!$counter || $counter === $businessPixel)
149 {
150 $counter = $businessPixel;
151 $businessPixel = null;
152 }
153
154 if ($counter)
155 {
157 'fbp',
158 '!function(f,b,e,v,n,t,s)
159 {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
160 n.callMethod.apply(n,arguments):n.queue.push(arguments)};
161 if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version=\'2.0\';n.agent=\'plbitrix\';
162 n.queue=[];t=b.createElement(e);t.async=!0;
163 t.src=v;s=b.getElementsByTagName(e)[0];
164 s.parentNode.insertBefore(t,s)}(window, document,\'script\',
165 \'https://connect.facebook.net/en_US/fbevents.js\');
166 fbq(\'init\', \'' . $counter . '\');
167 fbq(\'track\', \'PageView\');'.
168 ($businessPixel
169 ? "\n fbq('init', '{$businessPixel}');" .
170 "\n fbq('track', 'PageView');"
171 : '')
172 );
174 'Noscript',
175 '<noscript>
176 <img height="1" width="1" style="display:none" alt="" src="https://www.facebook.com/tr?id=' .$counter . '&ev=PageView&noscript=1"/>
177 </noscript>'
178 );
179 }
180 }
181}
static addCookieScript(string $cookieCode, string $functionBody)
Definition cookies.php:167
static getOption($code, $default=null)
Definition manager.php:160
static setOption($code, $value)
Definition manager.php:171
static setPageView(string $marker, string $content, bool $skipTrim=false)
Definition manager.php:465
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29