Fix ya metrika hopefully

This commit is contained in:
2022-07-05 11:58:01 +03:00
parent a77fefc251
commit d3cb737972
6 changed files with 2948 additions and 22363 deletions

View File

@@ -0,0 +1,39 @@
import Router from 'next/router'
import React, { useCallback, useEffect } from 'react'
import ym, { YMInitializer } from 'react-yandex-metrika'
const enabled =
process.env.NODE_ENV === 'production' &&
process.env.YA_METRIKA_ID
const WithYandexMetrika = (props) => {
const { children } = props
const hit = useCallback((url) => {
if (enabled) {
ym('hit', url)
} else {
console.log('%c[YandexMetrika](HIT)', 'color: orange', url)
}
}, [])
useEffect(() => {
hit(window.location.pathname + window.location.search)
Router.events.on('routeChangeComplete', hit)
}, [hit])
return (
<>
{enabled && (
<YMInitializer
accounts={[Number(process.env.YA_METRIKA_ID)]}
options={{ webvisor: true, defer: true }}
version='2'
/>
)}
{children}
</>
)
}
export default WithYandexMetrika