yandex添加

This commit is contained in:
谢宇宁
2026-10-17 09:59:58 +07:00
parent fe52aefe4a
commit bbd86e4119
7 changed files with 34 additions and 2 deletions
+2 -1
View File
@@ -39,6 +39,7 @@ import "@/styles/overrides/pwa-safe-area.css";
import type { ReactNode } from "react";
import { PwaProvider } from "@/components/pwa/PwaProvider";
import { GoogleAnalytics } from "@/components/analytics/GoogleAnalytics";
import { YandexMetrika } from "@/components/analytics/YandexMetrika";
import { serverConfig } from "@/server/config";
import { pwaViewport } from "@/lib/pwa/config";
import { getRequestPage } from "@/server/react/request-page";
@@ -83,7 +84,7 @@ export default async function RootLayout({
/>
)}
</head>
<body className={presentation.bodyClass}><PwaProvider />{children}<GoogleAnalytics id={serverConfig().googleAnalyticsID} /></body>
<body className={presentation.bodyClass}><PwaProvider />{children}<GoogleAnalytics id={serverConfig().googleAnalyticsID} /><YandexMetrika id={serverConfig().yandexMetrikaID} /></body>
</html>
);
}
@@ -0,0 +1,23 @@
import Script from "next/script";
/** Yandex.Metrika counter; loads after hydration, with the official noscript pixel. */
export function YandexMetrika({ id }: { id: string }) {
if (!id) return null;
const src = `https://mc.yandex.ru/metrika/tag.js?id=${id}`;
return (
<>
<Script id="yandex-metrika" 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();for(var j=0;j<document.scripts.length;j++){if(document.scripts[j].src===r){return;}}k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)})(window,document,'script',${JSON.stringify(src)},'ym');ym(${id},'init',{ssr:true,webvisor:true,clickmap:true,ecommerce:"dataLayer",referrer:document.referrer,url:location.href,accurateTrackBounce:true,trackLinks:true});`}
</Script>
<noscript>
<div>
<img
src={`https://mc.yandex.ru/watch/${id}`}
style={{ position: "absolute", left: "-9999px" }}
alt=""
/>
</div>
</noscript>
</>
);
}
+2
View File
@@ -27,6 +27,8 @@ export function serverConfig() {
navigationOnly: process.env.NAVIGATION_ONLY === 'true',
/** Google Analytics 4 measurement ID; empty or invalid disables the Google tag. */
googleAnalyticsID: /^G-[A-Z0-9]{4,20}$/.test((process.env.GOOGLE_ANALYTICS_ID || '').trim()) ? (process.env.GOOGLE_ANALYTICS_ID || '').trim() : '',
/** Yandex.Metrika counter ID; empty or invalid disables the counter. */
yandexMetrikaID: /^\d{4,12}$/.test((process.env.YANDEX_METRIKA_ID || '').trim()) ? (process.env.YANDEX_METRIKA_ID || '').trim() : '',
/** Every Telegram contact entry points here, overriding the upstream tgGroup/telegram/telegramAdd. */
contactTelegramURL: installationURL(process.env.CONTACT_TELEGRAM_URL) || 'https://t.me/jbd125',
installation: {