From bc351a3b11cdba2454c8852575ae8d0de13fdb05 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Fri, 14 Feb 2025 18:21:17 +0800 Subject: [PATCH] G-Tag fix --- components/Gtag.js | 13 +++++++++---- lib/plugins/gtag.js | 6 ++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/components/Gtag.js b/components/Gtag.js index 445b6e1ebf4..07f8af4eda1 100644 --- a/components/Gtag.js +++ b/components/Gtag.js @@ -1,12 +1,17 @@ -import { useEffect } from 'react' -import { useRouter } from 'next/router' +import { siteConfig } from '@/lib/config' import * as gtag from '@/lib/plugins/gtag' - +import { useRouter } from 'next/router' +import { useEffect } from 'react' +/** + * Google Analytics + * @returns + */ const Gtag = () => { const router = useRouter() + const ANALYTICS_GOOGLE_ID = siteConfig('ANALYTICS_GOOGLE_ID') useEffect(() => { const gtagRouteChange = url => { - gtag.pageview(url) + gtag.pageview(url, ANALYTICS_GOOGLE_ID) } router.events.on('routeChangeComplete', gtagRouteChange) return () => { diff --git a/lib/plugins/gtag.js b/lib/plugins/gtag.js index 322ab7e6908..9d62181fb69 100644 --- a/lib/plugins/gtag.js +++ b/lib/plugins/gtag.js @@ -1,8 +1,6 @@ -import BLOG from '@/blog.config' - // https://developers.google.com/analytics/devguides/collection/gtagjs/pages -export const pageview = url => { - window.gtag('config', BLOG.ANALYTICS_GOOGLE_ID, { +export const pageview = (url, ANALYTICS_GOOGLE_ID) => { + window.gtag('config', ANALYTICS_GOOGLE_ID, { page_path: url }) }