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

Yandex Metrica counter snippet #4895

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
122 changes: 122 additions & 0 deletions app/Module/YandexMetrica.php
@@ -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

Check warning on line 39 in app/Module/YandexMetrica.php

View check run for this annotation

Codecov / codecov/patch

app/Module/YandexMetrica.php#L39

Added line #L39 was not covered by tests
{
return I18N::translate('Yandex Metrica');

Check warning on line 41 in app/Module/YandexMetrica.php

View check run for this annotation

Codecov / codecov/patch

app/Module/YandexMetrica.php#L41

Added line #L41 was not covered by tests
}

/**
* Should this module be enabled when it is first installed?
*
* @return bool
*/
public function isEnabledByDefault(): bool

Check warning on line 49 in app/Module/YandexMetrica.php

View check run for this annotation

Codecov / codecov/patch

app/Module/YandexMetrica.php#L49

Added line #L49 was not covered by tests
{
return false;

Check warning on line 51 in app/Module/YandexMetrica.php

View check run for this annotation

Codecov / codecov/patch

app/Module/YandexMetrica.php#L51

Added line #L51 was not covered by tests
}

/**
* Is this a tracker, as opposed to just a site-verification.
*
* @return bool
*/
public function isTracker(): bool

Check warning on line 59 in app/Module/YandexMetrica.php

View check run for this annotation

Codecov / codecov/patch

app/Module/YandexMetrica.php#L59

Added line #L59 was not covered by tests
{
return false;

Check warning on line 61 in app/Module/YandexMetrica.php

View check run for this annotation

Codecov / codecov/patch

app/Module/YandexMetrica.php#L61

Added line #L61 was not covered by tests
}

/**
* Form fields to edit the parameters.
*
* @return string
*/
public function analyticsFormFields(): string

Check warning on line 69 in app/Module/YandexMetrica.php

View check run for this annotation

Codecov / codecov/patch

app/Module/YandexMetrica.php#L69

Added line #L69 was not covered by tests
{
return view('modules/yandex-metrica/form', $this->analyticsParameters());

Check warning on line 71 in app/Module/YandexMetrica.php

View check run for this annotation

Codecov / codecov/patch

app/Module/YandexMetrica.php#L71

Added line #L71 was not covered by tests
}

/**
* Home page for the service.
*
* @return string
*/
public function externalUrl(): string

Check warning on line 79 in app/Module/YandexMetrica.php

View check run for this annotation

Codecov / codecov/patch

app/Module/YandexMetrica.php#L79

Added line #L79 was not covered by tests
{
return 'https://metrika.yandex.ru';

Check warning on line 81 in app/Module/YandexMetrica.php

View check run for this annotation

Codecov / codecov/patch

app/Module/YandexMetrica.php#L81

Added line #L81 was not covered by tests
}

/**
* The parameters that need to be embedded in the snippet.
*
* @return array<string>
*/
public function analyticsParameters(): array

Check warning on line 89 in app/Module/YandexMetrica.php

View check run for this annotation

Codecov / codecov/patch

app/Module/YandexMetrica.php#L89

Added line #L89 was not covered by tests
{
return [
'YANDEX_METRICA_ID' => $this->getPreference('YANDEX_METRICA_ID')
];

Check warning on line 93 in app/Module/YandexMetrica.php

View check run for this annotation

Codecov / codecov/patch

app/Module/YandexMetrica.php#L91-L93

Added lines #L91 - L93 were not covered by tests
}

/**
* Embed placeholders in the snippet.
*
* @param array<string> $parameters
*
* @return string
*/
public function analyticsSnippet(array $parameters): string

Check warning on line 103 in app/Module/YandexMetrica.php

View check run for this annotation

Codecov / codecov/patch

app/Module/YandexMetrica.php#L103

Added line #L103 was not covered by tests
{
return view('modules/yandex-metrica/snippet', $parameters);

Check warning on line 105 in app/Module/YandexMetrica.php

View check run for this annotation

Codecov / codecov/patch

app/Module/YandexMetrica.php#L105

Added line #L105 was not covered by tests
}

/**
* 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

Check warning on line 114 in app/Module/YandexMetrica.php

View check run for this annotation

Codecov / codecov/patch

app/Module/YandexMetrica.php#L114

Added line #L114 was not covered by tests
{
if ($this->analyticsCanShow()) {
return $this->analyticsSnippet($this->analyticsParameters());

Check warning on line 117 in app/Module/YandexMetrica.php

View check run for this annotation

Codecov / codecov/patch

app/Module/YandexMetrica.php#L116-L117

Added lines #L116 - L117 were not covered by tests
}

return '';

Check warning on line 120 in app/Module/YandexMetrica.php

View check run for this annotation

Codecov / codecov/patch

app/Module/YandexMetrica.php#L120

Added line #L120 was not covered by tests
}
}
2 changes: 2 additions & 0 deletions app/Services/ModuleService.php
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
@@ -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') ?>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: Metica -> Metrica

<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
@@ -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", {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are casting this value to an int, then we do not need json_encode()?

Also, $YANDEX_METRICA_ID can never be null. So we do not need ?? ''

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
@@ -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));
}
}