Skip to content

Commit

Permalink
G-Tag fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Feb 14, 2025
1 parent f5be02e commit bc351a3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
13 changes: 9 additions & 4 deletions components/Gtag.js
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down
6 changes: 2 additions & 4 deletions lib/plugins/gtag.js
Original file line number Diff line number Diff line change
@@ -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
})
}
Expand Down

0 comments on commit bc351a3

Please sign in to comment.