Skip to content

Commit cd9d366

Browse files
Save utms
1 parent aba9ae3 commit cd9d366

2 files changed

Lines changed: 80 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CHANGELOG
33

44
2.1.8
55
-----------------
6+
* Save utms
67
* countersContent выводится автоматически в шаблон, перед закрывающим тегом body
78

89
2.1.7

src/CmsSeoComponent.php

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@
3030

3131
/**
3232
* @property CanUrl $canUrl;
33+
* @property array $utms;
3334
*
3435
* @author Semenov Alexander <[email protected]>
3536
*/
3637
class CmsSeoComponent extends Component implements BootstrapInterface
3738
{
39+
/**
40+
*
41+
*/
42+
const SESSION_SEO_REQUEST_UTMS = 'session_seo_request_utms';
43+
3844
/**
3945
* Максимадбгая длина ключевых слов
4046
* @var int
@@ -69,6 +75,11 @@ class CmsSeoComponent extends Component implements BootstrapInterface
6975
*/
7076
public $countersContent = "";
7177

78+
/**
79+
* @var bool Подключить в низ страницы автоматически
80+
*/
81+
public $isAutoIncludecountersContent = true;
82+
7283

7384
/**
7485
* @var string
@@ -176,7 +187,8 @@ public function attributeLabels()
176187
public function attributeHints()
177188
{
178189
return ArrayHelper::merge(parent::attributeHints(), [
179-
'countersContent' => \Yii::t('skeeks/seo', 'В это поле вы можете поставить любые коды счетчиков и сторонних систем (yandex.metrics jivosite google.metrics и прочие). Они будут выведены внизу страницы, перед закрывающим тегом body'),
190+
'countersContent' => \Yii::t('skeeks/seo',
191+
'В это поле вы можете поставить любые коды счетчиков и сторонних систем (yandex.metrics jivosite google.metrics и прочие). Они будут выведены внизу страницы, перед закрывающим тегом body'),
180192
'enableKeywordsGenerator' => \Yii::t('skeeks/seo', 'If the page is not specified keywords, they will generate is for her, according to certain rules automatically'),
181193
'minKeywordLenth' => \Yii::t('skeeks/seo', 'The minimum length of the keyword, which is listed by the key (automatic generation)'),
182194
'maxKeywordsLength' => \Yii::t('skeeks/seo', 'The maximum length of the string of keywords (automatic generation)'),
@@ -261,7 +273,7 @@ public function bootstrap($application)
261273
}
262274
}
263275

264-
if (!BackendComponent::getCurrent()) {
276+
if (!BackendComponent::getCurrent() && !in_array(\Yii::$app->controller->module->id, ['debug', 'gii']) && $this->isAutoIncludecountersContent) {
265277
if ($this->countersContent) {
266278
$content = ob_get_contents();
267279
if (strpos($content, $this->countersContent) === false) {
@@ -289,6 +301,41 @@ public function bootstrap($application)
289301
\Yii::$container->set('yii\data\Pagination', [
290302
'forcePageParam' => $this->forcePageParam,
291303
]);
304+
305+
if (ENV == 'dev') {
306+
if (\Yii::$app->request->queryParams) {
307+
$utms = [];
308+
foreach (\Yii::$app->request->queryParams as $paramName => $paramValue) {
309+
if (in_array($paramName, [
310+
'from',
311+
'_openstat',
312+
313+
'utm_source',
314+
'utm_medium',
315+
'utm_campaign',
316+
'utm_content',
317+
'utm_term',
318+
'utm_referrer',
319+
320+
'pm_source',
321+
'pm_block',
322+
'pm_position',
323+
324+
'clid',
325+
'yclid',
326+
'ymclid',
327+
'frommarket',
328+
'text',
329+
])) {
330+
$utms[$paramName] = $paramValue;
331+
}
332+
}
333+
334+
if ($utms) {
335+
$this->setUtms($utms);
336+
}
337+
}
338+
}
292339
});
293340

294341

@@ -332,6 +379,34 @@ public function bootstrap($application)
332379
});
333380
}
334381

382+
/**
383+
* @param array $utms
384+
* @return $this
385+
*/
386+
public function setUtms($utms = [])
387+
{
388+
$utms['created_at'] = time();
389+
\Yii::$app->session->set(self::SESSION_SEO_REQUEST_UTMS, $utms);
390+
return $this;
391+
}
392+
393+
/**
394+
* @return array
395+
*/
396+
public function getUtms()
397+
{
398+
$utms = (array)\Yii::$app->session->get(self::SESSION_SEO_REQUEST_UTMS);
399+
if ($utms) {
400+
$created_at = ArrayHelper::getValue($utms, 'created_at');
401+
402+
//Если с момента создания утм прошло более 24 часов то
403+
if (time() - $created_at >= 3600*24) {
404+
$utms = [];
405+
}
406+
}
407+
return $utms;
408+
}
409+
335410
protected function _initDefaultCanUrl()
336411
{
337412
/**
@@ -354,7 +429,7 @@ protected function _initDefaultCanUrl()
354429
if (!$this->canUrl->scheme) {
355430
$this->canUrl->scheme = \Yii::$app->request->isSecureConnection ? "https" : "http";
356431
}
357-
432+
358433
if (\Yii::$app->requestedRoute) {
359434
$requestedUrl = Url::to(ArrayHelper::merge(["/".\Yii::$app->requestedRoute],
360435
(array)\Yii::$app->request->queryParams));
@@ -397,7 +472,7 @@ public function _addCanurlParams(WidgetEvent $e)
397472
$r = new \ReflectionClass($sender);
398473

399474
if (YII_DEBUG === true) {
400-
\Yii::info('_addCanurlParams: '.$r->getName(), (new \ReflectionClass($this->canUrl))->getName() ) ;
475+
\Yii::info('_addCanurlParams: '.$r->getName(), (new \ReflectionClass($this->canUrl))->getName());
401476
}
402477

403478
if ($pagination = $sender->dataProvider->getPagination()) {

0 commit comments

Comments
 (0)