-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from hufs-sports-live/fix/ga
[FIX]: 서버 컴포넌트에서 useEffect가 안돌아가는 문제 해결
- Loading branch information
Showing
7 changed files
with
92 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use client'; | ||
|
||
import Script from 'next/script'; | ||
|
||
import * as gtag from '@/utils/gtag'; | ||
|
||
export default function GoogleAnalytics() { | ||
gtag.useGtag(); | ||
|
||
return ( | ||
<> | ||
{process.env.NODE_ENV !== 'development' && ( | ||
<> | ||
{/* Global Site Tag (gtag.js) - Google Analytics */} | ||
<Script | ||
strategy="afterInteractive" | ||
src={`https://www.googletagmanager.com/gtag/js?id=${gtag.GA_TRACKING_ID}`} | ||
/> | ||
<Script | ||
id="gtag-init" | ||
strategy="afterInteractive" | ||
dangerouslySetInnerHTML={{ | ||
__html: ` | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
gtag('js', new Date()); | ||
gtag('config', '${gtag.GA_TRACKING_ID}', { | ||
page_path: window.location.pathname, | ||
}); | ||
`, | ||
}} | ||
/> | ||
</> | ||
)} | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/// <reference types="gtag.js" /> | ||
|
||
declare module 'gtag.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { usePathname } from 'next/navigation'; | ||
import { useEffect, useRef } from 'react'; | ||
|
||
export const GA_TRACKING_ID = process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_TAG; | ||
|
||
// https://developers.google.com/analytics/devguides/collection/gtagjs/pages | ||
export const pageview = (url: URL) => { | ||
window.gtag('config', GA_TRACKING_ID as string, { | ||
page_path: url, | ||
}); | ||
}; | ||
|
||
// https://developers.google.com/analytics/devguides/collection/gtagjs/events | ||
export const event = ( | ||
action: Gtag.EventNames, | ||
{ event_category, event_label, value }: Gtag.EventParams, | ||
) => { | ||
window.gtag('event', action, { | ||
event_category, | ||
event_label, | ||
value, | ||
}); | ||
}; | ||
|
||
export const useGtag = () => { | ||
const pathname = usePathname(); // Get current route | ||
|
||
// Save pathname on component mount into a REF | ||
const savedPathNameRef = useRef(pathname); | ||
|
||
useEffect(() => { | ||
if (process.env.NODE_ENV === 'development') return; | ||
|
||
const handleRouteChange = (url: URL) => { | ||
pageview(url); | ||
}; | ||
|
||
if (savedPathNameRef.current !== pathname) { | ||
handleRouteChange(new URL(pathname, window.location.origin)); | ||
// Update REF | ||
savedPathNameRef.current = pathname; | ||
} | ||
}, [pathname]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters