Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azlydnev yandex metrica #1

Merged
merged 7 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions app/Module/YandexMetrica.php
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 '';
}
}
2 changes: 2 additions & 0 deletions app/Services/ModuleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@
use Fisharebest\Webtrees\Module\WelcomeBlockModule;
use Fisharebest\Webtrees\Module\XeneaTheme;
use Fisharebest\Webtrees\Module\YahrzeitModule;
use Fisharebest\Webtrees\Module\YandexMetrica;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\Webtrees;
Expand Down Expand Up @@ -380,6 +381,7 @@ class ModuleService
'google-analytics' => GoogleAnalyticsModule::class,
'google-maps' => GoogleMaps::class,
'google-webmaster-tools' => GoogleWebmasterToolsModule::class,
'yandex-metrica' => YandexMetrica::class,
'here-maps' => HereMaps::class,
'hit-counter' => HitCountFooterModule::class,
'hourglass_chart' => HourglassChartModule::class,
Expand Down
21 changes: 21 additions & 0 deletions resources/views/modules/yandex-metrica/form.phtml
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>
25 changes: 25 additions & 0 deletions resources/views/modules/yandex-metrica/snippet.phtml
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 -->
35 changes: 35 additions & 0 deletions tests/app/Module/YandexMetricaTest.php
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));
}
}