From 66eef55aafe8ed6d0a9c1dfd074e344a9e99fd87 Mon Sep 17 00:00:00 2001 From: "dev.daminik00" Date: Mon, 28 Jul 2025 01:48:56 +0300 Subject: [PATCH] yandex metrika --- src/app/[locale]/layout.tsx | 2 + .../analytics/YandexMetrika/YandexMetrika.tsx | 83 +++++++++++++++++++ .../analytics/YandexMetrika/index.ts | 1 + 3 files changed, 86 insertions(+) create mode 100644 src/components/analytics/YandexMetrika/YandexMetrika.tsx create mode 100644 src/components/analytics/YandexMetrika/index.ts diff --git a/src/app/[locale]/layout.tsx b/src/app/[locale]/layout.tsx index acf3d6f..10550cf 100644 --- a/src/app/[locale]/layout.tsx +++ b/src/app/[locale]/layout.tsx @@ -9,6 +9,7 @@ import { hasLocale, NextIntlClientProvider } from "next-intl"; import { getMessages } from "next-intl/server"; import clsx from "clsx"; +import YandexMetrika from "@/components/analytics/YandexMetrika"; import { loadUser, loadUserId } from "@/entities/user/loaders"; import { routing } from "@/i18n/routing"; import { AppUiStoreProvider } from "@/providers/app-ui-store-provider"; @@ -63,6 +64,7 @@ export default async function RootLayout({ return ( + diff --git a/src/components/analytics/YandexMetrika/YandexMetrika.tsx b/src/components/analytics/YandexMetrika/YandexMetrika.tsx new file mode 100644 index 0000000..0b4cdcb --- /dev/null +++ b/src/components/analytics/YandexMetrika/YandexMetrika.tsx @@ -0,0 +1,83 @@ +"use client"; + +import { useEffect } from "react"; +import Script from "next/script"; + +const YANDEX_METRIKA_ID = 103412914; + +export default function YandexMetrika() { + useEffect(() => { + // Initialize Yandex.Metrika after script loads + const initializeYandexMetrika = () => { + if (typeof window.ym === "function") { + try { + window.ym(YANDEX_METRIKA_ID, "init", { + webvisor: true, + clickmap: true, + accurateTrackBounce: true, + trackLinks: true, + }); + } catch { + // Silently handle initialization errors + } + } + }; + + // Check if ym is already available or wait for it + if (typeof window.ym === "function") { + initializeYandexMetrika(); + } else { + // Wait for script to load + const checkYm = setInterval(() => { + if (typeof window.ym === "function") { + initializeYandexMetrika(); + clearInterval(checkYm); + } + }, 100); + + // Cleanup interval after 10 seconds + setTimeout(() => { + clearInterval(checkYm); + }, 10000); + + return () => clearInterval(checkYm); + } + }, []); + + return ( + <> + {/* Yandex.Metrika counter */} +