yandex和google统计修改
This commit is contained in:
+26
-9
@@ -6,7 +6,8 @@ import "./globals.css";
|
||||
|
||||
export const instant = false;
|
||||
|
||||
const GTAG_ID = process.env.NEXT_PUBLIC_GTAG_ID?.trim() || "";
|
||||
const GTM_ID = process.env.NEXT_PUBLIC_GTM_ID?.trim() || "";
|
||||
const YANDEX_METRIKA_ID = process.env.NEXT_PUBLIC_YANDEX_METRIKA_ID?.trim() || "";
|
||||
|
||||
/** metadataBase 随请求 Host 变化,保证 og:image 为绝对地址 */
|
||||
export async function generateMetadata(): Promise<Metadata> {
|
||||
@@ -44,21 +45,37 @@ export default function RootLayout({ children }: { children: React.ReactNode })
|
||||
<html lang="zh-CN" className="dark" data-theme="dark" data-scroll-behavior="smooth">
|
||||
<body>
|
||||
<AppShell>{children}</AppShell>
|
||||
{GTAG_ID ? (
|
||||
{GTM_ID ? (
|
||||
<>
|
||||
<Script
|
||||
id="ga4-tag"
|
||||
src={`https://www.googletagmanager.com/gtag/js?id=${GTAG_ID}`}
|
||||
strategy="afterInteractive"
|
||||
/>
|
||||
<Script id="ga4-init" strategy="afterInteractive">
|
||||
<Script id="gtm-init" strategy="afterInteractive">
|
||||
{`
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
function gtag(){dataLayer.push(arguments);}
|
||||
gtag('js', new Date());
|
||||
gtag('config', '${GTAG_ID}');
|
||||
dataLayer.push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });
|
||||
`}
|
||||
</Script>
|
||||
<Script id="gtm-tag" src={`https://www.googletagmanager.com/gtm.js?id=${GTM_ID}`} strategy="afterInteractive" />
|
||||
</>
|
||||
) : null}
|
||||
{YANDEX_METRIKA_ID ? (
|
||||
<>
|
||||
<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', 'https://mc.yandex.ru/metrika/tag.js?id=${YANDEX_METRIKA_ID}', 'ym');
|
||||
ym(${YANDEX_METRIKA_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/${YANDEX_METRIKA_ID}`} style={{ position: "absolute", left: "-9999px" }} alt="" />
|
||||
</div>
|
||||
</noscript>
|
||||
</>
|
||||
) : null}
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user