Skip to content

Instantly share code, notes, and snippets.

@koras
Created May 6, 2026 00:33
Show Gist options
  • Select an option

  • Save koras/a4c19db073c7944e8b33499cd3ac171a to your computer and use it in GitHub Desktop.

Select an option

Save koras/a4c19db073c7944e8b33499cd3ac171a to your computer and use it in GitHub Desktop.
Упрощённый вариант без Suspense
'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