Created
May 6, 2026 00:33
-
-
Save koras/a4c19db073c7944e8b33499cd3ac171a to your computer and use it in GitHub Desktop.
Упрощённый вариант без Suspense
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use client' | |
| import Script from 'next/script' | |
| import { usePathname } from 'next/navigation' | |
| import { useEffect } from 'react' | |
| const YM_ID = Number(process.env.NEXT_PUBLIC_YANDEX_METRIKA_ID) | |
| declare global { | |
| interface Window { | |
| ym?: (...args: any[]) => void | |
| } | |
| } | |
| export default function YandexMetrika() { | |
| const pathname = usePathname() | |
| useEffect(() => { | |
| if (!window.ym) return | |
| window.ym(YM_ID, 'hit', pathname) | |
| }, [pathname]) | |
| return ( | |
| <Script id="ym" strategy="afterInteractive"> | |
| {` | |
| (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(${YM_ID}, 'init', { | |
| defer: true, | |
| clickmap: true, | |
| trackLinks: true, | |
| accurateTrackBounce: true, | |
| webvisor: true | |
| }); | |
| `} | |
| </Script> | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment