-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(wip): gtag * chore: rm tool-versions
- Loading branch information
1 parent
541fba4
commit 4377909
Showing
3 changed files
with
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.next/ | ||
node_modules/ | ||
.envrc |
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,15 @@ | ||
export const GA_TRACKING_ID = process.env.NEXT_PUBLIC_GA_ID; | ||
|
||
export const pageview = (url) => { | ||
window.gtag("config", GA_TRACKING_ID, { | ||
page_path: url, | ||
}); | ||
}; | ||
|
||
export const event = ({ action, category, label, value }) => { | ||
window.gtag("event", action, { | ||
event_category: category, | ||
event_label: label, | ||
value: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import Head from "next/head"; | ||
import Script from 'next/script' | ||
import { useEffect } from "react"; | ||
import { useRouter } from "next/router"; | ||
import * as gtag from "../lib/gtag"; | ||
|
||
export default function App({ Component, pageProps }) { | ||
const router = useRouter(); | ||
useEffect(() => { | ||
const handleRouteChange = (url) => { | ||
gtag.pageview(url); | ||
}; | ||
router.events.on("routeChangeComplete", handleRouteChange); | ||
return () => { | ||
router.events.off("routeChangeComplete", handleRouteChange); | ||
}; | ||
}, [router.events]); | ||
|
||
return ( | ||
|
||
<> | ||
<Head> | ||
<script | ||
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, | ||
}); | ||
`, | ||
}} | ||
/> | ||
</Head> | ||
{/* Global Site Tag (gtag.js) - Google Analytics */} | ||
<Script | ||
strategy="afterInteractive" | ||
src={`https://www.googletagmanager.com/gtag/js?id=${gtag.GA_TRACKING_ID}`} | ||
/> | ||
<Component {...pageProps} /> | ||
</> | ||
); | ||
} |