Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ The project uses several custom `$settings` in `settings.php`:
| `telegram_secret_token` | Secret token for Telegram webhook verification |
| `telegram_chat_id` | Telegram chat ID for moderation notifications |
| `app_foresight` | (default: `TRUE`) Allows to disable the ForesightJS prefetch library for an environment. Only active for anonymous users. When disabled, no link prefetching is performed. |
| `app_yandex_metrika_id` | Yandex.Metrika counter ID. When set, adds a tracking pixel (`<img src="https://mc.yandex.ru/watch/{ID}" …>`) to the bottom of every page. |

## 🧬 Quality Tools

Expand Down
30 changes: 30 additions & 0 deletions app/modules/main/src/Hook/Core/YandexMetrikaPageBottom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Drupal\app_main\Hook\Core;

use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Site\Settings;

#[Hook('page_bottom')]
final readonly class YandexMetrikaPageBottom {

public function __invoke(array &$page_bottom): void {
$metrika_id = Settings::get('app_yandex_metrika_id');
if (!\is_string($metrika_id) || $metrika_id === '') {
return;
}

$page_bottom['app_main_yandex_metrika'] = [
'#type' => 'html_tag',
'#tag' => 'img',
'#attributes' => [
'src' => 'https://mc.yandex.ru/watch/' . \rawurlencode($metrika_id),
'style' => 'position:absolute; left:-9999px;',
'alt' => '',
],
];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\app_main\Unit\Hook\Core;

use Drupal\app_main\Hook\Core\YandexMetrikaPageBottom;
use Drupal\Core\Site\Settings;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(YandexMetrikaPageBottom::class)]
final class YandexMetrikaPageBottomTest extends UnitTestCase {

protected function setUp(): void {
parent::setUp();
new Settings([]);
}

public function testNoIdAddsNothing(): void {
$hook = new YandexMetrikaPageBottom();
$page_bottom = [];

$hook($page_bottom);

self::assertEmpty($page_bottom);
}

public function testEmptyStringAddsNothing(): void {
new Settings(['app_yandex_metrika_id' => '']);
$hook = new YandexMetrikaPageBottom();
$page_bottom = [];

$hook($page_bottom);

self::assertEmpty($page_bottom);
}

public function testNonStringAddsNothing(): void {
new Settings(['app_yandex_metrika_id' => 123]);
$hook = new YandexMetrikaPageBottom();
$page_bottom = [];

$hook($page_bottom);

self::assertEmpty($page_bottom);
}

public function testValidIdAddsImage(): void {
new Settings(['app_yandex_metrika_id' => '48390929']);
$hook = new YandexMetrikaPageBottom();
$page_bottom = [];

$hook($page_bottom);

self::assertArrayHasKey('app_main_yandex_metrika', $page_bottom);
self::assertSame('html_tag', $page_bottom['app_main_yandex_metrika']['#type']);
self::assertSame('img', $page_bottom['app_main_yandex_metrika']['#tag']);
self::assertSame('https://mc.yandex.ru/watch/48390929', $page_bottom['app_main_yandex_metrika']['#attributes']['src']);
self::assertSame('position:absolute; left:-9999px;', $page_bottom['app_main_yandex_metrika']['#attributes']['style']);
self::assertSame('', $page_bottom['app_main_yandex_metrika']['#attributes']['alt']);
}

public function testIdIsEncoded(): void {
new Settings(['app_yandex_metrika_id' => '48<39>09']);
$hook = new YandexMetrikaPageBottom();
$page_bottom = [];

$hook($page_bottom);

self::assertSame('https://mc.yandex.ru/watch/48%3C39%3E09', $page_bottom['app_main_yandex_metrika']['#attributes']['src']);
}

}
1 change: 1 addition & 0 deletions assets/scaffold/local.settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@
$settings['telegram_chat_id'] = NULL;
$settings['telegram_secret_token'] = NULL;
$settings['app_foresight'] = FALSE;
$settings['app_yandex_metrika_id'] = NULL;

$config['cache_pilot.settings']['connection_dsn'] = 'tcp://php:9000';
2 changes: 2 additions & 0 deletions config/cspell/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ TYPEHINT
Webmozart
wght
youtu
metrika
Metrika
Loading