-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from azlydnev/azlydnev-yandex-metrica
Azlydnev yandex metrica
- Loading branch information
Showing
5 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
/** | ||
* webtrees: online genealogy | ||
* Copyright (C) 2023 webtrees development team | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fisharebest\Webtrees\Module; | ||
|
||
use Fisharebest\Webtrees\I18N; | ||
|
||
/** | ||
* Class YandexMetrica - add support for Yandex Metrica. | ||
*/ | ||
class YandexMetrica extends AbstractModule implements ModuleAnalyticsInterface, ModuleConfigInterface, ModuleExternalUrlInterface, ModuleGlobalInterface | ||
{ | ||
use ModuleAnalyticsTrait; | ||
use ModuleConfigTrait; | ||
use ModuleExternalUrlTrait; | ||
use ModuleGlobalTrait; | ||
|
||
/** | ||
* How should this module be identified in the control panel, etc.? | ||
* | ||
* @return string | ||
*/ | ||
public function title(): string | ||
{ | ||
return I18N::translate('Yandex Metrica'); | ||
} | ||
|
||
/** | ||
* Should this module be enabled when it is first installed? | ||
* | ||
* @return bool | ||
*/ | ||
public function isEnabledByDefault(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* Is this a tracker, as opposed to just a site-verification. | ||
* | ||
* @return bool | ||
*/ | ||
public function isTracker(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* Form fields to edit the parameters. | ||
* | ||
* @return string | ||
*/ | ||
public function analyticsFormFields(): string | ||
{ | ||
return view('modules/yandex-metrica/form', $this->analyticsParameters()); | ||
} | ||
|
||
/** | ||
* Home page for the service. | ||
* | ||
* @return string | ||
*/ | ||
public function externalUrl(): string | ||
{ | ||
return 'https://metrika.yandex.ru'; | ||
} | ||
|
||
/** | ||
* The parameters that need to be embedded in the snippet. | ||
* | ||
* @return array<string> | ||
*/ | ||
public function analyticsParameters(): array | ||
{ | ||
return [ | ||
'YANDEX_METRICA_ID' => $this->getPreference('YANDEX_METRICA_ID') | ||
]; | ||
} | ||
|
||
/** | ||
* Embed placeholders in the snippet. | ||
* | ||
* @param array<string> $parameters | ||
* | ||
* @return string | ||
*/ | ||
public function analyticsSnippet(array $parameters): string | ||
{ | ||
return view('modules/yandex-metrica/snippet', $parameters); | ||
} | ||
|
||
/** | ||
* Raw content, to be added at the end of the <head> element. | ||
* Typically, this will be <link> and <meta> elements. | ||
* | ||
* @return string | ||
*/ | ||
public function headContent(): string | ||
{ | ||
if ($this->analyticsCanShow()) { | ||
return $this->analyticsSnippet($this->analyticsParameters()); | ||
} | ||
|
||
return ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Fisharebest\Webtrees\I18N; | ||
|
||
?> | ||
|
||
<div class="row mb-3"> | ||
<label for="YANDEX_METRICA_ID" class="col-sm-3 col-form-label"> | ||
<?= /* I18N: A configuration setting */ I18N::translate('Yandex Metica') ?> | ||
<span class="visually-hidden">Yandex Metrica</span> | ||
</label> | ||
<div class="col-sm-9"> | ||
<input type="text" class="form-control" id="YANDEX_METRICA_ID" name="YANDEX_METRICA_ID" value="<?= e($YANDEX_METRICA_ID ?? '') ?>" maxlength="255" pattern="[0-9]+"> | ||
</div> | ||
</div> | ||
|
||
<p> | ||
<?= I18N::translate('Yandex Metrica') ?> | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @var string $YANDEX_METICA_ID, | ||
*/ | ||
?> | ||
<!-- Yandex.Metrika counter --> | ||
<script type="text/javascript" > | ||
(function(m,e,t,r,i,k,a){m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)}; | ||
m[i].l=1*new Date(); | ||
for (var j = 0; j < document.scripts.length; j++) {if (document.scripts[j].src === r) { return; }} | ||
k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)}) | ||
(window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym"); | ||
|
||
ym(<?= json_encode((int) ($YANDEX_METRICA_ID ?? ''), JSON_THROW_ON_ERROR) ?> , "init", { | ||
clickmap:true, | ||
trackLinks:true, | ||
accurateTrackBounce:true, | ||
webvisor:true | ||
}); | ||
</script> | ||
<noscript><div><img src="https://mc.yandex.ru/watch/<?= e($YANDEX_METRICA_ID ?? '') ?>" style="position:absolute; left:-9999px;" alt="" /></div></noscript> | ||
<!-- /Yandex.Metrika counter --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/** | ||
* webtrees: online genealogy | ||
* Copyright (C) 2023 webtrees development team | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fisharebest\Webtrees\Module; | ||
|
||
use Fisharebest\Webtrees\TestCase; | ||
|
||
/** | ||
* Test harness for the class YandexMetrica | ||
* | ||
* @covers Fisharebest\Webtrees\Module\YandexMetrica | ||
*/ | ||
class YandexMetricaTest extends TestCase | ||
{ | ||
public function testClass(): void | ||
{ | ||
$this->assertTrue(class_exists(\Fisharebest\Webtrees\Module\YandexMetrica::class)); | ||
} | ||
} |