-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
95 additions
and
0 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,28 @@ | ||
import Script from "next/script"; | ||
|
||
import { PRODUCTION } from "../../constants/config"; | ||
import { GA_TRACKING_ID } from "../../libs/gtag"; | ||
|
||
function Scripts() { | ||
return ( | ||
<div> | ||
{PRODUCTION && ( | ||
<> | ||
<Script | ||
src={`https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`} | ||
/> | ||
<Script strategy="lazyOnload" id="ga"> | ||
{`window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
gtag('js', new Date()); | ||
gtag('config', '${GA_TRACKING_ID}', { | ||
page_path: window.location.pathname, | ||
});`} | ||
</Script> | ||
</> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
export default Scripts; |
22 changes: 22 additions & 0 deletions
22
packages/frontend/src/components/analytics/useGtagEffect.ts
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,22 @@ | ||
import { useRouter } from "next/router"; | ||
import { useEffect } from "react"; | ||
|
||
import { PRODUCTION } from "../../constants/config"; | ||
import * as gtag from "../../libs/gtag"; | ||
|
||
const useGtagEffect = () => { | ||
const router = useRouter(); | ||
useEffect(() => { | ||
if (!PRODUCTION) return () => {}; | ||
|
||
const handleRouteChange = (url: URL) => { | ||
gtag.pageview(url); | ||
}; | ||
router.events.on("routeChangeComplete", handleRouteChange); | ||
return () => { | ||
router.events.off("routeChangeComplete", handleRouteChange); | ||
}; | ||
}, [router.events]); | ||
return null; | ||
}; | ||
export default useGtagEffect; |
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 @@ | ||
export const PRODUCTION = process.env.NODE_ENV === "production"; |
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,30 @@ | ||
export const GA_TRACKING_ID: string = | ||
process.env.NEXT_PUBLIC_GOOGLE_MEASUREMENT_ID || ""; | ||
|
||
// https://developers.google.com/analytics/devguides/collection/gtagjs/pages | ||
export const pageview = (url: URL) => { | ||
if (typeof window !== "object") return; | ||
window.gtag("config", GA_TRACKING_ID, { | ||
page_path: url, | ||
}); | ||
}; | ||
|
||
// https://developers.google.com/analytics/devguides/collection/gtagjs/events | ||
export const event = ({ | ||
action, | ||
category, | ||
label, | ||
value, | ||
}: { | ||
action: string; | ||
category: string; | ||
label: string; | ||
value: string; | ||
}) => { | ||
if (typeof window !== "object") return; | ||
window.gtag("event", action, { | ||
event_category: category, | ||
event_label: label, | ||
value, | ||
}); | ||
}; |
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