Версия 1.1.1 Добавлена Яндекс метрика.
This commit is contained in:
parent
fcad77cf9d
commit
39a3ab7279
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "vite-react-typescript-starter",
|
"name": "vite-react-typescript-starter",
|
||||||
"private": true,
|
"private": true,
|
||||||
"version": "1.1.0",
|
"version": "1.1.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
|
|||||||
@ -13,6 +13,7 @@ import { BookmarksPage } from './pages/BookmarksPage';
|
|||||||
import { Footer } from './components/Footer';
|
import { Footer } from './components/Footer';
|
||||||
import { AuthGuard } from './components/AuthGuard';
|
import { AuthGuard } from './components/AuthGuard';
|
||||||
import { ImportArticlesPage } from "./pages/ImportArticlesPage";
|
import { ImportArticlesPage } from "./pages/ImportArticlesPage";
|
||||||
|
import YandexMetrika from "./components/YandexMetrika";
|
||||||
|
|
||||||
|
|
||||||
const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:5000';
|
const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:5000';
|
||||||
@ -46,6 +47,7 @@ function App() {
|
|||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
<div className="min-h-screen bg-gray-50 flex flex-col">
|
<div className="min-h-screen bg-gray-50 flex flex-col">
|
||||||
<div className="flex-1">
|
<div className="flex-1">
|
||||||
|
<YandexMetrika />
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path="/" element={<HomePage />} />
|
<Route path="/" element={<HomePage />} />
|
||||||
<Route path="/article/:id" element={<ArticlePage />} />
|
<Route path="/article/:id" element={<ArticlePage />} />
|
||||||
|
|||||||
79
src/components/YandexMetrika.tsx
Normal file
79
src/components/YandexMetrika.tsx
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
// src/components/YandexMetrika.tsx
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
import { useLocation } from 'react-router-dom';
|
||||||
|
|
||||||
|
|
||||||
|
// ⚙️ Мой ID счётчика Метрики
|
||||||
|
const YANDEX_METRIKA_ID = 104768935; // мой ID
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface Window {
|
||||||
|
ym?: ((id: number, method: string, ...params: unknown[]) => void) & {
|
||||||
|
a?: unknown[];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function YandexMetrika() {
|
||||||
|
const location = useLocation();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// 🧩 Не инициализируем Метрику в dev-режиме
|
||||||
|
if (import.meta.env.MODE !== 'production') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 🧩 Подключаем скрипт Метрики, если его ещё нет
|
||||||
|
if (!document.getElementById('ym-script')) {
|
||||||
|
const script = document.createElement('script');
|
||||||
|
script.id = 'ym-script';
|
||||||
|
script.src = 'https://mc.yandex.ru/metrika/tag.js';
|
||||||
|
script.async = true;
|
||||||
|
document.head.appendChild(script);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 🧩 Инициализация
|
||||||
|
window.ym =
|
||||||
|
window.ym ||
|
||||||
|
function (...args: unknown[]) {
|
||||||
|
(window.ym!.a = window.ym!.a || []).push(args);
|
||||||
|
};
|
||||||
|
|
||||||
|
window.ym(YANDEX_METRIKA_ID, 'init', {
|
||||||
|
clickmap: true,
|
||||||
|
trackLinks: true,
|
||||||
|
accurateTrackBounce: true,
|
||||||
|
webvisor: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
// 🧩 Обработчик переходов
|
||||||
|
const handleRouteChange = () => {
|
||||||
|
const path = location.pathname;
|
||||||
|
|
||||||
|
// 🚫 Игнорируем внутренние маршруты CRM / админки
|
||||||
|
const ignoredPaths = ['/login', '/admin'];
|
||||||
|
|
||||||
|
if (ignoredPaths.some(prefix => path.startsWith(prefix))) {
|
||||||
|
return; // не отправляем hit
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ Отправляем hit для публичных страниц
|
||||||
|
window.ym?.(YANDEX_METRIKA_ID, 'hit', path + location.search);
|
||||||
|
};
|
||||||
|
|
||||||
|
handleRouteChange();
|
||||||
|
}, [location]);
|
||||||
|
|
||||||
|
// 🧩 Поддержка noscript (Яндекс требует для полной валидации)
|
||||||
|
return (
|
||||||
|
<noscript>
|
||||||
|
<div>
|
||||||
|
<img
|
||||||
|
src={`https://mc.yandex.ru/watch/${YANDEX_METRIKA_ID}`}
|
||||||
|
style={{ position: 'absolute', left: '-9999px' }}
|
||||||
|
alt=""
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</noscript>
|
||||||
|
);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user