Skip to content

Commit 3ec6b89

Browse files
authored
feat(analytics): add GA4 tracking (#16)
1 parent 23fcf0a commit 3ec6b89

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,8 @@ Guilherme Biff Zarelli
122122
- **LinkedIn**: [https://linkedin.com/in/gbzarelli/](https://linkedin.com/in/gbzarelli/)
123123
- **GitHub**: [https://github.com/gbzarelli](https://github.com/gbzarelli)
124124
- **Articles in Medium**: [https://medium.com/@guilherme.zarelli](https://medium.com/@guilherme.zarelli)
125-
- **Email**: [gbzarelli@helpdev.com.br](mailto:gbzarelli@helpdev.com.br)
125+
- **Email**: [gbzarelli@helpdev.com.br](mailto:gbzarelli@helpdev.com.br)
126+
127+
## Google Analytics
128+
129+
O site envia pageviews para a propriedade GA4 `HelpDev Website` (`G-FYBBYE3DXD`), incluindo navegações internas. Para usar outra propriedade, defina `NEXT_PUBLIC_GA_ID` no ambiente de build.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"use client";
2+
3+
import { useEffect, useRef } from "react";
4+
import { usePathname } from "next/navigation";
5+
import Script from "next/script";
6+
7+
const measurementId = process.env.NEXT_PUBLIC_GA_ID || "G-FYBBYE3DXD";
8+
9+
declare global {
10+
interface Window {
11+
dataLayer: unknown[][];
12+
gtag: (...args: unknown[]) => void;
13+
}
14+
}
15+
16+
export default function GoogleAnalytics() {
17+
const pathname = usePathname();
18+
const lastTrackedPath = useRef<string | null>(null);
19+
20+
useEffect(() => {
21+
if (!pathname || lastTrackedPath.current === pathname) return;
22+
23+
lastTrackedPath.current = pathname;
24+
window.gtag("event", "page_view", {
25+
page_path: pathname,
26+
page_location: `${window.location.origin}${pathname}`,
27+
page_title: document.title,
28+
});
29+
}, [pathname]);
30+
31+
return (
32+
<>
33+
<Script
34+
src={`https://www.googletagmanager.com/gtag/js?id=${measurementId}`}
35+
strategy="afterInteractive"
36+
/>
37+
<Script id="google-analytics-init" strategy="beforeInteractive">
38+
{`window.dataLayer = window.dataLayer || [];
39+
window.gtag = window.gtag || function(...args) { window.dataLayer.push(args); };
40+
window.gtag('js', new Date());
41+
window.gtag('config', '${measurementId}', { send_page_view: false });`}
42+
</Script>
43+
</>
44+
);
45+
}

‎src/app/layout.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Metadata, Viewport } from "next";
22
import { Inter } from "next/font/google";
33
import "./globals.css";
44
import Footer from "./components/Footer";
5+
import GoogleAnalytics from "./components/GoogleAnalytics";
56
import Script from "next/script";
67

78
const inter = Inter({ subsets: ["latin"] });
@@ -74,6 +75,7 @@ export default function RootLayout({
7475
return (
7576
<html lang="pt-BR">
7677
<body className={inter.className} suppressHydrationWarning>
78+
<GoogleAnalytics />
7779
<Script id="ld-organization" type="application/ld+json" strategy="afterInteractive"
7880
dangerouslySetInnerHTML={{ __html: JSON.stringify({
7981
'@context': 'https://schema.org',

0 commit comments

Comments
 (0)