@@ -4,7 +4,7 @@ import { useSession } from 'next-auth/react' import { useAppSelector } from '#/app/store/store' import { getDeviceType } from '#/shared/lib/helpers' -import { initYandexMetrika, stopYandexMetrika } from '#/shared/lib/yandex-metrika' +import { bindYandexMetrikaUserId, initYandexMetrika, stopYandexMetrika } from '#/shared/lib/yandex-metrika' import styles from './cookie-consent.module.scss' @@ -19,6 +19,7 @@ export function CookieConsent() { const { status } = useSession() const account_type = useAppSelector((state) => state.user.account_type) const meStatus = useAppSelector((state) => state.user.status) + const uid = useAppSelector((state) => state.user.uid) const [consent, setConsent] = React.useState(undefined) const desktop = getDeviceType() === 'desktop' @@ -30,13 +31,19 @@ export function CookieConsent() { } const stored = getConsent() setConsent(stored) - if (stored === 'all') initYandexMetrika() - }, [account_type, meStatus]) + if (stored === 'all') { + initYandexMetrika() + if (uid) bindYandexMetrikaUserId(uid) + } + }, [account_type, meStatus, uid]) const choose = (value: string) => { localStorage.setItem('cookie-consent', value) setConsent(value) - if (value === 'all') initYandexMetrika() + if (value === 'all') { + initYandexMetrika() + if (uid) bindYandexMetrikaUserId(uid) + } } if (status !== 'authenticated' || meStatus !== 'succeeded') return null @@ -33,3 +33,9 @@ export function initYandexMetrika() { webvisor: true, }) } + +export function bindYandexMetrikaUserId(userId: string) { + if (typeof window === 'undefined' || !userId) return + ym(93475901, 'setUserID', userId) + ym(93475901, 'userParams', { UserID: userId }) +}