yandex metrica code

This commit is contained in:
Nikita 2026-09-26 21:00:15 +05:00
parent 08e94d7e91
commit 284c6b5c96
6 changed files with 34 additions and 91 deletions

View file

@ -0,0 +1,6 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>Verification: 701e19522bb44c01</body>
</html>

View file

@ -6,7 +6,6 @@ import { CookieConsent } from "@/widgets/cookie-consent";
import { Cursor } from "@/widgets/cursor";
import { Preloader } from "@/widgets/preloader";
import { ScrollCounter } from "@/widgets/scroll-counter";
import { YandexMetrika } from "@/widgets/yandex-metrika";
export function generateStaticParams() {
return [{ lang: "ru" }, { lang: "en" }];
@ -80,7 +79,6 @@ export default async function LocaleLayout({
<ContextMenu />
{children}
<CookieConsent />
<YandexMetrika />
</I18nProvider>
);
}

View file

@ -144,6 +144,33 @@ export default function RootLayout({ children }: { children: ReactNode }) {
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
/>
{/* Yandex.Metrika counter */}
<script
type="text/javascript"
dangerouslySetInnerHTML={{
__html: `
(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?id=113088628', 'ym');
ym(113088628, 'init', {ssr:true, webvisor:true, clickmap:true, ecommerce:"dataLayer", referrer: document.referrer, url: location.href, accurateTrackBounce:true, trackLinks:true});
`,
}}
/>
<noscript>
<div>
<img
src="https://mc.yandex.ru/watch/113088628"
style={{ position: "absolute", left: "-9999px" }}
alt=""
/>
</div>
</noscript>
{/* /Yandex.Metrika counter */}
</head>
<body className="min-h-full flex flex-col">
{children}

View file

@ -140,7 +140,7 @@ export const dictionaries: Record<Locale, Dictionary> = {
},
cookieConsent: {
title: "Мы используем файлы cookie",
text: "Этот сайт использует файлы cookie и сервис веб-аналитики для сбора обезличенных данных и улучшения работы.",
text: "Этот сайт использует файлы cookie для улучшения взаимодействия и корректной работы интерфейса.",
acceptButton: "Понятно",
},
preloader: {

View file

@ -1 +0,0 @@
export { default as YandexMetrika } from "./ui/YandexMetrika";

View file

@ -1,87 +0,0 @@
"use client";
import { useEffect, useRef, useState } from "react";
import { usePathname } from "next/navigation";
import Script from "next/script";
import {
COOKIE_CONSENT_EVENT,
COOKIE_CONSENT_KEY,
} from "@/widgets/cookie-consent";
declare global {
interface Window {
ym?: (
counterId: number,
action: "init" | "hit",
options?: Record<string, unknown> | string
) => void;
}
}
const counterId =
Number(process.env.NEXT_PUBLIC_YANDEX_METRIKA_ID) || 111930174;
const YandexMetrika = () => {
const pathname = usePathname();
const [accepted, setAccepted] = useState(false);
const previousUrl = useRef<string | null>(null);
useEffect(() => {
const timer = window.setTimeout(() => {
setAccepted(window.localStorage.getItem(COOKIE_CONSENT_KEY) === "accepted");
}, 0);
const onAccepted = () => setAccepted(true);
window.addEventListener(COOKIE_CONSENT_EVENT, onAccepted);
return () => {
window.clearTimeout(timer);
window.removeEventListener(COOKIE_CONSENT_EVENT, onAccepted);
};
}, []);
useEffect(() => {
if (!accepted || !counterId || !window.ym) return;
const url = window.location.href;
if (previousUrl.current === null) {
previousUrl.current = url;
return;
}
if (previousUrl.current === url) return;
previousUrl.current = url;
window.ym(counterId, "hit", url);
}, [accepted, pathname]);
if (!accepted || !counterId) return null;
return (
<Script
id="yandex-metrika"
strategy="afterInteractive"
dangerouslySetInnerHTML={{
__html: `
(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();
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(${counterId}, "init", {
accurateTrackBounce: true,
clickmap: true,
defer: true,
ecommerce: "dataLayer",
trackLinks: true,
webvisor: true
});
`,
}}
/>
);
};
export default YandexMetrika;