Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
gacounter.php
1<?php
3
4use \Bitrix\Landing\Field;
5use \Bitrix\Landing\Manager;
6use \Bitrix\Main\Localization\Loc;
7
8Loc::loadMessages(__FILE__);
9
11{
16 protected function getMap()
17 {
18 $helpUrl = \Bitrix\Landing\Help::getHelpUrl('GACOUNTER');
19 return array(
20 'USE' => new Field\Checkbox('USE', array(
21 'title' => Loc::getMessage('LANDING_HOOK_GACOUNTER_USE')
22 )),
23 'COUNTER' => new Field\Text('COUNTER', array(
24 'title' => Loc::getMessage('LANDING_HOOK_GACOUNTER_COUNTER'),
25 'placeholder' => Loc::getMessage('LANDING_HOOK_GACOUNTER_PLACEHOLDER_1')
26 )),
27 'COUNTER_GA4' => new Field\Text('COUNTER_GA4', array(
28 'title' => Loc::getMessage('LANDING_HOOK_GACOUNTER_COUNTER'),
29 'placeholder' => Loc::getMessage('LANDING_HOOK_GACOUNTER_PLACEHOLDER_2'),
30 'help' => $helpUrl
31 ? '<a href="' . $helpUrl . '" target="_blank">' .
32 Loc::getMessage('LANDING_HOOK_DETAIL_HELP') .
33 '</a>'
34 : ''
35 )),
36 'SEND_CLICK' => new Field\Checkbox('SEND_CLICK', array(
37 'title' => Loc::getMessage('LANDING_HOOK_GACOUNTER_SEND_CLICK')
38 )),
39 'CLICK_TYPE' => new Field\Select('CLICK_TYPE', array(
40 'title' => Loc::getMessage('LANDING_HOOK_GACOUNTER_CLICK_TYPE'),
41 'options' => [
42 'href' => Loc::getMessage('LANDING_HOOK_GACOUNTER_CLICK_TYPE_HREF'),
43 'text' => Loc::getMessage('LANDING_HOOK_GACOUNTER_CLICK_TYPE_TEXT'),
44 ]
45 )),
46 'SEND_SHOW' => new Field\Checkbox('SEND_SHOW', array(
47 'title' => Loc::getMessage('LANDING_HOOK_GACOUNTER_SEND_SHOW'),
48 ))
49 );
50 }
51
56 public function isFree()
57 {
58 return false;
59 }
60
65 public function isLocked()
66 {
67 return !\Bitrix\Landing\Restriction\Manager::isAllowed(
68 'limit_sites_google_analytics'
69 );
70 }
71
76 public function enabled()
77 {
78 if ($this->isLocked())
79 {
80 return false;
81 }
82
83 if ($this->issetCustomExec())
84 {
85 return true;
86 }
87
88 return $this->fields['USE']->getValue() == 'Y';
89 }
90
95 public function enabledInEditMode()
96 {
97 return false;
98 }
99
104 public function exec()
105 {
106 if ($this->execCustom())
107 {
108 return;
109 }
110
111 if ($this->fields['USE']->getValue() != 'Y')
112 {
113 return;
114 }
115
116 //Universal Analytics
117 $this->setCounter($this->fields['COUNTER']);
118 //Google Analytics 4
119 $this->setCounter($this->fields['COUNTER_GA4']);
120
121 // send analytics
122 $sendData = [];
123 if ($this->fields['SEND_CLICK']->getValue() == 'Y')
124 {
125 $sendData[] = 'click';
126 }
127 if ($this->fields['SEND_SHOW']->getValue() == 'Y')
128 {
129 $sendData[] = 'show';
130 }
131 if (!empty($sendData))
132 {
133 \Bitrix\Landing\Manager::setPageView(
134 'BodyTag',
135 ' data-event-tracker=\'' . json_encode($sendData) . '\''
136 );
137 $clickType = $this->fields['CLICK_TYPE']->getValue();
138 if (!$clickType)
139 {
140 $clickType = 'text';
141 }
142 if ($clickType)
143 {
144 \Bitrix\Landing\Manager::setPageView(
145 'BodyTag',
146 ' data-event-tracker-label-from="' . \htmlspecialcharsbx($clickType) . '"'
147 );
148 }
149 }
150 }
151
157 public static function setCounter(string $counter): void
158 {
159 $counter = \htmlspecialcharsbx(trim($counter));
160 $counter = \CUtil::jsEscape($counter);
161
162 if (!$counter)
163 {
164 return;
165 }
166
168 'AfterHeadOpen',
169 '<script async
170 src="https://www.googletagmanager.com/gtag/js?id=' . $counter . '"
171 data-skip-moving="true"
172 ></script>'
173 );
175 'ga',
176 'window.dataLayer = window.dataLayer || [];
177 function gtag(){dataLayer.push(arguments)};
178 gtag("js", new Date());
179 gtag("config", "' . $counter . '");'
180 );
181 }
182}
static addCookieScript(string $cookieCode, string $functionBody)
Definition cookies.php:167
static setCounter(string $counter)
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