-
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.
- Loading branch information
1 parent
26b98c9
commit a6c9804
Showing
6 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,40 @@ | ||
'use client' | ||
import { usePathname, useSearchParams } from 'next/navigation' | ||
import Script from 'next/script' | ||
import { useEffect } from 'react' | ||
|
||
import { existsGaId, GA_MEASUREMENT_ID, pageview } from '../../gtag' | ||
|
||
const GoogleAnalytics = () => { | ||
const pathname = usePathname() | ||
const searchParams = useSearchParams() | ||
|
||
useEffect(() => { | ||
if (!existsGaId) { | ||
return | ||
} | ||
const url = pathname + searchParams.toString() | ||
pageview(url) | ||
}, [pathname, searchParams]) | ||
|
||
return ( | ||
<> | ||
<Script | ||
strategy='lazyOnload' | ||
src={`https://www.googletagmanager.com/gtag/js?id=${GA_MEASUREMENT_ID}`} | ||
/> | ||
<Script id='gtag-init' strategy='afterInteractive'> | ||
{` | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
gtag('js', new Date()); | ||
gtag('config', '${GA_MEASUREMENT_ID}', { | ||
page_path: window.location.pathname, | ||
}); | ||
`} | ||
</Script> | ||
</> | ||
) | ||
} | ||
|
||
export default GoogleAnalytics |
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 @@ | ||
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,9 @@ | ||
export const GA_MEASUREMENT_ID = process.env.NEXT_PUBLIC_GA_ID || ""; | ||
|
||
export const existsGaId = GA_MEASUREMENT_ID !== ""; | ||
|
||
export const pageview = (path: string) => { | ||
window.gtag("config", GA_MEASUREMENT_ID, { | ||
page_path: path, | ||
}); | ||
}; |