Files
warframe-center/app/components/WithYandexMetrika.js
2022-07-05 12:03:08 +03:00

40 lines
892 B
JavaScript

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